一、テストデータ
頂点数と辺数を入力してください:7 12入力辺:1条辺:1 2第2条辺:1 4第3条辺:2 4第3条辺:2 4第4条辺:2 5第5条辺:3 1 4第6条辺:3 6第7条辺:4 3 2第8条辺:4 5第9条辺:4 6 8第10条辺:4 7第11条辺:5 6第12条辺:7 6 6開始接点と終了接点:1 6 BFS:14 4 7 6第6 BFS経路:to 1 to 4 to 6 Dijkstra:142 3 5 7 6 Dijkstraパス:to 1 to 4 to 7 to 6 Press any key to continue
二、ソースコード
//BFS
#include
#include
#include
#include
三、アルゴリズムの原理
BFS:初期条件は、出発ノードの距離を0とし、キューに挿入することである.ループ中にキューをポップアップし、1つのキーワードに対応する隣接テーブルを取得し、そのキーワードに対応する頂点の隣接頂点距離が無限でなければ(隣接テーブルに格納されている)、頂点の距離をキーワード頂点の距離に1を加え、距離をリセットした頂点をスタックに割り当てる.
Dijkstraアルゴリズム:初期条件は出発ノードの距離付与値が0であり、すべての頂点の最短距離がIsknowによって識別されFALSEに初期化されるかどうかを決定する.ループでは、距離がまだ決定されていない頂点の中で最も距離が小さい頂点を見つけ、既知(Isknow=TRUE)として識別し、その頂点に隣接するすべての頂点を巡り、距離(Isknow=FALSE)がまだ決定されていない頂点を巡り、新しい距離(pre.dist+weight)を古い距離(current.dist)と比較し、小さい場合は隣接する頂点の距離を更新する.