Run Hierholzer stack traversal, consume every edge once, and append vertices while dead ends unwind.
Core idea
Hierholzer's algorithm walks unused edges until it reaches a dead end. Dead-end vertices are appended while the stack unwinds, automatically splicing every discovered cycle into one trail.
Read the visualization
Remaining-degree badges shrink as edges are consumed. The traversal stack records the live walk, and backtracking builds the answer in reverse order.
Load the connected graph and the requirement to use every edge exactly once.
7conststart= odd.length? odd[0]:0; // check odd-degree vertex check from check
8constused=newArray(edges.length).fill(false);
9conststack= [start];
10constpath:number[] = [];
11while (stack.length) {
12constu= stack[stack.length-1];
13constnext= adj[u].find((e) =>!used[e.eid]);
14if (next) {
15 used[next.eid] =true; // walk edge: walk edge + walk stack
16 stack.push(next.v);
17 } else {
18 stack.pop(); // back: back stack back path
19 path.push(u);
20 }
21 }
22return path.reverse(); // done pop order done Eulerian path
23}
stack( empty)
path( empty)
target7 count edge visit each once
1 / 12
Complexity and tradeoffs
Time: O(V+E). Space: O(V+E). Connectivity and odd-degree conditions determine whether an undirected Eulerian trail exists.
Invariant: Every consumed edge appears exactly once in the unfinished stack walk or the reversed output suffix.
Where it fits
Eulerian trails model route inspection, DNA assembly with de Bruijn graphs, and reconstruction from adjacent pairs. Hamiltonian paths instead require visiting vertices once and are much harder.