Browse Source

add DFS.py

pull/13/head
Ayush Yadav 5 years ago
committed by GitHub
parent
commit
61a0085682
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 37 additions and 0 deletions
  1. +37
    -0
      searching_algo/DFS.py

+ 37
- 0
searching_algo/DFS.py View File

@ -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)

Loading…
Cancel
Save