Skip to content

Commit dc5e86b

Browse files
Alvin NguyenAlvin Nguyen
Alvin Nguyen
authored and
Alvin Nguyen
committed
Fixed compilation errors, fixes for readability/convention, changed double equals to boolean equality operator 'is'
1 parent 3ecb193 commit dc5e86b

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

data_structures/Graph/Breadth_First_Search.py

+23-21
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
class GRAPH:
22
"""docstring for GRAPH"""
33
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
77

88

99
def show(self):
@@ -23,7 +23,7 @@ def bfs(self,v):
2323
v = queue[0]
2424
for u in range(self.vertex):
2525
if self.graph[v][u] == 1:
26-
if visited[u]== False:
26+
if visited[u] is False:
2727
visited[u] = True
2828
queue.append(u)
2929
print('%d visited' % (u +1))
@@ -41,30 +41,32 @@ def bfs(self,v):
4141
g.add_edge(5,9)
4242
g.add_edge(6,10)
4343
g.bfs(4)
44-
=======
45-
print self.graph
44+
45+
print(self.graph)
4646

4747
def add_edge(self, i, j):
4848
self.graph[i][j]=1
4949
self.graph[j][i]=1
5050

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)
5656
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:
5959
queue.append(i)
60-
self.visited[i]=1
60+
self.visited[i] = 1
6161

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 : "))
6565
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 :"))
7072
g.bfs(s)

0 commit comments

Comments
 (0)