Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Check if a Directed Graph Contains an Eulerian Path in C++



The Euler path is a path using which we can visit every edge exactly once in a graph. The same vertex can be used for multiple times. The source and destination nodes in the Euler path are different. If the source and destination node become the same, then the Eulerian path is also an Eulerian cycle. In this article, our task is to check if there exists an Eulerian path in the given directed graph.

Example of Eulerian Path

The figure below displays that an Eulerian path exists in the given directed graph. We can see that the starting and ending nodes are not the same in the Eulerian path.

Eulerian Path

Conditions for Checking Eulerian Path in Directed Graph

The conditions for checking an directed graph if there is any Eulerian path or not, are given below. If any of these conditions fails, the graph has no Eulerian path.

  • Condition 1: The given directed graph should be a connected graph either strongly connected or weekly connected.
  • Condition 2: There must be one single vertex an such as in-degree + 1 = out-degree.
  • Condition 3: There must be one single vertex bn such as in-degree = out-degree + 1.
  • Condition 4: The remaining vertices should have in-degree = out-degree.

Steps to Check Eulerian Path in Directed Graph

The steps given below are used to check if the given directed graph has an Eulerian path or not:

  • We defined the number of nodes and provided the adjacency matrix of the graph.
  • Then we check all the conditions required for the Eulerian path in the graph.
  • First, we check if the graph is connected or not. For this, we have used the Depth-First Search(DFS) algorithm to mark the nodes as visited.
  • In the hasEulerianPath() function, we have defined two counter variables i.e., in and out that keep the count of in-degree and out-degree of the nodes.
  • Using the in and out-degree count of the nodes, we check for the remaining conditions(Condition 2, Condition 3, Condition 4) using the if/else condition and increase the counter start_nodes and end_nodes.
  • If the value of start_nodes and end_nodes is 1, then it satisfies condition 2 (in-degree + 1 = out-degree) and condition 3 (in-degree = out-degree + 1) respectively.
  • If all the conditions hold true, it returns "Eulerian Path Found".

C++ Implementation of Checking Eulerian Path in Directed Graph

The following code implements the above steps to check the directed graph for the Eulerian path.

#include <iostream>
#include <vector>
#include <stack>
#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}
};

// Function to implement 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 graph is connected or not 
bool isConnected() {
    bool visited[NODE] = {false};
    int start = -1;
    
    
    for (int i = 0; i < NODE; i++) {
        for (int j = 0; j < NODE; j++) {
            if (graph[i][j]) {
                start = i;
                break;
            }
        }
        if (start != -1) 
            break;
    }

    if (start == -1) 
        return true; 

    dfs(start, visited, graph);

    // Checking if all nodes are visited or not
    for (int i = 0; i < NODE; i++) {
        for (int j = 0; j < NODE; j++) {
            if ((graph[i][j] || graph[j][i]) && !visited[i])
                return false;
        }
    }

    return true;
}

// Function for checking if graph has Eulerian Path
bool hasEulerianPath() {
    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]++;
            }
        }
    }

    int start_nodes = 0, end_nodes = 0;
    for (int i = 0; i < NODE; i++) {
        if (out[i] - in[i] == 1)    // Condition 2
            start_nodes++;
        else if (in[i] - out[i] == 1)   // Condition 3
            end_nodes++;
        else if (in[i] != out[i])   // Condition 4
            return false;
    }

    if ((start_nodes == 1 && end_nodes == 1) || (start_nodes == 0 && end_nodes == 0))
        return true;

    return false;
}

int main() {
    if (hasEulerianPath())
        cout << "Eulerian Path Found.";
    else
        cout << "No Eulerian Path Found.";
    return 0;
}

The output of the above code is as follows:

Eulerian Path Found.

Complexity to Check Eulerian Path in Directed Graph

  • Time Complexity: The time complexity for checking the Eulerian path in the directed graph is O(v^2).
  • Space Complexity: The space complexity for checking the Eulerian path in the directed graph is O(V^2).
Updated on: 2025-05-28T12:11:45+05:30

477 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements