-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfs.py
63 lines (48 loc) · 1011 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
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
57
58
59
60
61
from collections import defaultdict
class Graph:
"""docstring for Graph"""
def __init__(self):
# super(Graph, self).__init__()
# self.arg = arg
self.graph= defaultdict(list)
def addEdge(self,u,v):
self.graph[u].append(v)
def BFS(self,s):
queue=[]
visited=[False]*(len(self.graph))
queue.append(s)
visited[s]=True
bfs=[]
while queue:
v=queue.pop(0)
bfs.append(v)
for i in self.graph[v]:
if visited[i]==False:
queue.append(i)
visited[i]=True
return bfs
def bfsTree(self,root):
queue=deque()
queue.append(root)
ans=[]
while queue:
curSize=len(queue)
temp=[]
for _ in range(curSize):
curRoot=queue.popleft()
temp.append(curRoot.val)
if curRoot.left:
queue.append(curRoot.left)
if curRoot.right:
queue.append(curRoot.right)
ans.append(temp)
return ans
if __name__ == '__main__':
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(g.BFS(2))