2017-07-18 10:47:48 +00:00
|
|
|
class GRAPH:
|
|
|
|
"""docstring for GRAPH"""
|
|
|
|
def __init__(self, nodes):
|
2017-10-10 00:00:37 +00:00
|
|
|
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):
|
|
|
|
|
|
|
|
for i in self.graph:
|
|
|
|
for j in i:
|
|
|
|
print(j, end=' ')
|
|
|
|
print(' ')
|
|
|
|
def bfs(self,v):
|
|
|
|
|
|
|
|
visited = [False]*self.vertex
|
|
|
|
visited[v - 1] = True
|
|
|
|
print('%d visited' % (v))
|
|
|
|
|
|
|
|
queue = [v - 1]
|
|
|
|
while len(queue) > 0:
|
|
|
|
v = queue[0]
|
|
|
|
for u in range(self.vertex):
|
|
|
|
if self.graph[v][u] == 1:
|
2017-10-10 00:00:37 +00:00
|
|
|
if visited[u] is False:
|
2017-06-22 23:15:03 +00:00
|
|
|
visited[u] = True
|
|
|
|
queue.append(u)
|
|
|
|
print('%d visited' % (u +1))
|
|
|
|
queue.pop(0)
|
|
|
|
|
|
|
|
g = Graph(10)
|
|
|
|
|
|
|
|
g.add_edge(1,2)
|
|
|
|
g.add_edge(1,3)
|
|
|
|
g.add_edge(1,4)
|
|
|
|
g.add_edge(2,5)
|
|
|
|
g.add_edge(3,6)
|
|
|
|
g.add_edge(3,7)
|
|
|
|
g.add_edge(4,8)
|
|
|
|
g.add_edge(5,9)
|
|
|
|
g.add_edge(6,10)
|
2017-08-27 11:20:28 +00:00
|
|
|
g.bfs(4)
|
2017-10-10 00:00:37 +00:00
|
|
|
|
|
|
|
print(self.graph)
|
2017-07-18 10:47:48 +00:00
|
|
|
|
|
|
|
def add_edge(self, i, j):
|
|
|
|
self.graph[i][j]=1
|
|
|
|
self.graph[j][i]=1
|
|
|
|
|
2017-10-10 00:00:37 +00:00
|
|
|
def bfs(self, s):
|
|
|
|
queue = [s]
|
|
|
|
self.visited[s] = 1
|
|
|
|
while len(queue)!= 0:
|
|
|
|
x = queue.pop(0)
|
2017-07-18 10:47:48 +00:00
|
|
|
print(x)
|
2017-10-10 00:00:37 +00:00
|
|
|
for i in range(0, self.nodes):
|
|
|
|
if self.graph[x][i] == 1 and self.visited[i] == 0:
|
2017-07-18 10:47:48 +00:00
|
|
|
queue.append(i)
|
2017-10-10 00:00:37 +00:00
|
|
|
self.visited[i] = 1
|
2017-07-18 10:47:48 +00:00
|
|
|
|
2017-10-10 00:00:37 +00:00
|
|
|
n = int(input("Enter the number of Nodes : "))
|
|
|
|
g = GRAPH(n)
|
|
|
|
e = int(input("Enter the no of edges : "))
|
2017-07-18 10:47:48 +00:00
|
|
|
print("Enter the edges (u v)")
|
2017-10-10 00:00:37 +00:00
|
|
|
|
|
|
|
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 :"))
|
2017-07-18 10:47:48 +00:00
|
|
|
g.bfs(s)
|