mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-24 13:31:07 +00:00
Merge pull request #154 from alveeno/master
Fixed compilation errors, fixes for readability/convention, changed d…
This commit is contained in:
commit
48dc64fbff
|
@ -8,7 +8,7 @@ class Node:
|
||||||
def __init__(self, label):
|
def __init__(self, label):
|
||||||
self.label = label
|
self.label = label
|
||||||
self.left = None
|
self.left = None
|
||||||
self.rigt = None
|
self.right = None
|
||||||
|
|
||||||
def getLabel(self):
|
def getLabel(self):
|
||||||
return self.label
|
return self.label
|
||||||
|
@ -23,10 +23,10 @@ class Node:
|
||||||
self.left = left
|
self.left = left
|
||||||
|
|
||||||
def getRight(self):
|
def getRight(self):
|
||||||
return self.rigt
|
return self.right
|
||||||
|
|
||||||
def setRight(self, right):
|
def setRight(self, right):
|
||||||
self.rigt = right
|
self.right = right
|
||||||
|
|
||||||
|
|
||||||
class BinarySearchTree:
|
class BinarySearchTree:
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
class GRAPH:
|
class GRAPH:
|
||||||
"""docstring for GRAPH"""
|
"""docstring for GRAPH"""
|
||||||
def __init__(self, nodes):
|
def __init__(self, nodes):
|
||||||
self.nodes=nodes
|
self.nodes = nodes
|
||||||
self.graph=[[0]*nodes for i in range (nodes)]
|
self.graph = [[0]*nodes for i in range (nodes)]
|
||||||
self.visited=[0]*nodes
|
self.visited = [0]*nodes
|
||||||
|
|
||||||
|
|
||||||
def show(self):
|
def show(self):
|
||||||
|
@ -23,7 +23,7 @@ class GRAPH:
|
||||||
v = queue[0]
|
v = queue[0]
|
||||||
for u in range(self.vertex):
|
for u in range(self.vertex):
|
||||||
if self.graph[v][u] == 1:
|
if self.graph[v][u] == 1:
|
||||||
if visited[u]== False:
|
if visited[u] is False:
|
||||||
visited[u] = True
|
visited[u] = True
|
||||||
queue.append(u)
|
queue.append(u)
|
||||||
print('%d visited' % (u +1))
|
print('%d visited' % (u +1))
|
||||||
|
@ -41,30 +41,32 @@ g.add_edge(4,8)
|
||||||
g.add_edge(5,9)
|
g.add_edge(5,9)
|
||||||
g.add_edge(6,10)
|
g.add_edge(6,10)
|
||||||
g.bfs(4)
|
g.bfs(4)
|
||||||
=======
|
|
||||||
print self.graph
|
print(self.graph)
|
||||||
|
|
||||||
def add_edge(self, i, j):
|
def add_edge(self, i, j):
|
||||||
self.graph[i][j]=1
|
self.graph[i][j]=1
|
||||||
self.graph[j][i]=1
|
self.graph[j][i]=1
|
||||||
|
|
||||||
def bfs(self,s):
|
def bfs(self, s):
|
||||||
queue=[s]
|
queue = [s]
|
||||||
self.visited[s]=1
|
self.visited[s] = 1
|
||||||
while len(queue)!=0:
|
while len(queue)!= 0:
|
||||||
x=queue.pop(0)
|
x = queue.pop(0)
|
||||||
print(x)
|
print(x)
|
||||||
for i in range(0,self.nodes):
|
for i in range(0, self.nodes):
|
||||||
if self.graph[x][i]==1 and self.visited[i]==0:
|
if self.graph[x][i] == 1 and self.visited[i] == 0:
|
||||||
queue.append(i)
|
queue.append(i)
|
||||||
self.visited[i]=1
|
self.visited[i] = 1
|
||||||
|
|
||||||
n=int(input("Enter the number of Nodes : "))
|
n = int(input("Enter the number of Nodes : "))
|
||||||
g=GRAPH(n)
|
g = GRAPH(n)
|
||||||
e=int(input("Enter the no of edges : "))
|
e = int(input("Enter the no of edges : "))
|
||||||
print("Enter the edges (u v)")
|
print("Enter the edges (u v)")
|
||||||
for i in range(0,e):
|
|
||||||
u,v=map(int, raw_input().split())
|
for i in range(0, e):
|
||||||
g.add_edge(u,v)
|
u ,v = map(int, raw_input().split())
|
||||||
s=int(input("Enter the source node :"))
|
g.add_edge(u, v)
|
||||||
|
|
||||||
|
s = int(input("Enter the source node :"))
|
||||||
g.bfs(s)
|
g.bfs(s)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user