Repository where I mostly put random python scripts.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
753 B

5 years ago
  1. from collections import defaultdict
  2. class Graph:
  3. def __init__(self):
  4. self.graph = defaultdict(list)
  5. def addEdge(self,u,v):
  6. self.graph[u].append(v)
  7. def DFSUtil(self,v,visited):
  8. visited[v]= True
  9. print v,
  10. for i in self.graph[v]:
  11. if visited[i] == False:
  12. self.DFSUtil(i, visited)
  13. def DFS(self,v):
  14. visited = [False]*(len(self.graph))
  15. self.DFSUtil(v,visited)
  16. g = Graph()
  17. edges = input("input the number of edges : ")
  18. print "enter nodes with zero based indexing : "
  19. for i in range(edges):
  20. a, b = map(int, raw_input().split())
  21. g.addEdge(a, b)
  22. check = input("DFS check from node : ")
  23. g.DFS(check)