dfs and bfs inn python

Solutions on MaxInterview for dfs and bfs inn python by the best coders in the world

showing results for - "dfs and bfs inn python"
Hendrik
21 Sep 2020
1def dfs(graph, start):
2    visited, stack = set(), [start]
3    while stack:
4        vertex = stack.pop()
5        if vertex not in visited:
6            visited.add(vertex)
7            stack.extend(graph[vertex] - visited)
8    return visited
9
10dfs(graph, 'A') # {'E', 'D', 'F', 'A', 'C', 'B'}
11