-
Notifications
You must be signed in to change notification settings - Fork 364
/
BreadthFirstSearch.py
46 lines (34 loc) · 1.23 KB
/
BreadthFirstSearch.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
45
46
from collections import deque
class Graph:
def __init__(self, v):
self.v = v
self.adj = [[] for _ in range(v)]
def addEdge(self, v, w):
self.adj[v].append(w)
self.adj[w].append(v)
# BFS traversal from a given source node 's'
def BFS(self, s):
#Create a queue for BFS traversal
queue = deque()
visited = [False] * self.v
# Mark the current node as visited and add into queue
visited[s] = True
queue.append(s)
while queue:
curr = queue.popleft()
# Remove a vertex from queue and print it
print(curr, end=" ")
# Get all adjacent vertices of the dequeued vertex s.
# If an adjacent has not been visited, then mark it visited and enqueue it
for nbr in self.adj[curr]:
if not visited[nbr]:
queue.append(nbr)
visited[nbr] = True
if __name__ == "__main__":
N, M = map(int, input("Enter the no. of vertices and edges: ").split())
g = Graph(N)
for i in range(M):
u, v = map(int, input(f"Enter edge No. {i+1}: ").split())
g.addEdge(u, v)
s = int(input("Enter source vertex: "))
g.BFS(s)