C++程序检查有向图是否包含欧拉路径
欧拉路径是一条路径,通过它我们可以精确地访问每条边一次。我们可以多次使用相同的顶点。包含欧拉回路的图在这种情况下也被考虑在内,因为它也具有欧拉路径。
要检查有向图是否具有欧拉路径,我们必须检查以下条件:
- 必须存在一个唯一的顶点an,其中(入度 + 1 = 出度)
- 必须存在一个唯一的顶点bn,其中(入度 = 出度 + 1)
- 如果任何这些情况失败,则图中没有欧拉路径,其余所有顶点都有(入度 = 出度)。

顶点b具有(入度1,出度2),顶点c具有(入度2,出度1)。对于其余顶点a、d,具有(入度2,出度2),e具有(入度1,出度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)
输入:给定的图。
输出:找到一个欧拉回路时返回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.
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP