Skip to content

Commit 5c6d328

Browse files
authored
Merge pull request #430 from itayfry/dfs-python
DFS implementation in python
2 parents 53f916d + 6c9842c commit 5c6d328

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

Graph Traversal/dfs.py

+10
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)