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