1
1
class GRAPH :
2
2
"""docstring for GRAPH"""
3
3
def __init__ (self , nodes ):
4
- self .nodes = nodes
5
- self .graph = [[0 ]* nodes for i in range (nodes )]
6
- self .visited = [0 ]* nodes
4
+ self .nodes = nodes
5
+ self .graph = [[0 ]* nodes for i in range (nodes )]
6
+ self .visited = [0 ]* nodes
7
7
8
8
9
9
def show (self ):
@@ -23,7 +23,7 @@ def bfs(self,v):
23
23
v = queue [0 ]
24
24
for u in range (self .vertex ):
25
25
if self .graph [v ][u ] == 1 :
26
- if visited [u ]== False :
26
+ if visited [u ] is False :
27
27
visited [u ] = True
28
28
queue .append (u )
29
29
print ('%d visited' % (u + 1 ))
@@ -41,30 +41,32 @@ def bfs(self,v):
41
41
g .add_edge (5 ,9 )
42
42
g .add_edge (6 ,10 )
43
43
g .bfs (4 )
44
- == == == =
45
- print self .graph
44
+
45
+ print ( self .graph )
46
46
47
47
def add_edge (self , i , j ):
48
48
self .graph [i ][j ]= 1
49
49
self .graph [j ][i ]= 1
50
50
51
- def bfs (self ,s ):
52
- queue = [s ]
53
- self .visited [s ]= 1
54
- while len (queue )!= 0 :
55
- x = queue .pop (0 )
51
+ def bfs (self , s ):
52
+ queue = [s ]
53
+ self .visited [s ] = 1
54
+ while len (queue )!= 0 :
55
+ x = queue .pop (0 )
56
56
print (x )
57
- for i in range (0 ,self .nodes ):
58
- if self .graph [x ][i ]== 1 and self .visited [i ]== 0 :
57
+ for i in range (0 , self .nodes ):
58
+ if self .graph [x ][i ] == 1 and self .visited [i ] == 0 :
59
59
queue .append (i )
60
- self .visited [i ]= 1
60
+ self .visited [i ] = 1
61
61
62
- n = int (input ("Enter the number of Nodes : " ))
63
- g = GRAPH (n )
64
- e = int (input ("Enter the no of edges : " ))
62
+ n = int (input ("Enter the number of Nodes : " ))
63
+ g = GRAPH (n )
64
+ e = int (input ("Enter the no of edges : " ))
65
65
print ("Enter the edges (u v)" )
66
- for i in range (0 ,e ):
67
- u ,v = map (int , raw_input ().split ())
68
- g .add_edge (u ,v )
69
- s = int (input ("Enter the source node :" ))
66
+
67
+ for i in range (0 , e ):
68
+ u ,v = map (int , raw_input ().split ())
69
+ g .add_edge (u , v )
70
+
71
+ s = int (input ("Enter the source node :" ))
70
72
g .bfs (s )
0 commit comments