-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBFS.py
50 lines (38 loc) · 964 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue May 2 16:29:13 2017
@author: ska
Breadth First Search
"""
def BFS(s):
visited = [False]*len(g)
traversal =[]
queue = []
queue.append(s)
visited[s] = True
while len(queue) >0:
a = queue.pop(0)
traversal.append(a)
for i in g[a]:
if visited[i]==False:
queue.append(i)
visited[i] = True
return traversal
#==============================================================================
# Graph implementation
#==============================================================================
from collections import defaultdict
g = defaultdict(list)
def addEdge(a,b):
g[a].append(b)
#==============================================================================
# Example Implementation
#==============================================================================
addEdge(0, 1)
addEdge(0, 2)
addEdge(1, 2)
addEdge(2, 0)
addEdge(2, 3)
addEdge(3, 3)
print(BFS(2))