Minimum edges required to add to make Euler Circuit in Python

An Euler Circuit is a path in a graph that visits every edge exactly once and returns to the starting vertex. For an undirected graph to have an Euler Circuit, all vertices must have even degree and the graph must be connected. This article shows how to find the minimum edges needed to make such a circuit possible.

Theory Behind Euler Circuits

For an undirected graph to have an Euler Circuit:

  • All vertices must have even degree
  • The graph must be connected

If these conditions aren't met, we need to add minimum edges to satisfy both requirements.

Algorithm Approach

The solution uses DFS to find connected components and count odd-degree vertices in each component −

  1. Find all connected components using DFS
  2. For each component, count vertices with odd degree
  3. Calculate minimum edges needed based on components and odd vertices

Implementation

def dfs(g, visit, odd_vert, degree, comp, v):
    visit[v] = 1
    if (degree[v] % 2 == 1):
        odd_vert[comp] += 1
    for u in g[v]:
        if (visit[u] == 0):
            dfs(g, visit, odd_vert, degree, comp, u)

def solve(n, m, source, destination):
    # Create adjacency list
    g = [[] for i in range(n + 1)]
    e = []  # components with all even degree vertices
    o = []  # components with odd degree vertices
    degree = [0] * (n + 1)
    visit = [0] * (n + 1)
    odd_vert = [0] * (n + 1)
    
    # Build graph and calculate degrees
    for i in range(m):
        g[source[i]].append(destination[i])
        g[destination[i]].append(source[i])
        degree[source[i]] += 1
        degree[destination[i]] += 1
    
    ans = 0
    comp = 0
    
    # Find connected components
    for i in range(1, n + 1):
        if (visit[i] == 0):
            comp += 1
            dfs(g, visit, odd_vert, degree, comp, i)
            if (odd_vert[comp] == 0):
                e.append(comp)
            else:
                o.append(comp)
    
    # Calculate minimum edges needed
    if (len(o) == 0 and len(e) == 1):
        return 0  # Already has Euler circuit
    
    if (len(o) == 0):
        return len(e) - 1  # Connect all even components
    
    if (len(e) != 0):
        ans += len(e)  # Connect even components to odd ones
    
    for i in range(len(o)):
        ans += odd_vert[o[i]] // 2  # Pair odd vertices in each component
    
    return ans

# Test the solution
nodes = 3
edges = 2
source = [1, 2]
destination = [2, 3]

result = solve(nodes, edges, source, destination)
print(f"Minimum edges needed: {result}")
Minimum edges needed: 1

How It Works

For the given example graph with 3 nodes and edges (1,2) and (2,3):

  • Node 1: degree 1 (odd)
  • Node 2: degree 2 (even)
  • Node 3: degree 1 (odd)

Since nodes 1 and 3 have odd degrees, we need to add 1 edge between them to make all degrees even, enabling an Euler Circuit.

Edge Cases

# Case 1: Already has Euler circuit
nodes1 = 3
edges1 = 3
source1 = [1, 2, 3]
destination1 = [2, 3, 1]
print(f"Case 1 result: {solve(nodes1, edges1, source1, destination1)}")

# Case 2: Disconnected components
nodes2 = 4
edges2 = 2
source2 = [1, 3]
destination2 = [2, 4]
print(f"Case 2 result: {solve(nodes2, edges2, source2, destination2)}")
Case 1 result: 0
Case 2 result: 1

Conclusion

This algorithm efficiently finds the minimum edges needed for an Euler Circuit by analyzing connected components and odd-degree vertices. The time complexity is O(V + E) using DFS traversal.

Updated on: 2026-03-25T10:00:32+05:30

271 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements