有向グラフにオイラーパスが含まれているかどうかを確認するC++プログラム
オイラーパスはパスです。これにより、すべてのエッジを1回だけ訪問できます。同じ頂点を複数回使用できます。この場合、オイラー経路もあるため、オイラー回路を含む1つのグラフも考慮されます。
有向グラフにオイラーパスがあるかどうかを確認するには、これらの条件を確認する必要があります-
- 単一の頂点anが1つ存在する必要があります ここで(in-degree + 1 =out_degree)
- 単一の頂点bnが1つ存在する必要があります ここで(in-degree =out_degree + 1)
- これらのケースのいずれかが失敗した場合、すべての頂点に(in-degree =out_degree)RESTがあり、グラフにはオイラーパスがありません。
頂点bには(in-degree 1、out-degree 2)、頂点cには(in-degree 2、out-degree 1)があります。そして、残りの頂点a、dは(in-degree 2、out-degree 2)、eは(in-degree 1、out-degree 1)を持っています。
入力
グラフの隣接行列。
| 0 | 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 | 0 |
| 0 | 0 | 0 | 1 | 0 |
| 0 | 1 | 0 | 0 | 1 |
| 1 | 0 | 0 | 0 | 0 |
出力
オイラーパスが見つかりました。
アルゴリズム
traverse(u、visited)
入力開始ノードuと訪問済みノードを入力して、どのノードが訪問されたかをマークします。
出力接続されているすべての頂点をトラバースします。
Begin mark u as visited for all vertex v, if it is adjacent with u, do if v is not visited, then traverse(v, visited) done End
isConnected(graph)
入力:グラフ。
出力:グラフが接続されている場合はTrue。
Begin define visited array for all vertices u in the graph, do make all nodes unvisited traverse(u, visited) if any unvisited node is still remaining, then return false done return true End
hasEulerPath(Graph)
指定されたグラフを入力します。
1つのオイラー回路が見つかったときにTrueを出力します。
Begin an := 0 bn := 0 if isConnected() is false, then return false define list for inward and outward edge count for each node for all vertex i in the graph, do sum := 0 for all vertex j which are connected with i, do inward edges for vertex i increased increase sum done number of outward of vertex i is sum done if inward list and outward list are same, then return true for all vertex i in the vertex set V, do if inward[i] ≠ outward[i], then if inward[i] + 1 = outward[i], then an := an + 1 else if inward[i] = outward[i] + 1, then bn := bn + 1 done if an and bn both are 1, then return true otherwise return false End
サンプルコード
#include<iostream>
#include<vector>
#define NODE 5
using namespace std;
int graph[NODE][NODE] = {{0, 0, 1, 1, 0},
{1, 0, 1, 0, 0},
{0, 0, 0, 1, 0},
{0, 1, 0, 0, 1},
{1, 0, 0, 0, 0}};
void traverse(int u, bool visited[]) {
visited[u] = true; //mark v as visited
for(int v = 0; v<NODE; v++) {
if(graph[u][v]) {
if(!visited[v])
traverse(v, visited);
}
}
}
bool isConnected() {
bool *vis = new bool[NODE];
//for all vertex u as start point, check whether all nodes are visible or not
for(int u; u < NODE; u++) {
for(int i = 0; i<NODE; i++)
vis[i] = false; //initialize as no node is visited
traverse(u, vis);
for(int i = 0; i<NODE; i++) {
if(!vis[i]) //if there is a node, not visited by traversal, graph is not connected
return false;
}
}
return true;
}
bool hasEulerPath() {
int an = 0, bn = 0;
if(isConnected() == false){ //when graph is not connected
return false;
}
vector<int> inward(NODE, 0), outward(NODE, 0);
for(int i = 0; i<NODE; i++) {
int sum = 0;
for(int j = 0; j<NODE; j++) {
if(graph[i][j]) {
inward[j]++; //increase inward edge for destination vertex
sum++; //how many outward edge
}
}
outward[i] = sum;
}
//check the condition for Euler paths
if(inward == outward) //when number inward edges and outward edges for each node is same
return true; //Euler Circuit, it has Euler path
for(int i = 0; i<NODE; i++) {
if(inward[i] != outward[i]) {
if((inward[i] + 1 == outward[i])) {
an++;
} else if((inward[i] == outward[i] + 1)) {
bn++;
}
}
}
if(an == 1 && bn == 1) { //if there is only an, and bn, then this has euler path
return true;
}
return false;
}
int main() {
if(hasEulerPath())
cout << "Euler Path Found.";
else
cout << "There is no Euler Circuit.";
} 出力
Euler Path Found.
-
グラフが強く接続されているかどうかをチェックするC++プログラム
有向グラフでは、1つのコンポーネントの頂点の各ペアの間にパスがある場合、コンポーネントは強く接続されていると言われます。 このアルゴリズムを解決するには、まず、DFSアルゴリズムを使用して各頂点の終了時間を取得し、次に転置されたグラフの終了時間を検索します。次に、頂点をトポロジカルソートの降順で並べ替えます。 入力 :グラフの隣接行列。 0 0 1 1 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 出力 :以下は、与え
-
DFSを使用して有向グラフの接続性をチェックするC++プログラム
グラフの接続性を確認するために、トラバーサルアルゴリズムを使用してすべてのノードをトラバースしようとします。トラバーサルの完了後、アクセスされていないノードがある場合、グラフは接続されていません。 有向グラフの場合、接続を確認するためにすべてのノードからトラバースを開始します。 1つのエッジに外向きのエッジのみがあり、内向きのエッジがない場合があるため、他の開始ノードからノードにアクセスできなくなります。 この場合、トラバーサルアルゴリズムは再帰的なDFSトラバーサルです。 入力 :グラフの隣接行列 0 1 0 0 0 0 0 1 0