-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComparison_between_BFS_and_DFS.py
More file actions
47 lines (37 loc) · 1.03 KB
/
Comparison_between_BFS_and_DFS.py
File metadata and controls
47 lines (37 loc) · 1.03 KB
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
45
46
47
# This code is for comparing between BFS and DFS Search Algorithms
graph = {
'A': ['C', 'B'],
'B': ['F', "G"],
'C': ['D', "E"],
'D': [],
'E': ['I', 'H'],
"F": [],
'G': [],
"H": [],
'I': ['J', 'K'],
'J': [],
'K': ['L', 'M'],
'L': [],
'M': []
}
def breadth_first_search(visit_complete, graph, current_node):
visit_complete.append(current_node)
queue = [current_node]
while queue:
s = queue.pop(0)
print(s)
for neighbour in graph[s]:
if neighbour not in visit_complete:
visit_complete.append(neighbour)
queue.append(neighbour)
print("Breadth first search algorithm: ")
breadth_first_search([], graph, 'A')
visited = set()
def depth_first_search(visited, graph, node):
if node not in visited:
print(node)
visited.add(node)
for neighbour in graph[node]:
depth_first_search(visited, graph, neighbour)
print("Depth first search algorithm: ")
depth_first_search(visited, graph, 'A')