Published by Dimitris Kougioumtzis · Updated
Consume each directed edge once, then splice the trail by reversing the backtracking order. Reject invalid degrees or disconnected edge sets.
Consume outgoing edges on a walk stack; backtracking emits exhausted vertices, and reversing that order splices an Euler trail.
When to use Hierholzer directed Euler trail
Routes using every directed edge exactly once.
Choosing an alternative
Hamiltonian paths visit every vertex once and solve a different problem.
Worked example: input and state changes
Example input
0,1,1;1,2,1;2,0,1;0,3,1
These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.
Step 0: Initialize graph state.
State at this checkpoint
{ "values": [ "-", "-", "-", "-" ], "active": [], "kind": "graph", "chosen": [], "visited": [] }Step 3: Consume edge #1: 1 > 2.
State at this checkpoint
{ "values": [ "out:2 in:1", "out:1 in:1", "out:1 in:1", "out:0 in:1" ], "active": [ 1, 2 ], "kind": "graph", "chosen": [ 0, 1 ], "visited": [], "stack": [ 0, 1, 2 ], "reverse": [] }Step 6: No unused outgoing edge at 3; prepend it by backtracking.
State at this checkpoint
{ "values": [ "out:2 in:1", "out:1 in:1", "out:1 in:1", "out:0 in:1" ], "active": [ 3 ], "kind": "graph", "chosen": [ 0, 1, 2, 3 ], "visited": [], "stack": [ 0, 1, 2, 0 ], "reverse": [ 3 ] }Step 8: No unused outgoing edge at 2; prepend it by backtracking.
State at this checkpoint
{ "values": [ "out:2 in:1", "out:1 in:1", "out:1 in:1", "out:0 in:1" ], "active": [ 2 ], "kind": "graph", "chosen": [ 0, 1, 2, 3 ], "visited": [], "stack": [ 0, 1 ], "reverse": [ 3, 0, 2 ] }Step 11: Euler trail: 0 > 1 > 2 > 0 > 3.
State at this checkpoint
{ "values": [ "out:2 in:1", "out:1 in:1", "out:1 in:1", "out:0 in:1" ], "active": [], "kind": "graph", "chosen": [ 0, 1, 2, 3 ], "visited": [ 0, 1, 2, 3 ], "result": 5, "path": [ 0, 1, 2, 0, 3 ] }
Expected final state
Euler trail: 0 > 1 > 2 > 0 > 3.
Complexity and input bounds
Family: Graphs. Time complexity: O(V + VE) with edge-array scans.
Space complexity: O(V + E) cursors, walk stack, and trail.
Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.
Directed edges as source,target,weight; separated by semicolons. Nodes 0–7, positive weights 1–99. Start node is 0. At most 16 edges. Weights are ignored. Vertices include every index from 0 to the highest endpoint.
The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.
Common mistakes and limitations
Degree balance alone is insufficient: every edge must be consumed in the same edge component, including distinct loops and parallel edges.
Further reading
For background on algorithm analysis and the JavaScript function interface:
- MIT OpenCourseWare: algorithm design and analysis foundations
- MDN: JavaScript function parameters and return values
Editorial and testing approach · Browse all algorithm guides
Complete JavaScript implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | // Edge arrays from/to/weight; 1..8 vertices indexed 0..vertices-1, 0..16 edges, endpoints in bounds. Weights ignored. Output buffer at least 256 integers. Returns number of nodes in an Euler trail (edges+1), writes those nodes, or returns -1 if no trail exists. Empty edge set returns [0]. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation. function algorithm(from, to, weight, edges, vertices, output) { let incoming = Array(256).fill(0); let outgoing = Array(256).fill(0); let cursor = Array(256).fill(0); let stack = Array(256).fill(0); let reversed = Array(256).fill(0); let u = 0; let index = 0; let delta = 0; let plus = 0; let minus = 0; let start = 0; let top = 0; let length = 0; for (index = 0; index < edges; index++) { outgoing[from[index]]++; incoming[to[index]]++; } for (u = 0; u < vertices; u++) { delta = outgoing[u] - incoming[u]; if (delta == 1) { plus++; start = u; } else if (delta == -1) { minus++; } else if (delta != 0) { return -1; } } if (!((plus == 0 && minus == 0) || (plus == 1 && minus == 1))) { return -1; } if (plus == 0) { for (u = 0; u < vertices; u++) { if (outgoing[u] > 0) { start = u; break; } } } stack[top++] = start; while (top > 0) { u = stack[top - 1]; index = cursor[u]; while (index < edges && from[index] != u) { index++; } cursor[u] = index + 1; if (index < edges) { stack[top++] = to[index]; } else { reversed[length++] = stack[--top]; } } if (length != edges + 1) { return -1; } for (index = 0; index < length; index++) { output[index] = reversed[length - 1 - index]; } return length; } |
Source
The 1873 paper concerns Eulerian traversals. This playground adapts the construction to directed edges.
- Ueber die Möglichkeit, einen Linienzug ohne Wiederholung und ohne Unterbrechung zu umfahren
Carl Hierholzer; Chr Wiener (1873). Historical research paper.