Minimum edges required to add to make Euler Circuit

Given a undirected graph of n nodes and m edges. The task is to find minimum edges required to make Euler Circuit in the given graph.


Input : n = 3, 
        m = 2
        Edges[] = {{1, 2}, {2, 3}}
Output : 1

By connecting 1 to 3, we can create a Euler Circuit.

For a Euler Circuit to exist in the graph we require that every node should have even degree because then there exists an edge that can be used to exit the node after entering it.

Now, there can be two case:
1. There is one connected component in the graph
In this case, if all the nodes in the graph is of even degree then we say that the graph already have a Euler Circuit and we don’t need to add any edge in it. But if there is any node with odd degree we need to add edges.
There can be even number of odd degree vertices in the graph. This can be easily proved by the fact that the sum of degrees from the even degrees node and degrees from odd degrees node should match the total degrees that is always even as every edge contributes two to this sum. Now, if we pair up random odd degree nodes in the graph and add an edge between them we can make all nodes to have even degree and thus make an Euler Circuit exist.

2. There are disconnected components in the graph
We first mark component as odd and even. Odd components are those which have at least one odd degree node in them. Take all the even components and select a random vertex from every component and line them up linearly. Now we add an edge between adjacent vertices. So we have connected the even components and made an equivalent odd components that has two nodes with odd degree.
Now to deal with odd components i.e components with at least one odd degree node. We can connect all these odd components using edges whose number is equal to the number of disconnected components. This can be done by placing the components in the cyclic order and picking two odd degree nodes from every component and using these to connect to the components on either side. Now we have a single connected component for which we have discussed.

Below is C++ implementation of this approach:





// C++ program to find minimum edge required
// to make Euler Circuit
#include <bits/stdc++.h>
using namespace std;
// Depth-First Search to find a connected
// component
void dfs(vector<int> g[], int vis[], int odd[],
                    int deg[],  int comp, int v)
    vis[v] = 1;
    if (deg[v]%2 == 1)
    for (int u : g[v])
        if (vis[u] == 0)
            dfs(g, vis, odd, deg, comp, u);
// Return minimum edge required to make Euler
// Circuit
int minEdge(int n, int m, int s[], int d[])
    // g : to store adjacency list
    //     representation of graph.
    // e : to store list of even degree vertices
    // o : to store list of odd degree vertices
    vector<int> g[n+1], e, o;
    int deg[n+1];  // Degrees of vertices
    int vis[n+1];  // To store visited in DFS
    int odd[n+1];  // Number of odd nodes in components
    memset(deg, 0, sizeof(deg));
    memset(vis, 0, sizeof(vis));
    memset(odd, 0, sizeof(odd));
    for (int i = 0; i < m; i++)
    // 'ans' is result and 'comp' is component id
    int ans = 0, comp = 0;
    for (int i = 1; i <= n; i++)
        if (vis[i]==0)
            dfs(g, vis, odd, deg, comp, i);
            // Checking if connected component
            // is odd.
            if (odd[comp] == 0)
            // Checking if connected component
            // is even.
    // If whole graph is a single connected
    // component with even degree.
    if (o.size() == 0 && e.size() == 1)
        return 0;
    // If all connected component is even
    if (o.size() == 0)
        return e.size();
    // If graph have atleast one even connected
    // component
    if (e.size() != 0)
        ans += e.size();
    // For all the odd connected component.
    for (int i : o)
        ans += odd[i]/2;
    return ans;
// Driven Program
int main()
    int n = 3, m = 2;
    int source[] = { 1, 2 };
    int destination[] = { 2, 3 };
    cout << minEdge(n, m, source, destination) << endl;
    return 0;




This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :


Please write to us at to report any issue with the above content.