Skip to content

Commit

Permalink
Merge pull request #113 from Adi2612/master
Browse files Browse the repository at this point in the history
dfs and bfs added in python3
  • Loading branch information
ravivarshney01 authored Oct 3, 2018
2 parents 36055fc + 7f270ec commit 437e569
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions graph/dfs/dfs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Graph():
def __init__(self ,Vert):
self.graph = []
for i in range(Vert):
self.graph.append([])


def addEdge(self,u,v):
self.graph[u].append(v)

def DFSrecur(self,s,visited):
visited[s] = True
print(s , end=" ")
for i in self.graph[s]:
if visited[i] == False:
self.DFSrecur(i,visited)

def DFS(self,s):
visited = [False]*(len(self.graph))
for i in range(len(self.graph)):
if visited[i] == False:
self.DFSrecur(s , visited)



g = Graph(4)
g.addEdge(0, 1)
g.addEdge(0, 2)
g.addEdge(1, 2)
g.addEdge(2, 0)
g.addEdge(2, 3)
g.addEdge(3, 3)

g.DFS(2)

0 comments on commit 437e569

Please sign in to comment.