-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathBFS_BreadthFirstSearch.py
More file actions
59 lines (50 loc) · 1.51 KB
/
BFS_BreadthFirstSearch.py
File metadata and controls
59 lines (50 loc) · 1.51 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
48
49
50
51
52
53
54
55
56
57
58
59
from collections import deque
class Graph:
def __init__(self):
self.graph = {}
def add_edge(self, vertex, neighbor):
if vertex in self.graph:
self.graph[vertex].append(neighbor)
else:
self.graph[vertex] = [neighbor]
def bfs(self, start):
visited = set()
queue = deque()
queue.append(start)
visited.add(start)
while queue:
vertex = queue.popleft()
print(vertex, end=" ")
for neighbor in self.graph.get(vertex, []):
if neighbor not in visited:
queue.append(neighbor)
visited.add(neighbor)
if __name__ == "__main__":
graph = Graph()
graph.add_edge(0, 1)
graph.add_edge(0, 2)
graph.add_edge(1, 2)
graph.add_edge(2, 0)
graph.add_edge(2, 3)
graph.add_edge(3, 3)
graph.add_edge(4, 5)
graph.add_edge(5, 6)
graph.add_edge(6, 7)
graph.add_edge(7, 4)
graph.add_edge(8, 9)
graph.add_edge(9, 10)
graph.add_edge(10, 11)
graph.add_edge(11, 8)
graph.add_edge(12, 13)
graph.add_edge(13, 14)
graph.add_edge(14, 15)
graph.add_edge(15, 12)
graph.add_edge(16, 17)
graph.add_edge(17, 18)
graph.add_edge(18, 19)
graph.add_edge(19, 16)
print("Breadth-First Traversal (starting from vertex 2):")
graph.bfs(2)
print("\n")
print("Breadth-First Traversal (starting from vertex 9):")
graph.bfs(9)