-
Notifications
You must be signed in to change notification settings - Fork 122
/
BFS.py
29 lines (26 loc) · 847 Bytes
/
BFS.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
class Graph:
def __init__(self,nodes):
self.graph=[]
for i in range(0,nodes):
self.graph.append([])
def addedge(self,initial,final):
(self.graph[initial]).append(final)
# Graph is the adjacency list
def BFS(self,root):
visited = []
bfs = []
# Using Queue implementation
for i in range(0,len(self.graph)):
visited.append(False)
bfs.append(root)
visited[root] = True
while len(bfs)!=0:
t = bfs.pop(0)
# Can change this part to your own convenience
print(t)
# Endblock
if len(self.graph[t])>0:
for i in self.graph[t]:
if visited[i] == False:
bfs.append(i)
visited[i] = True