-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphs_BFS.py
36 lines (30 loc) · 850 Bytes
/
Graphs_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
graph = []
nodes = None
visited = {}
visit_order = []
queue = []
def BFS(node):
for i in range(0,nodes):
if graph[node][i]==1 and visited[i]==0:
queue.append(i)
visited[i] = 1
print "queue is now, ",queue
while len(queue)!=0:
next = queue.pop(0)
visit_order.append(next)
BFS(next)
nodes = int(raw_input("please enter the no of nodes"))
for i in range(0,nodes):
graph.append([])
print "setting connections for the node ",i
for j in range(0,nodes):
connector = int(raw_input("Please Develop the graph structure"))
graph[i].append(connector)
for k in range(0,nodes):
visited[k] = 0
for k in range(0,nodes):
if visited[k]==0:
visited[k]=1
visit_order.append(k)
BFS(k)
print visit_order