How Hierholzer builds an Eulerian circuit

Short answer: Hierholzer’s algorithm builds a closed walk that uses every edge exactly once. An undirected graph has such a circuit when it is connected aside from isolated vertices and every degree is even. The algorithm walks unused edges until it returns, then splices in a side tour at any vertex that still has unused edges. With adjacency structures that delete an edge in constant time, the walk is Θ(E).


What the problem actually asks

An Eulerian circuit is a closed trail through every edge. Brute force over edge orders is factorial. The degree condition is necessary because a closed trail enters and leaves a vertex the same number of times. It is also sufficient, together with connectivity of the edges, and Hierholzer’s walk is the construction that proves sufficiency.

A naive walk can finish a cycle and leave a side cluster of unused edges touching the cycle at one vertex. The algorithm does not backtrack edge choices. It starts a new walk at that vertex and splices the new closed trail into the old one.


What the algorithm guarantees

If the undirected graph meets the conditions, the spliced walk contains every edge once and returns to the start. If some degree is odd, or the non-isolated vertices are disconnected, the algorithm’s failure to consume every edge is a proof that no circuit exists. Time is Θ(E) when each used edge is removed from an adjacency structure in constant time, for example an adjacency list of deques and a parallel reverse-edge index so an undirected edge is deleted from both ends. Extra memory is the walk and the unused-edge structure.

A directed Eulerian circuit needs every vertex’s in-degree equal to its out-degree, and the underlying edges weakly connected in one piece. The same splice works, following outgoing arcs. An Eulerian trail that is not closed exists in the undirected case when exactly zero or two vertices have odd degree. The walk then starts at one odd vertex and ends at the other. Fleury’s algorithm also builds the tour, more slowly, by refusing to use a bridge of the remaining graph unless no other edge is available.


The working steps

  1. Check the degree condition. For a circuit, every undirected degree is even, or every directed in-degree equals the out-degree. Check that the edges lie in one connected piece.
  2. Start at a vertex with edges. Walk, deleting each edge as it is used, until the walk returns to its start and the current vertex has no unused edge.
  3. If some vertex on the walk still has an unused edge, start another walk there and splice it into the tour in place of that visit.
  4. Repeat the splice until no used vertex has unused edges. Every edge was in the connected piece, so none remain elsewhere.
  5. The directed case uses the same steps on outgoing arcs and deletes the arc rather than both ends.

The linear bound needs O(1) edge deletion. An adjacency matrix scan that hunts for the next unused edge costs more. The algorithm produces an edge tour, not a Hamiltonian path through vertices. A graph that fails the degree test has no Eulerian circuit, and no splice will invent one.


Approaches that look simpler and fail

Approach Why it fails when a leftover side tour is not spliced
Stop at the first return to the start Unused edges can still hang off a vertex on that cycle. The splice is the part that absorbs them.
Delete an undirected edge from only one adjacency list The other end still offers the edge. The walk can traverse it twice or get stuck seeing a ghost.
Start at an arbitrary vertex when exactly two degrees are odd and a circuit was required A circuit does not exist. A trail does, and it must start at an odd-degree vertex.
Use Fleury’s bridge test and quote Hierholzer’s linear bound Finding bridges inside the loop is more expensive. Hierholzer avoids the test by splicing after the fact.

Fleury’s algorithm is the bridge-avoiding construction of the same tour. Hamiltonian cycles, which visit each vertex once, are a different and NP-hard problem. Nothing in the degree check solves them.


A working example

This Python fragment builds an Eulerian circuit by splicing side tours into the main walk. Every vertex degree is even. It prints a circuit that uses each edge once, 0 1 2 0.

from collections import defaultdict
adj = defaultdict(list)
for u, v, eid in ((0, 1, 0), (1, 2, 1), (2, 0, 2)):
    adj[u].append((v, eid))
    adj[v].append((u, eid))
used = set()
stack = [0]
circuit = []
while stack:
    u = stack[-1]
    while adj[u] and adj[u][-1][1] in used:
        adj[u].pop()
    if not adj[u]:
        circuit.append(stack.pop())
    else:
        v, eid = adj[u].pop()
        used.add(eid)
        stack.append(v)
assert len(used) == 3 and circuit[0] == circuit[-1]
print(*circuit)

Where it is used

Hierholzer’s algorithm builds the Eulerian circuit used for snowplow and postal routes and for the Eulerian path in a de Bruijn graph during genome assembly. The tour exists when every vertex has even degree. Fleury’s bridge test computes the same tour more slowly.


When you can drop this algorithm

Drop Hierholzer when a vertex tour, not an edge tour, is the requirement. Keep it when an Eulerian circuit or trail exists and the adjacency structure can delete an edge from both ends in constant time.


Why this stays published

We publish this so a search for Hierholzer returns the splice of a side tour and the even-degree condition that makes the splice finish. Hierholzer’s algorithm builds an Eulerian circuit in linear time by walking unused edges and splicing side tours, provided every degree is even and the edges are connected.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *