-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_BFS.py
More file actions
39 lines (34 loc) · 721 Bytes
/
graph_BFS.py
File metadata and controls
39 lines (34 loc) · 721 Bytes
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
class Graph:
def __init__(self, vertices):
self.V = vertices
self.graph = defaultdict(list)
# Function to add an edge in an undirected graph
def add_edge(self, src, dest):
self.graph[src].append(dest)
self.graph[dest].append(src)
def BFS(n, vertex):
lst = [0]*n
queue = []
queue.append(vertex)
ans = []
lst[vertex] = 1
while(queue!=[]):
x = queue.pop(0)
ans.append(x)
ar = g.graph[x]
for i in ar:
if lst[i] == 0:
queue.append(i)
lst[i] = 1
return ans
t = 5
g = Graph(t)
g.add_edge(0, 1)
g.add_edge(0, 4)
g.add_edge(1, 2)
g.add_edge(1, 3)
g.add_edge(1, 4)
g.add_edge(2, 3)
g.add_edge(3, 4)
print(BFS(t, 0))