-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphBFS.py
More file actions
31 lines (24 loc) · 766 Bytes
/
graphBFS.py
File metadata and controls
31 lines (24 loc) · 766 Bytes
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
import networkx as nx
import pickle
import queue
graph = pickle.load(open('graph50knx.pkl','rb'))
print("graph50knx.pkl loaded")
nodes = pickle.load(open('nodes.pkl','rb'))
print("nodes.pkl loaded")
def traversal(source):
N = len(nodes)
visited = [False]*N
distance = [None]*N
que = queue.Queue()
que.put(source)
distance[source] = 0
visited[source] = True
while(not que.empty()):
temp = que.get()
data = graph.neighbors(temp)
for i in data:
if(visited[i] == False):
que.put(i)
visited[i] = True
distance[i] = distance[temp]+1
return [i for i, x in enumerate(distance) if x==1], [i for i, x in enumerate(distance) if x==2]