をキューから取り出すときに私はBFSのための擬似コードは、このようなかなりのものです多くのサイトで見つけBFSに訪れたとしてノードをマーク:グラフ</p> <p>上BFSトラバーサルについては、単に迅速かつ愚かな質問
BFS (Graph, root):
create empty set S
create empty queue Q
add root to S //mark as visited here
Q.enqueue(root)
while Q is not empty:
current = Q.dequeue()
if current is the goal:
return current
for each node n that is adjacent to current:
if n is not in S:
add n to S //mark as visited here
Q.enqueue(n)
私はちょうど与えられたノードをマークアップするほうが、後のアプローチでは余分なステップを書く必要があるため、ノードをマークするのではなく、ノードをマークした方が少し簡単です。大きなことではないことは分かっていますが、2つではなく1つの場所でノードを訪問したとマークする方が理にかなっています。より簡潔で、覚えやすく、学習することさえ容易です。
修正版は、ちょうどこのようになります:
BFS (Graph, root):
create empty set S
create empty queue Q
Q.enqueue(root)
while Q is not empty:
current = Q.dequeue()
add current to s //just add this and remove the other 2 lines
if current is the goal:
return current
for each node n that is adjacent to current:
if n is not in S:
Q.enqueue(n)
私はちょうどない、私の知る限り、これがすべてではトラバーサルの動作を変更するべきではありません知っているように、この変更が正しいかどうかを知りたいですそれ?