From 7f270ec5e4d4c8267ecec2b3fe835fefe1fd35dd Mon Sep 17 00:00:00 2001 From: Adi2612 Date: Thu, 4 Oct 2018 00:00:35 +0530 Subject: [PATCH] dfs and bfs added in py --- graph/dfs/dfs.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 graph/dfs/dfs.py diff --git a/graph/dfs/dfs.py b/graph/dfs/dfs.py new file mode 100644 index 000000000..aa7fe57f6 --- /dev/null +++ b/graph/dfs/dfs.py @@ -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) \ No newline at end of file