有向非巡回グラフの最短経路
より短い距離を検出するには、負の重みを持つグラフにベルマンフォードのような別のアルゴリズムを使用できます。正の重みには、ダイクストラのアルゴリズムも役立ちます。ここでは、有向非巡回グラフについて、トポロジカルソート手法を使用して複雑さを軽減します。
入力と出力
Input: The cost matrix of the graph. 0 5 3 -∞ -∞ -∞ -∞ 0 2 6 -∞ -∞ -∞ -∞ 0 7 4 2 -∞ -∞ -∞ 0 -1 1 -∞ -∞ -∞ -∞ 0 -2 -∞ -∞ -∞ -∞ -∞ 0 Output: Shortest Distance from Source Vertex 1 Infinity 0 2 6 5 3
アルゴリズム
topoSort(u、visited、stack)
入力 :開始ノードu、追跡するために訪問したリスト、スタック。
出力: トポロジ的な方法でノードを並べ替えます。
Begin mark u as visited for all vertex v, which is connected with u, do if v is not visited, then topoSort(v, visited, stack) done push u into the stack End
shortestPath(start)
入力- 開始ノード。
出力- 開始ノードからのすべての頂点の最短距離のリスト。
Begin initially make all nodes as unvisited for each node i, in the graph, do if i is not visited, then topoSort(i, visited, stack) done make distance of all vertices as ∞ dist[start] := 0 while stack is not empty, do pop stack item and take into nextVert if dist[nextVert] ≠∞, then for each vertices v, which is adjacent with nextVert, do if cost[nextVert, v] ≠∞, then if dist[v] > dist[nectVert] + cost[nextVert, v], then dist[v] := dist[nectVert] + cost[nextVert, v] done done for all vertices i in the graph, do if dist[i] = ∞, then display Infinity else display dist[i] done Endを表示します。
例
#include<iostream>
#include<stack>
#define NODE 6
#define INF 9999
using namespace std;
int cost[NODE][NODE] = {
{0, 5, 3, INF, INF, INF},
{INF, 0, 2, 6, INF, INF},
{INF, INF, 0, 7, 4, 2},
{INF, INF, INF, 0, -1, 1},
{INF, INF, INF, INF, 0, -2},
{INF, INF, INF, INF, INF, 0}
};
void topoSort(int u, bool visited[], stack<int>&stk) {
visited[u] = true; //set as the node v is visited
for(int v = 0; v<NODE; v++) {
if(cost[u][v]) { //for allvertices v adjacent to u
if(!visited[v])
topoSort(v, visited, stk);
}
}
stk.push(u); //push starting vertex into the stack
}
void shortestPath(int start) {
stack<int> stk;
int dist[NODE];
bool vis[NODE];
for(int i = 0; i<NODE;i++)
vis[i] = false; // make all nodes as unvisited at first
for(int i = 0; i<NODE; i++) //perform topological sort for vertices
if(!vis[i])
topoSort(i, vis, stk);
for(int i = 0; i<NODE; i++)
dist[i] = INF; //initially all distances are infinity
dist[start] = 0; //distance for start vertex is 0
while(!stk.empty()) { //when stack contains element, process in topological order
int nextVert = stk.top(); stk.pop();
if(dist[nextVert] != INF) {
for(int v = 0; v<NODE; v++) {
if(cost[nextVert][v] && cost[nextVert][v] != INF){ if(dist[v] > dist[nextVert] +cost[nextVert][v])dist[v] = dist[nextVert] + cost[nextVert][v];
}
}
}
for(int i = 0; i<NODE; i++)
(dist[i] == INF)?cout << "Infinity ":cout << dist[i]<<" ";
}
main() {
int start = 1;
cout << "Shortest Distance From Source Vertex "<<start<<endl;
shortestPath(start);
} 出力
Shortest Distance From Source Vertex 1 Infinity 0 2 6 5 3
-
有向グラフにオイラーパスが含まれているかどうかを確認するC++プログラム
オイラーパスはパスです。これにより、すべてのエッジを1回だけ訪問できます。同じ頂点を複数回使用できます。この場合、オイラー経路もあるため、オイラー回路を含む1つのグラフも考慮されます。 有向グラフにオイラーパスがあるかどうかを確認するには、これらの条件を確認する必要があります- 単一の頂点anが1つ存在する必要があります ここで(in-degree + 1 =out_degree) 単一の頂点bnが1つ存在する必要があります ここで(in-degree =out_degree + 1) これらのケースのいずれかが失敗した場合、すべての頂点に(in-degree =out_degree)RE
-
グラフを通る最短経路を計算するダイクストラのアルゴリズム
定義 ダイクストラのアルゴリズムは、ソースノードと呼ばれる特定のノードから連結グラフ内の他のすべてのノードへの最短経路を見つけます。ソースノードをルートとする最短パスツリーを生成します。ルーティングコストを最小限に抑えることを目的として、最適なルートを生成するためにコンピュータネットワークで広く使用されています。 ダイクストラのアルゴリズム 入力-ネットワークを表すグラフ。およびソースノード、s 出力-ルートノードとしてsを使用した最短パスツリーspt[]。 初期化- 距離の配列dist[] サイズの|V | (ノードの数)、ここで dist [s] = 0 およびdis