-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.py
44 lines (35 loc) · 773 Bytes
/
Graph.py
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
40
41
42
43
44
graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F'],
'D': [],
'E': ['F'],
'F': []
}
counter_dfs = 0
def iterative_dfs(graph, start, path=[]):
global counter_dfs
q = [start]
while q:
v = q.pop(0)
if v not in path:
counter_dfs += 1
path = path + [v]
q = graph[v] + q
return path
counter_bfs = 0
def iterative_bfs(graph, start, path=[]):
global counter_bfs
q = [start]
while q:
v = q.pop(0)
if not v in path:
counter_bfs += 1
path = path + [v]
q = q + graph[v]
return path
print(iterative_dfs(graph, 'A'))
print(counter_dfs)
print('-----------------')
print(iterative_bfs(graph, 'A'))
print(counter_bfs)