C++程式檢查有向圖是否包含尤拉路徑


尤拉路徑是一條路徑,透過它我們可以精確地訪問每條邊一次。我們可以多次使用相同的頂點。包含歐拉回路的圖在這種情況下也被考慮在內,因為它也具有尤拉路徑。

要檢查有向圖是否具有尤拉路徑,我們必須檢查以下條件:

  • 必須存在一個唯一的頂點an,其中(入度 + 1 = 出度)
  • 必須存在一個唯一的頂點bn,其中(入度 = 出度 + 1)
  • 如果任何這些情況失敗,則圖中沒有尤拉路徑,其餘所有頂點都有(入度 = 出度)。

頂點b具有(入度1,出度2),頂點c具有(入度2,出度1)。對於其餘頂點a、d,具有(入度2,出度2),e具有(入度1,出度1)。

輸入

圖的鄰接矩陣。

00110
10100
00010
01001
10000

輸出

找到尤拉路徑。

演算法

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.

更新於:2019年7月30日

412次瀏覽

開啟您的職業生涯

透過完成課程獲得認證

開始學習
廣告