-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfs.py
More file actions
38 lines (33 loc) · 672 Bytes
/
Copy pathdfs.py
File metadata and controls
38 lines (33 loc) · 672 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def dfs(graph,source):
stack = [source]
while(len(stack)>0):
current=stack.pop()
print(current)
a= graph[current]
for i in a:
stack.append(i)
def rec_dfs(graph,source):
print(source)
for neigbor in graph[source]:
rec_dfs(graph,neigbor)
def bfs(graph,source):
queue = [source]
while(len(queue)>0):
current=queue.pop(0)
print(current)
for neigbor in graph[current]:
queue.append(neigbor)
graph = {
'a':['b','c',],
'b':['d'],
'c':['e'],
'd' : ['f'],
'e':[],
'f':[]
}
'''graph = {'0': set(['1', '2']),
'1': set(['0', '3', '4']),
'2': set(['0']),
'3': set(['1']),
'4': set(['2', '3'])}'''
bfs(graph,'a')