BFS模板

visited = set()

def BFS(graph, start, end):

    queue = []
    queue.append(start)
    visited.add(start)

    while queue:
        node = queue.pop(0)
        visited.add(node)

        process(node)
        nodes = generate_related_nodes(node)
        queue.append(nodes)

你可能感兴趣的:(BFS模板)