forked from VikashPR/18CSC305J-AI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBFS-DFS.py
44 lines (33 loc) · 768 Bytes
/
BFS-DFS.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': []
}
visited_bfs = []
queue = []
def bfs(visited_bfs, graph, node):
visited_bfs.append(node)
queue.append(node)
while queue:
s = queue.pop(0)
print(s, end=" ")
for neighbour in graph[s]:
if neighbour not in visited_bfs:
visited_bfs.append(neighbour)
queue.append(neighbour)
visited = set()
def dfs(visited, graph, node):
if node not in visited:
print(node, end=" ")
visited.add(node)
for neighbour in graph[node]:
dfs(visited, graph, neighbour)
print("BFS:", end=" ")
bfs(visited_bfs, graph, 'A')
print('\n')
print("DFS:", end=" ")
dfs(visited, graph, 'A')
print(test[0])