-
Notifications
You must be signed in to change notification settings - Fork 0
/
geeks_BFS.py
48 lines (39 loc) · 942 Bytes
/
geeks_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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from collections import defaultdict
class Graph:
def __init__(self):
self.graph = defaultdict(list)
#adding an edge
def addEdge(self, u, v):
self.graph[u].append(v)
#print the BFS graph
def BFS(self, s):
#mark all vertices as !visited
visited = [False] * (max(self.graph) + 1)
queue = [] #create a queue
queue.append(s)
visited[s] = True
#iterate while len(queue) =! 0
while queue:
#dequeue a vertex
s = queue.pop(0)
print(s, end=' ')
'''
get all adjacent vertices of
dequeued vertex s, if adjacent
!visited, mark, visit & enqueue
'''
for i in self.graph[s]:
if visited[i] == False:
queue.append(i)
visited[i] = True
#instantiate a class object
g = Graph()
g.addEdge(0,1)
g.addEdge(0,2)
g.addEdge(1,2)
g.addEdge(2,0)
g.addEdge(2,3)
g.addEdge(3,3)
print('[+] BFS traversal from vertex 2')
print(f'{g.graph[0:]}')
g.BFS(2)