C++程序:检查有向图是否包含欧拉回路


欧拉回路/环路是一条路径;通过它我们可以恰好访问每条边一次。我们可以多次使用相同的顶点。欧拉环路是欧拉路径的一种特殊类型。当欧拉路径的起始顶点也与该路径的结束顶点相连时,则称其为欧拉环路。

要检查图是否为欧拉图,我们必须检查两个条件:

  • 图必须是连通的。

  • 每个顶点的入度和出度必须相同。

输入 - 图的邻接矩阵。

01000
00100
00011
10000
00100

输出 - 找到欧拉回路

算法

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

isEulerCircuit(Graph)

输入 - 给定的图。

输出 - 找到一个欧拉回路时返回True。

Begin
   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
    otherwise return false
End

示例代码

#include<iostream>
#include<vector>
#define NODE 5
using namespace std;
int graph[NODE][NODE] = {{0, 1, 0, 0, 0},
   {0, 0, 1, 0, 0},
   {0, 0, 0, 1, 1},
   {1, 0, 0, 0, 0},
   {0, 0, 1, 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 isEulerCircuit() {
   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;
   }
   if(inward == outward)      //when number inward edges and outward edges
      for each node is same
         return true;
   return false;
}
int main() {
   if(isEulerCircuit())
      cout << "Euler Circuit Found.";
   else
     cout << "There is no Euler Circuit.";
}

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

输出

Euler Circuit Found.

更新于:2019年7月30日

495 次浏览

开启您的职业生涯

完成课程获得认证

开始学习
广告