📜  dfs and bfs inn python 代码示例

📅  最后修改于: 2022-03-11 14:46:11.282000             🧑  作者: Mango

代码示例1
def dfs(graph, start):
    visited, stack = set(), [start]
    while stack:
        vertex = stack.pop()
        if vertex not in visited:
            visited.add(vertex)
            stack.extend(graph[vertex] - visited)
    return visited

dfs(graph, 'A') # {'E', 'D', 'F', 'A', 'C', 'B'}