We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 53f916d + 6c9842c commit 5c6d328Copy full SHA for 5c6d328
Graph Traversal/dfs.py
@@ -0,0 +1,10 @@
1
+def dfs(graph, root, visited = []):
2
+ res = [root]
3
+ visited.append(root)
4
+ for neighbour in graph[root]:
5
+ if neighbour not in visited:
6
+ res.extend(dfs(graph, neighbour, visited))
7
+ return res
8
+if __name__ == '__main__':
9
+ graph = {0: [1,3], 1: [3], 2: [3], 3: [2,4], 4:[]}
10
+ print dfs(graph, 0)
0 commit comments