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 Algorithms

An 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

  1. Count the degree of every vertex; the default graph has odd degrees at B and E, so the trail must begin at B.
  2. Push the start vertex on a stack and walk any unused edge from the vertex on top.
  3. Delete the edge as you use it — each edge is consumed once and never revisited.
  4. When the current vertex has no unused edges, pop it onto the circuit and back up the stack.
  5. 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.

Eulerian Path (Hierholzer)O(E)
Use every edge exactly once by walking and backtracking onto a stack.
4215810263ABCDEF
Step 1 / 21
Default Current / Compare Swap / Write Settled Active / Path Pivot / Reject Locked (hollow) Target
Eulerian trail: walk unused edges, backtrack onto the stack when stuck (Hierholzer). Odd-degree nodes here: B, E — start at one of them.
Python · Graph Algorithms in Code
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 once

Behind 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

BestO(E)
AverageO(E)
WorstO(E) — every edge is pushed and popped at most once
SpaceO(V + E) for the adjacency multisets, stack and circuit

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

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Keep the momentum going

Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.