Eulerian Path and Circuit is the classic "draw this shape without lifting your pen" puzzle, turned into a graph problem. The whole thing hinges on a single, beautiful counting rule about vertex degrees.
Problem. Given an undirected connected graph, decide whether you can walk a trail that uses every edge exactly once. If the trail returns to where it started it is an Euler circuit; if it starts and ends at different vertices it is an Euler path.
Example: vertices A, B, C, D with edges A-B, A-C, B-C, B-D, C-D. Degrees are A=2, B=3, C=3, D=2 →
two odd vertices → an Euler path exists, e.g. B → A → C → B → D → C.
The slow way first
The brute-force instinct is to try every possible ordering of edges and check whether one forms a valid single-pass walk. With m edges that is up to m! orderings — hopeless for anything but a tiny graph.
The question to ask: is there a cheap structural test that tells me whether such a walk can exist at all, before I try to build one? There is, and it depends only on the degree of each vertex.
The idea: count odd degrees
Every time a walk passes through a vertex, it uses one edge to enter and one to leave — edges get consumed in pairs. So a vertex you pass through must have an even degree. The only vertices allowed to have an odd degree are the two endpoints of the walk (you leave the start without entering, and enter the end without leaving).
That gives the rule for a connected graph:
Zero odd vertices → Euler circuit. Exactly two → Euler path starting at one odd vertex and ending at the other. Any other count (only 1, or 4, or more) → no Euler trail. Once the test passes, you build the actual walk with Hierholzer's algorithm, greedily following unused edges and splicing in detours.
Walk through it
Step through the animation. First each vertex gets a deg= label. Then the two odd-degree vertices (B and C) light up red — exactly two, so an Euler path exists. Finally the walk traces edge by edge, each one turning green as it is used, starting at B and ending at C.
Pseudocode
count degree[v] for every vertex
odd = vertices whose degree is odd
if number of odd vertices is not 0 and not 2:
return "no Euler trail"
start = an odd vertex if any, else any vertex
# Hierholzer: greedily follow unused edges, backtrack to emit
push start onto a stack
while stack not empty:
v = top of stack
if v still has an unused edge (v, w):
mark that edge used; push w
else:
pop v into the trail
trail reversed is the Euler walkThe Python solution
def euler_trail(n, edges):
deg = [0] * n
for u, v in edges: # tally every endpoint
deg[u] += 1
deg[v] += 1
odd = [v for v in range(n) if deg[v] % 2 == 1]
if len(odd) not in (0, 2):
return None # no Euler trail
start = odd[0] if odd else 0
trail = []
stack = [start]
while stack: # Hierholzer's algorithm
v = stack[-1]
if adj[v]:
stack.append(take_edge(v))
else:
trail.append(stack.pop())
return trail[::-1]degcounts how many edges touch each vertex; every edge bumps both of its endpoints.oddcollects the vertices with an odd degree — the only ones allowed to be trail endpoints.- The
len(odd) not in (0, 2)check is the whole decision: 0 means circuit, 2 means path, anything else means impossible. startmust be an odd vertex when there are two, so the path begins at a valid endpoint; for a circuit any vertex works.- The
while stackloop is Hierholzer's algorithm: follow unused edges as deep as you can, and when a vertex is stuck, pop it into the trail. Reversing the popped order gives the walk.
Complexity
| Case | Time | Notes |
|---|---|---|
| Try every edge ordering | O(m!) (moderate) | brute force, infeasible |
| Degree check | O(n + m) (moderate) | one pass over edges |
| Hierholzer build | O(n + m) (moderate) | each edge consumed once |
O(n + m) (moderate)The degree rule replaces an astronomical search with a single linear pass, and constructing the actual walk is linear too.
When this pattern shows up
Whenever a problem is about traversing every edge (rather than every vertex), think Euler, and reach first for the odd-degree count. Reconstruct-itinerary and "valid arrangement of pairs" problems are Euler trails in disguise — the answer is built with Hierholzer's algorithm.
The degree rule assumes the graph is connected (ignoring isolated vertices). A graph can have all even degrees yet split into two pieces, in which case no single trail covers every edge. Always confirm the edge-bearing vertices are connected before trusting the count.
Practice
A graph has degrees A=2, B=2, C=2, D=2 and is connected. Circuit, path, or neither?
1. How many odd-degree vertices does a connected graph need for an Euler path (not circuit)?
2. Why must a pass-through vertex have even degree?
3. A connected graph has exactly four odd-degree vertices. What can you conclude?
4. If an Euler path exists with two odd vertices, where must the walk start?