Interactive Graph Theory Learning
Interactive Graph Theory Learning
Guest User
Using app without sign in
Euler path and circuit finder
Finds path visiting every edge exactly once in undirected graphs
Select an algorithm and generate steps to begin visualization
An Eulerian path traverses every edge of a graph exactly once; an Eulerian circuit does so and returns to its start. Leonhard Euler founded graph theory in 1736 by proving the Seven Bridges of Konigsberg admits no such walk.
Existence is easy to check: a connected undirected graph has an Eulerian circuit exactly when every vertex has even degree, and an Eulerian path when exactly zero or two vertices have odd degree. Hierholzer's algorithm constructs the walk in O(E) time: follow unused edges until returning to the start, then repeatedly splice in detour cycles from vertices that still have unused edges.
Eulerian paths solve route inspection problems such as snow plowing, street sweeping and postal delivery, reconstruct DNA sequences from k-mers in bioinformatics, and generate De Bruijn sequences. The parity-based existence test is a classic interview question distinguishing it from the much harder Hamiltonian problem.
The existence test is pure counting and takes one pass. Only if it succeeds do you build the trail, using Hierholzer rather than naive backtracking.
// Existence, undirected connected graph:
// 0 odd-degree vertices -> Eulerian circuit
// 2 odd-degree vertices -> Eulerian path between them
// anything else -> neither
Hierholzer(graph, start):
stack = [start]; trail = []
while stack is not empty:
u = stack.top
if u has an unused incident edge (u,v):
mark that edge used
stack.push(v)
else:
trail.append(stack.pop())
reverse(trail)Hierholzer works because it never has to guess. It walks until it gets stuck, which on an all-even-degree graph can only happen back at the start, then splices in detours from vertices that still have unused edges. Every vertex is entered and left the same number of times, which is exactly what even degree guarantees, so the pieces always merge into one closed trail.
Build an Eulerian circuit on two triangles that share a single vertex, taking neighbours in alphabetical order.
Example graph: Undirected edges A-B, B-C, C-A forming one triangle, and C-D, D-E, E-C forming a second, joined at C.
The Eulerian circuit is A to B to C to D to E to C to A, using all six edges exactly once and returning to the start. Note that C appears twice in the trail, which is allowed and expected: an Eulerian trail may revisit vertices freely, it may only not reuse an edge. That is the whole difference from a Hamiltonian path, which visits every vertex once and does not care about edges.
Time: O(V + E) · Space: O(V + E)
The degree count is one pass over the edges at O(E), and the connectivity check is one traversal at O(V + E). Hierholzer then pushes and pops each vertex occurrence once and marks each edge used exactly once, so it is O(E) provided each vertex keeps a pointer into its adjacency list rather than rescanning from the beginning. Without that pointer the inner search degrades to O(V·E). Space is the used-edge marks plus the stack and trail, which hold O(E) entries. The contrast with Hamiltonian paths is worth noting: Eulerian is linear, Hamiltonian is NP-complete, purely because edges can be counted locally by degree while vertices cannot.
Eulerian problems are easy; the superficially similar Hamiltonian ones are not. Check which you actually have.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Hamiltonian path | You must visit every VERTEX once rather than every edge. NP-complete, so entirely different methods apply. | exponential |
| Chinese Postman | Odd-degree vertices exist but you still want a closed route covering every edge, allowing repeats at minimum cost. | O(V^3) |
| Fleury’s algorithm | You want a trail built without a stack. Conceptually simpler but slower, since it avoids bridges by testing them. | O(E^2) |
| Route inspection / de Bruijn | Genome assembly and similar, where Eulerian paths in a de Bruijn graph reconstruct a sequence. | O(V + E) |
Read the full article: Eulerian Paths and Circuits
Related algorithms: Hamiltonian Path, Depth-First Search