Check if an Undirected Graph Contains a 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 undirected graph.

Example of Eulerian Path

The figure below displays an undirected graph and its respective adjacency matrix. As we can see there exists an Eulerian path in the graph and its starting and ending nodes are not same. The path starts from node '0' and ends on node '4'.

Euclerian Path in Undirected Graph

Conditions to Check Eulerian Path in Undirected Graph

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

  • The undirected graph should be a connected graph.
  • There should be exactly zero or two vertex with odd degree in the graph.
  • If there isn't any vertex with odd degree, then the graph has Eulerian path as well as Eulerian circle.
  • If there is exactly 2 vertices with odd degree, then the graph has Eulerian path but not Eulerian circle.

Steps to Check Eulerian Path in Undirected Graph

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

  • We defined the number of nodes and provided the adjacency matrix of the graph. After that, we check all the conditions required for the Eulerian path in the graph.
  • The first condition is to check if the given graph is a connected graph or not. We have used the Depth-First Search(DFS) algorithm to mark the nodes as visited using the dfs() function.
  • In the isEulerianPath() function, we have initialized a counter oddCount that counts how many nodes have odd degree in the graph.
  • We iterate over all the vertices and increase the oddCount counter by 1, each time we find an odd degree vertex.
  • Finally, we check the value of the oddCount counter. If it is 0, it indicates the graph has an Eulerian path as well as an Eulerian cycle.
  • If the value of oddCount is exactly 2, then the graph has an Eulerian path. If both the conditions are satisfied i.e., connected graph and 0 or 2 odd-degree vertices, then the Eulerian path is printed using the printCycle() function.

C++ Implementation of Checking Eulerian Path in Undirected Graph

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

#include <iostream>
#include <vector>
#define NODE 5
using namespace std;

int graph[NODE][NODE] = {{0, 1, 1, 1, 0},
   {1, 0, 1, 0, 0},
   {1, 1, 0, 0, 0},
   {1, 0, 0, 0, 1},
   {0, 0, 0, 1, 0}};

// Function to implement DFS
void dfs(int u, bool visited[]) {
    visited[u] = true;
    for (int v = 0; v < NODE; v++) {
        if (graph[u][v] && !visited[v])
            dfs(v, visited);
    }
}

// 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);

    // 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] && !visited[i])
                return false;
        }
    }
    return true;
}

// Function for checking if graph has Eulerian Path
bool isEulerianPath() {
    if (!isConnected()) 
        return false;

    int oddCount = 0;
    for (int i = 0; i < NODE; i++) {
        int degree = 0;
        for (int j = 0; j < NODE; j++) {
            if (graph[i][j]) 
                degree++;
        }
        if (degree % 2 != 0) 
            oddCount++;
    }

    return (oddCount == 0 || oddCount == 2);
}

// For printing the Eulerian Path
void printCycle(vector<int>& path, int u) {
    for (int v = 0; v < NODE; v++) {
        while (graph[u][v] > 0) {
            graph[u][v]--;
            graph[v][u]--;
            printCycle(path, v);
        }
    }
    path.push_back(u);
}

int main() {
    if (isEulerianPath()) {
        cout << "The given undirected graph has an Eulerian Path.\n";

        vector<int> path;
        int start = 0;

        for (int i = 0; i < NODE; i++) {
            int degree = 0;
            for (int j = 0; j < NODE; j++) {
                if (graph[i][j]) 
                    degree++;
            }
            if (degree % 2 != 0) {
                start = i;
                break;
            }
        }

        printCycle(path, start);

        cout << "Eulerian Path: ";
        for (int i = path.size() - 1; i >= 0; i--) {
            cout << path[i];
            if (i > 0) cout << " -> ";
        }
        cout << endl;
    }
    else
        cout << "The given undirected graph does NOT have an Eulerian Path.\n";

    return 0;
}

The output of the above code is as follows:

The given undirected graph has an Eulerian Path.
Eulerian Path: 0 -> 1 -> 2 -> 0 -> 3 -> 4

Complexity to Check Eulerian Path in Undirected Graph

  • Time Complexity: The time complexity for checking the Eulerian path in the undirected graph is O(v^2).
  • Space Complexity: The space complexity for checking the Eulerian path in the undirected graph is O(v^2).
Updated on: 2025-05-29T19:17:56+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements