forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
325deda
commit 3770551
Showing
2 changed files
with
57 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,35 @@ | ||
class Graph: | ||
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 | ||
|
||
def __init__(self, vertex): | ||
self.vertex = vertex | ||
self.graph = [[0] * vertex for i in range(vertex) ] | ||
|
||
def add_edge(self, u, v): | ||
self.graph[u - 1][v - 1] = 1 | ||
self.graph[v - 1][u - 1] = 1 | ||
|
||
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: | ||
if visited[u]== False: | ||
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) | ||
g.bfs(1) | ||
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] | ||
while len(queue)!=0: | ||
x=queue.pop(0) | ||
print(x) | ||
self.visited[x]=1 | ||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,32 @@ | ||
class Graph: | ||
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 | ||
|
||
def __init__(self, vertex): | ||
self.vertex = vertex | ||
self.graph = [[0] * vertex for i in range(vertex) ] | ||
self.visited = [False] * vertex | ||
|
||
def add_edge(self, u, v): | ||
self.graph[u - 1][v - 1] = 1 | ||
self.graph[v - 1][u - 1] = 1 | ||
def show(self): | ||
print self.graph | ||
|
||
for i in self.graph: | ||
for j in i: | ||
print(j, end=' ') | ||
print(' ') | ||
def add_edge(self, i, j): | ||
self.graph[i][j]=1 | ||
self.graph[j][i]=1 | ||
|
||
|
||
def dfs(self, u): | ||
self.visited[u - 1] = True | ||
print('%d visited' % u) | ||
for i in range(1, self.vertex + 1): | ||
if self.graph[u - 1][i - 1] == 1 and self.visited[i - 1] == False: | ||
def dfs(self,s): | ||
self.visited[s]=1 | ||
print(s) | ||
for i in range(0,self.nodes): | ||
if self.visited[i]==0 and self.graph[s][i]==1: | ||
self.dfs(i) | ||
|
||
|
||
|
||
g = Graph(5) | ||
g.add_edge(1,4) | ||
g.add_edge(4,2) | ||
g.add_edge(4,5) | ||
g.add_edge(2,5) | ||
g.add_edge(5,3) | ||
g.dfs(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.dfs(s) |