有向圖中的歐拉回路


尤拉路徑是一條路徑,我們可以透過它精確地訪問每條邊一次。我們可以多次使用相同的頂點。歐拉回路是一種特殊的尤拉路徑。當尤拉路徑的起始頂點也與該路徑的結束頂點相連時,則稱為歐拉回路。

要檢查圖是否為尤拉圖,我們必須檢查兩個條件:

  • 圖必須是連通的。
  • 每個頂點的入度和出度必須相同。

輸入和輸出

Input:
Adjacency matrix of the graph.
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

Output:
Euler Circuit Found.

演算法

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.";
}

輸出

Euler Circuit Found.

更新於: 2020年6月16日

2K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告