-
Notifications
You must be signed in to change notification settings - Fork 304
/
Copy pathbreath_first_search_algorithm.py
39 lines (31 loc) · 1.18 KB
/
breath_first_search_algorithm.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
from collections import defaultdict, deque
class Graph:
def __init__(self):
self.graph = defaultdict(list)
def add_edge(self, u, v):
# Add an edge from vertex u to vertex v
self.graph[u].append(v)
def bfs(self, start_vertex):
visited = set() # Set to keep track of visited vertices
queue = deque() # Queue for BFS traversal
visited.add(start_vertex) # Mark the start vertex as visited
queue.append(start_vertex) # Add the start vertex to the queue
while queue:
vertex = queue.popleft() # Get the next vertex from the queue
print(f"Visited vertex: {vertex}")
for neighbor in self.graph[vertex]:
if neighbor not in visited:
visited.add(neighbor) # Mark the neighbor as visited
queue.append(neighbor) # Add the neighbor to the queue
# Example usage and test
if __name__ == "__main__":
# Create a sample graph
g = Graph()
g.add_edge(0, 1)
g.add_edge(0, 2)
g.add_edge(1, 2)
g.add_edge(2, 0)
g.add_edge(2, 3)
g.add_edge(3, 3)
print("Breadth-First Traversal (starting from vertex 2):")
g.bfs(2)