
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a Directed Graph Contains an Eulerian Cycle in C++
The Euler path is a path by which we visit every edge exactly once. We can use the same vertices for multiple times. The Euler Circuit is a special type of Euler path. When the starting vertex of the Euler path is also connected with the ending vertex of that path, then it is called the Euler Cycle.
In this article, our task is to check if the Eulerian cycle exists in the given directed graph or not.
In the above figure, there is a directed graph and its respective adjacency matrix. The Eulerian path respective to the graph is also mentioned. As it can be seen it starts and ends on the same vertex and each edge is visited exactly once.
Condition to Check Eulerian Cycle in Directed Graph
The conditions for checking if a graph has any Eulerian cycle or not is mentioned below:
- The given directed graph should be a strongly connected graph. The strongly connected graph is a directed graph, where, for any pair of vertices u and v in the graph, there exists a directed path from u to v and a directed path from v to u.
- The in-degree and out-degree of each vertex must be equal.
Steps to Check Eulerian Cycle in Directed Graph
We will use the steps given below to check if the given graph has an Eulerian cycle or not:
- At first, we defined the number of nodes and provided the adjacency matrix of the graph.
- Then we check the two required conditions for the Eulerian cycle i.e., each node must have the same value of in and out-degree and the graph must be strongly connected graph.
- To check if the in-degree and out-degree of each vertex are the same or not, we have defined two counters (in and out) in the isEulerCycle() function. If the value of these two counters is equal, then the in and out-degree of each vertex is the same.
- To check the strongly connected graph, we will check if there exists a directed path from u to v and a directed path from v to u.
- For traversing the graph, we have used the DFS approach. Using the Depth-first search, first we traverse the graph from node 0 to other nodes with dfs(0, visited, graph).
- Then, we reverse the graph and use the dfs to traverse the reversed graph using dfs(0, visited, reverseGraph).
- The above two steps mark if the node has been visited or not in the graph and reversed graph. This gives the edges between source and destination nodes during the traversal of the given graph and the reversed graph.
- The function isEulerCycle() returns true if both conditions are satisfied, meaning the graph has an Eulerian cycle.
C++ Implementation to Check Eulerian Cycle in Directed Graph
Here is the code example implementing the above steps for checking the Eulerian cycle in the directed graph.
#include <iostream> #include <vector> #include <stack> #define NODE 5 using namespace std; int graph[NODE][NODE] = { {0, 0, 1, 1, 0}, {1, 0, 0, 0, 0}, {0, 1, 0, 0, 0}, {0, 0, 0, 0, 1}, {1, 0, 0, 0, 0} }; // Function to traverse the nodes using DFS void dfs(int u, bool visited[], int g[NODE][NODE]) { visited[u] = true; for (int v = 0; v < NODE; v++) { if (g[u][v] && !visited[v]) dfs(v, visited, g); } } // Function for checking if the graph is strongly connected bool isConnected() { bool visited[NODE] = {false}; // Using DFS to traverse from node 0 to all dfs(0, visited, graph); for (int i = 0; i < NODE; i++) { if (!visited[i]) return false; } // Reversing the graph int reverseGraph[NODE][NODE] = {0}; for (int i = 0; i < NODE; i++) { for (int j = 0; j < NODE; j++) { reverseGraph[i][j] = graph[j][i]; } } // Using DFS starting from node 0 after reversing the graph for (int i = 0; i < NODE; i++) visited[i] = false; dfs(0, visited, reverseGraph); for (int i = 0; i < NODE; i++) { if (!visited[i]) return false; } return true; } // Function to check Euler Cycle in graph bool isEulerCycle() { if (!isConnected()) return false; vector<int> in(NODE, 0), out(NODE, 0); for (int i = 0; i < NODE; i++) { for (int j = 0; j < NODE; j++) { if (graph[i][j]) { out[i]++; in[j]++; } } } for (int i = 0; i < NODE; i++) { if (in[i] != out[i]) return false; } return true; } int main() { if (isEulerCycle()) cout << "Euler Circuit Found."; else cout << "There is no Euler Circuit."; }
The output of the above code is as follows:
Euler Circuit Found.
Complexity to Check Eulerian Cycle in Directed Graph
- Time Complexity: The time complexity for checking the Eulerian cycle in the directed graph is O(v^2).
- Space Complexity: The space complexity for checking the Eulerian cycle in the directed graph is O(v^2).