ProofComplete

Hierholzer Algorithm - Proof and Construction

Hierholzer's algorithm efficiently constructs Eulerian circuits in O(m)O(m) time by building and merging cycles. We prove correctness and analyze the algorithm's elegant recursive structure.

TheoremHierholzer's Algorithm Correctness

For a connected graph GG where all vertices have even degree, Hierholzer's algorithm constructs an Eulerian circuit in time O(m)O(m) where m=∣E∣m = |E|.

Proof

Algorithm:

EulerCircuit(G, start vertex v):
  circuit = []
  stack = [v]
  current_path = []

  while stack not empty:
    curr = stack.top()
    if curr has unused edges:
      next = neighbor via unused edge
      mark edge used
      stack.push(next)
    else:
      current_path.append(stack.pop())

  return reverse(current_path)

Correctness: We prove by induction that this constructs an Eulerian circuit.

Base case: If GG is a single cycle, the algorithm traces it completely.

Inductive step: Start at vv and follow unused edges until stuck. Since all degrees are even, when stuck at vertex uu, we must have u=vu = v (else uu would have odd unused degree). This forms a cycle CC.

If CC doesn't use all edges, since GG is connected, some vertex w∈Cw \in C has unused incident edges. The subgraph HH of unused edges has all even degrees (each edge in CC used exactly one edge at each endpoint). By induction, starting from ww constructs a circuit Cβ€²C' in HH.

Merge: Insert Cβ€²C' into CC at vertex ww. The result is an Eulerian circuit of GG.

Time complexity: Each edge is examined exactly twice (once for each endpoint). Stack operations are O(1)O(1). Total: O(m)O(m).

β– 
ExampleAlgorithm Execution

Consider a graph with vertices {1,2,3,4}\{1,2,3,4\} and edges forming two triangles sharing vertex 1: edges (1,2),(2,3),(3,1),(1,4),(4,2),(2,1)(1,2), (2,3), (3,1), (1,4), (4,2), (2,1).

Starting at vertex 1:

  1. Follow 1β†’2β†’3β†’11 \to 2 \to 3 \to 1 (first triangle)
  2. At 1, still have unused edges to 4
  3. Follow 1β†’4β†’2β†’11 \to 4 \to 2 \to 1 (second triangle)
  4. Merge at vertex 1: 1β†’2β†’3β†’1β†’4β†’2β†’11 \to 2 \to 3 \to 1 \to 4 \to 2 \to 1

The algorithm naturally discovers and merges cycles.

DefinitionComparison with Fleury's Algorithm

Fleury's algorithm avoids bridges (cut edges) of the remaining graph:

  • Time: O(m2)O(m^2) (bridge detection at each step)
  • Simpler conceptually but less efficient

Hierholzer's algorithm:

  • Time: O(m)O(m) (optimal)
  • More complex but builds cycles systematically
  • Preferred for implementation
Remark

Directed graphs: Hierholzer's algorithm extends naturally. A directed graph has an Eulerian circuit if and only if it's strongly connected and every vertex has in-degree equal to out-degree. The algorithm follows directed edges, maintaining the same cycle-merging structure.

Applications: De Bruijn graph construction for DNA assembly uses Hierholzer's algorithm to find Eulerian paths through sequences, enabling genome reconstruction from short reads.

ProofOptimality of Hierholzer

Any Eulerian circuit algorithm must examine each edge at least once to include it in the circuit. Hierholzer achieves O(m)O(m) by visiting each edge at most twice (once to traverse, once during the merge phase). This is optimal for explicit circuit construction.

For just deciding Eulerian circuit existence, O(m)O(m) is also optimal since we must verify degree parity, requiring edge examination.

β– 

The elegance of Hierholzer's algorithm lies in its recursive structure: build local cycles and merge them globally. This divide-and-conquer approach efficiently solves a problem that appears inherently global, demonstrating the power of local-to-global reasoning in graph algorithms.