From 61a00856827ff49ff95e22c48fe35c61df61e9a3 Mon Sep 17 00:00:00 2001 From: Ayush Yadav Date: Sat, 20 Oct 2018 02:08:06 +0530 Subject: [PATCH] add DFS.py --- searching_algo/DFS.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 searching_algo/DFS.py diff --git a/searching_algo/DFS.py b/searching_algo/DFS.py new file mode 100644 index 0000000..61de004 --- /dev/null +++ b/searching_algo/DFS.py @@ -0,0 +1,37 @@ +from collections import defaultdict + +class Graph: + + def __init__(self): + + self.graph = defaultdict(list) + + def addEdge(self,u,v): + self.graph[u].append(v) + + def DFSUtil(self,v,visited): + + visited[v]= True + print v, + + for i in self.graph[v]: + if visited[i] == False: + self.DFSUtil(i, visited) + + + def DFS(self,v): + + visited = [False]*(len(self.graph)) + + self.DFSUtil(v,visited) + + +g = Graph() +edges = input("input the number of edges : ") +print "enter nodes with zero based indexing : " +for i in range(edges): + a, b = map(int, raw_input().split()) + g.addEdge(a, b) + +check = input("DFS check from node : ") +g.DFS(check) \ No newline at end of file