Computer >> 컴퓨터 >  >> 프로그램 작성 >> C++

유향 그래프에 오일러 주기가 포함되어 있는지 확인하는 C++ 프로그램

<시간/>

오일러 사이클/회로는 경로입니다. 이를 통해 모든 가장자리를 정확히 한 번 방문할 수 있습니다. 같은 정점을 여러 번 사용할 수 있습니다. 오일러 회로는 특수한 유형의 오일러 경로입니다. 오일러 경로의 시작 꼭짓점과 해당 경로의 끝 꼭짓점이 연결되어 있는 경우 이를 오일러 회로라고 합니다.

유향 그래프에 오일러 주기가 포함되어 있는지 확인하는 C++ 프로그램

그래프가 오일러인지 아닌지 확인하려면 두 가지 조건을 확인해야 합니다. -

  • 그래프가 연결되어 있어야 합니다.

  • 각 꼭짓점의 내차수와 외차수는 같아야 합니다.

입력 − 그래프의 인접 행렬.

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

출력 - 오일러 회로 발견

알고리즘

traverse(u, 방문)

입력 − 시작 노드 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

연결됨(그래프)

입력 - 그래프.

출력 − 그래프가 연결되어 있으면 참입니다.

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(그래프)

입력 - 주어진 그래프.

출력 − 하나의 오일러 회로가 발견되면 참입니다.

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.