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