-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphBFS.py
More file actions
56 lines (39 loc) · 1.18 KB
/
Copy pathGraphBFS.py
File metadata and controls
56 lines (39 loc) · 1.18 KB
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
49
50
51
52
53
54
55
56
#https://www.geeksforgeeks.org/breadth-first-traversal-for-a-graph/
#Things learnt
# Difference between a graph and tree is:-
# graph is bidirectional whereas tree isn't
# Tree cannot have a cyclic node where as graph can have
#Why defaultdict ?
#https://shirishweb.wordpress.com/2017/05/06/python-defaultdict-versus-dict-get/
# defaultdict(<class 'list'>)
# {1: [2, 3], 2: [4, 5], 3: [5], 4: [6], 5: [6]}
# Assumption in this problem nodes start from 0 and incrementally adds to 6, also last node is cyclic
from collections import defaultdict
class Graph:
def __init__(self):
self.graph = defaultdict(list)
def addEdge(self,u,v):
self.graph[u].append(v)
def BFS(self,s):
visited = [False]*len(self.graph)
queue = [s]
#queue.append(s)
visited[s] = True
while queue:
s = queue.pop(0)
print(s)
for i in self.graph[s]:
if visited[i] is False:
queue.append(i)
visited[i] = True
g = Graph()
g.addEdge(0,1)
g.addEdge(1,2)
g.addEdge(1,3)
g.addEdge(2,4)
g.addEdge(2,5)
g.addEdge(3,5)
g.addEdge(4,6)
g.addEdge(5,6)
g.addEdge(6,6)
g.BFS(0)