次の例のように、幅優先検索のパスをどのように追跡しますか。

keyを検索する場合は11、1から11を結ぶ最短のリストを返します。
[1, 4, 7, 11]次の例のように、幅優先検索のパスをどのように追跡しますか。

keyを検索する場合は11、1から11を結ぶ最短のリストを返します。
[1, 4, 7, 11]回答:
最初にhttp://en.wikipedia.org/wiki/Breadth-first_searchを確認する必要があります。
以下は、パスのキューを表すためにリストのリストを使用した簡単な実装です。
# graph is in adjacent list representation
graph = {
        '1': ['2', '3', '4'],
        '2': ['5', '6'],
        '5': ['9', '10'],
        '4': ['7', '8'],
        '7': ['11', '12']
        }
def bfs(graph, start, end):
    # maintain a queue of paths
    queue = []
    # push the first path into the queue
    queue.append([start])
    while queue:
        # get the first path from the queue
        path = queue.pop(0)
        # get the last node from the path
        node = path[-1]
        # path found
        if node == end:
            return path
        # enumerate all adjacent nodes, construct a new path and push it into the queue
        for adjacent in graph.get(node, []):
            new_path = list(path)
            new_path.append(adjacent)
            queue.append(new_path)
print bfs(graph, '1', '11')
別のアプローチは、各ノードからその親へのマッピングを維持し、隣接ノードを検査するときに、その親を記録することです。検索が完了したら、親マッピングに従ってバックトレースするだけです。
graph = {
        '1': ['2', '3', '4'],
        '2': ['5', '6'],
        '5': ['9', '10'],
        '4': ['7', '8'],
        '7': ['11', '12']
        }
def backtrace(parent, start, end):
    path = [end]
    while path[-1] != start:
        path.append(parent[path[-1]])
    path.reverse()
    return path
def bfs(graph, start, end):
    parent = {}
    queue = []
    queue.append(start)
    while queue:
        node = queue.pop(0)
        if node == end:
            return backtrace(parent, start, end)
        for adjacent in graph.get(node, []):
            if node not in queue :
                parent[adjacent] = node # <<<<< record its parent 
                queue.append(adjacent)
print bfs(graph, '1', '11')
上記のコードは、循環がないという前提に基づいています。
私はチャオの最初の答えがとても気に入りました!ここで欠けているのは、頂点を訪問済みとしてマークすることだけです。
なぜそれをする必要があるのですか?
ノード11から接続されている別のノード番号13があるとしましょう。ここでの目的はノード13を見つけることです。
少し実行すると、キューは次のようになります。
[[1, 2, 6], [1, 3, 10], [1, 4, 7], [1, 4, 8], [1, 2, 5, 9], [1, 2, 5, 10]]末尾にノード番号10の2つのパスがあることに注意してください。
つまり、ノード番号10からのパスが2回チェックされます。この場合、ノード番号10には子がないため、それほど悪くはありません。しかし、それは本当に悪い可能性があります(ここでも、理由なしにそのノードを2回チェックします。)
ノード番号13は含まれていません。これらのパスは、プログラムが最後にノード番号10の2番目のパスに到達する前に戻らないようにします。そして、それを再チェックします。
足りないのは、訪れたノードをマークし、再度チェックしないようにするためのセットです。
これは、変更後のqiaoのコードです。
graph = {
    1: [2, 3, 4],
    2: [5, 6],
    3: [10],
    4: [7, 8],
    5: [9, 10],
    7: [11, 12],
    11: [13]
}
def bfs(graph_to_search, start, end):
    queue = [[start]]
    visited = set()
    while queue:
        # Gets the first path in the queue
        path = queue.pop(0)
        # Gets the last node in the path
        vertex = path[-1]
        # Checks if we got to the end
        if vertex == end:
            return path
        # We check if the current node is already in the visited nodes set in order not to recheck it
        elif vertex not in visited:
            # enumerate all adjacent nodes, construct a new path and push it into the queue
            for current_neighbour in graph_to_search.get(vertex, []):
                new_path = list(path)
                new_path.append(current_neighbour)
                queue.append(new_path)
            # Mark the vertex as visited
            visited.add(vertex)
print bfs(graph, 1, 13)
プログラムの出力は次のようになります。
[1, 4, 7, 11, 13]不必要なことをせずに再チェック..
collections.dequeために使用すると便利な場合があります。また、後世のために、DFSを実行したい場合は、変数を実際にのように設定します。queueO(n)path = queue.pop()queuestack
                    とても簡単なコード。ノードを検出するたびにパスを追加し続けます。
graph = {
         'A': set(['B', 'C']),
         'B': set(['A', 'D', 'E']),
         'C': set(['A', 'F']),
         'D': set(['B']),
         'E': set(['B', 'F']),
         'F': set(['C', 'E'])
         }
def retunShortestPath(graph, start, end):
    queue = [(start,[start])]
    visited = set()
    while queue:
        vertex, path = queue.pop(0)
        visited.add(vertex)
        for node in graph[vertex]:
            if node == end:
                return path + [end]
            else:
                if node not in visited:
                    visited.add(node)
                    queue.append((node, path + [node]))
私はこれを楽しみのためにコード化しようと思いました:
graph = {
        '1': ['2', '3', '4'],
        '2': ['5', '6'],
        '5': ['9', '10'],
        '4': ['7', '8'],
        '7': ['11', '12']
        }
def bfs(graph, forefront, end):
    # assumes no cycles
    next_forefront = [(node, path + ',' + node) for i, path in forefront if i in graph for node in graph[i]]
    for node,path in next_forefront:
        if node==end:
            return path
    else:
        return bfs(graph,next_forefront,end)
print bfs(graph,[('1','1')],'11')
# >>>
# 1, 4, 7, 11
サイクルが必要な場合は、これを追加できます。
for i, j in for_front: # allow cycles, add this code
    if i in graph:
        del graph[i]
@Qiaoの最初の回答と@Orの追加の両方が好きです。処理を少し減らすために、Orの答えに追加したいと思います。
@Orの回答では、訪問したノードを追跡することは素晴らしいことです。また、プログラムを現在よりも早く終了させることもできます。forループのある時点ではであるcurrent_neighbour必要があり、endそれが発生すると最短経路が見つかり、プログラムが戻ることができます。
メソッドを次のように変更し、forループに細心の注意を払います
graph = {
1: [2, 3, 4],
2: [5, 6],
3: [10],
4: [7, 8],
5: [9, 10],
7: [11, 12],
11: [13]
}
    def bfs(graph_to_search, start, end):
        queue = [[start]]
        visited = set()
    while queue:
        # Gets the first path in the queue
        path = queue.pop(0)
        # Gets the last node in the path
        vertex = path[-1]
        # Checks if we got to the end
        if vertex == end:
            return path
        # We check if the current node is already in the visited nodes set in order not to recheck it
        elif vertex not in visited:
            # enumerate all adjacent nodes, construct a new path and push it into the queue
            for current_neighbour in graph_to_search.get(vertex, []):
                new_path = list(path)
                new_path.append(current_neighbour)
                queue.append(new_path)
                #No need to visit other neighbour. Return at once
                if current_neighbour == end
                    return new_path;
            # Mark the vertex as visited
            visited.add(vertex)
print bfs(graph, 1, 13)
出力と他のすべては同じになります。ただし、コードの処理時間は短くなります。これは、大きなグラフで特に役立ちます。これが将来の誰かの助けになることを願っています。