Hierholzer Algorithm - Proof and Construction
Hierholzer's algorithm efficiently constructs Eulerian circuits in time by building and merging cycles. We prove correctness and analyze the algorithm's elegant recursive structure.
For a connected graph where all vertices have even degree, Hierholzer's algorithm constructs an Eulerian circuit in time where .
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 is a single cycle, the algorithm traces it completely.
Inductive step: Start at and follow unused edges until stuck. Since all degrees are even, when stuck at vertex , we must have (else would have odd unused degree). This forms a cycle .
If doesn't use all edges, since is connected, some vertex has unused incident edges. The subgraph of unused edges has all even degrees (each edge in used exactly one edge at each endpoint). By induction, starting from constructs a circuit in .
Merge: Insert into at vertex . The result is an Eulerian circuit of .
Time complexity: Each edge is examined exactly twice (once for each endpoint). Stack operations are . Total: .
Consider a graph with vertices and edges forming two triangles sharing vertex 1: edges .
Starting at vertex 1:
- Follow (first triangle)
- At 1, still have unused edges to 4
- Follow (second triangle)
- Merge at vertex 1:
The algorithm naturally discovers and merges cycles.
Fleury's algorithm avoids bridges (cut edges) of the remaining graph:
- Time: (bridge detection at each step)
- Simpler conceptually but less efficient
Hierholzer's algorithm:
- Time: (optimal)
- More complex but builds cycles systematically
- Preferred for implementation
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.
Any Eulerian circuit algorithm must examine each edge at least once to include it in the circuit. Hierholzer achieves 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, 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.