2017-07-18 10:47:48 +00:00
|
|
|
class GRAPH:
|
|
|
|
"""docstring for GRAPH"""
|
|
|
|
def __init__(self, nodes):
|
|
|
|
self.nodes=nodes
|
|
|
|
self.graph=[[0]*nodes for i in range (nodes)]
|
|
|
|
self.visited=[0]*nodes
|
2017-06-22 23:15:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def show(self):
|
2017-07-18 10:47:48 +00:00
|
|
|
print self.graph
|
|
|
|
|
|
|
|
def add_edge(self, i, j):
|
|
|
|
self.graph[i][j]=1
|
|
|
|
self.graph[j][i]=1
|
|
|
|
|
|
|
|
def bfs(self,s):
|
|
|
|
queue=[s]
|
2017-07-25 15:08:54 +00:00
|
|
|
self.visited[s]=1
|
2017-07-18 10:47:48 +00:00
|
|
|
while len(queue)!=0:
|
|
|
|
x=queue.pop(0)
|
|
|
|
print(x)
|
|
|
|
for i in range(0,self.nodes):
|
|
|
|
if self.graph[x][i]==1 and self.visited[i]==0:
|
|
|
|
queue.append(i)
|
|
|
|
self.visited[i]=1
|
|
|
|
|
|
|
|
n=int(input("Enter the number of Nodes : "))
|
|
|
|
g=GRAPH(n)
|
|
|
|
e=int(input("Enter the no of edges : "))
|
|
|
|
print("Enter the edges (u v)")
|
|
|
|
for i in range(0,e):
|
|
|
|
u,v=map(int, raw_input().split())
|
|
|
|
g.add_edge(u,v)
|
|
|
|
s=int(input("Enter the source node :"))
|
|
|
|
g.bfs(s)
|