-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path021graphs.py
More file actions
39 lines (35 loc) · 795 Bytes
/
021graphs.py
File metadata and controls
39 lines (35 loc) · 795 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
39
graph = {
'a':['c','b'],
'b':['d'],
'c':['e'],
'd':['f'],
'e':[],
'f':[],
}
def depthFirstPrint(graph,source):
stack = [source]
while len(stack)>0:
current = stack[-1]
stack = stack[:-1:]
print(current,end=' ')
for ele in graph[current]:
stack.append(ele)
print()
def depthRecursivFirstPrint(graph,source):
print(source,end=' ')
if len(graph[source]) > 0:
for ele in graph[source][::-1]:
depthRecursivFirstPrint(graph,ele)
def breadthFirstPrint(graph,source):
stack = [source]
while len(stack)>0:
current = stack[0]
stack = stack[1::]
print(current,end=' ')
for ele in graph[current]:
stack.append(ele)
print()
depthRecursivFirstPrint(graph,'a')
print()
depthFirstPrint(graph,'a')
breadthFirstPrint(graph,'a')