Algorithms · Visualized
Eulerian Path (Hierholzer)
Graph Algorithms
Traverse, order, and find shortest paths and spanning trees on graphs — the machinery behind maps, networks, schedulers and dependency resolvers.
An Eulerian path walks every edge exactly once, which is a very different question from visiting every vertex. Time: O(E) average, O(E) — every edge is pushed and popped at most once worst; space: O(V + E) for the adjacency multisets, stack and circuit.
Big-O: Traversals O(V + E); Dijkstra O(E log V) with a heap; Bellman–Ford O(V·E); Floyd–Warshall O(V³); MST O(E log E).
Eulerian Path (Hierholzer)
Graph AlgorithmsAn Eulerian path walks every edge exactly once, which is a very different question from visiting every vertex. A connected undirected graph has one only when exactly zero or two vertices have odd degree: with zero odd vertices every trail is a closed circuit that may start anywhere, and with two the trail must start at one odd vertex and end at the other. Hierholzer exploits that by walking unused edges greedily and pushing vertices onto a stack; when the walk gets stuck it pops the dead end onto the circuit and resumes from the vertex below it. The circuit is then reversed to produce the final trail.
How Eulerian Path (Hierholzer) Works
- Count the degree of every vertex; the default graph has odd degrees at B and E, so the trail must begin at B.
- Push the start vertex on a stack and walk any unused edge from the vertex on top.
- Delete the edge as you use it — each edge is consumed once and never revisited.
- When the current vertex has no unused edges, pop it onto the circuit and back up the stack.
- Continue until the stack is empty, then reverse the circuit to get the trail order.
Eulerian Path (Hierholzer) Worked Example
Input: {start: 1}
The default graph has nine undirected edges (A–B 4, A–C 2, B–C 1, B–D 5, C–D 8, C–E 10, D–E 2, D–F 6, E–F 3) with degrees A 2, B 3, C 4, D 4, E 3, F 2 — exactly two odd vertices, B and E, which is why the visualizer seeds start = 1 (B). One trail the walk can produce is B→D→C→A→B→C→E→D→F→E: it uses all nine edges once, begins at the odd vertex B and finishes at the other odd vertex E, and never repeats an edge even though it revisits vertices B, C, D and E.
def eulerian_trail(adj, start):
unused = {u: vs[:] for u, vs in adj.items()}
stack, circuit = [start], []
while stack:
u = stack[-1]
if unused[u]: # walk an unused edge
v = unused[u].pop()
unused[v].remove(u)
stack.append(v)
else: # stuck: backtrack to circuit
circuit.append(stack.pop())
return circuit[::-1] # every edge exactly onceBehind the Scenes Execution
Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.
Eulerian Path (Hierholzer) Time & Space Complexity
When to Use Eulerian Path (Hierholzer)
- Route inspection: sweeping every street once, or drawing a shape without lifting the pen.
- DNA fragment assembly, where reads are edges and the sequence is the trail.
- Validating that a network can be traversed edge-by-edge for testing or maintenance.
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Keep the momentum going
Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.