How Fleury avoids burning a bridge early

Short answer: Fleury’s algorithm builds an Eulerian tour by a walking rule: from the current vertex, take an edge that is not a bridge of the remaining graph, unless every remaining edge is a bridge. The rule keeps the unused edges in one connected piece until they are consumed. It produces the same kind of tour as Hierholzer and spends extra time on bridge tests, so its straightforward cost is O(E²) rather than linear.


What the problem actually asks

An Eulerian walk fails if it enters a region, leaves by the last exit, and still has unused edges inside. That last exit was a bridge of the remaining graph. Fleury’s rule refuses to use it while any non-bridge edge is available. The walk is then forced to finish the side region first.

The rule is local and greedy. It needs a bridge test after each choice, or at least a test of the candidate edges at the current vertex. Hierholzer never asks the question. It walks blindly and repairs the tour by splicing. Fleury is the version whose correctness you can see one edge at a time.


What the algorithm guarantees

On a connected undirected graph with every degree even, the rule produces an Eulerian circuit. With exactly two odd degrees, starting at one of them produces an Eulerian trail. Each bridge test by a depth-first search is O(E), and there are E steps, so a direct implementation is O(E²). Extra memory is the remaining adjacency and the current walk.

A bridge of the remaining graph is an edge whose removal disconnects the vertices that still have unused edges. Isolated vertices left behind by consumed edges do not count. The algorithm must recompute bridges on the remaining graph. Bridges of the original graph are not the right set once any edge has been used.


The working steps

  1. Check connectivity of the non-isolated vertices and the odd-degree count. Start at an odd-degree vertex if two exist, otherwise at any vertex of positive degree.
  2. While the current vertex has an unused edge, list those edges and find which of them are bridges of the remaining graph.
  3. Prefer a non-bridge. If every unused edge at this vertex is a bridge, take any of them.
  4. Delete the chosen edge from both ends and walk to its other end. Append it to the tour.
  5. When the vertex has no unused edge, the tour is complete if the preconditions held.

The tour is Eulerian, through edges. The quadratic bound is the cost of the bridge tests, not a property of Eulerian tours themselves. Hierholzer removes that cost. A Hamiltonian tour is still a different problem.


Approaches that look simpler and fail

Approach Why it fails when the bridge test uses the original graph
Avoid edges that were bridges before any edge was used An original bridge may be the correct first edge of a remaining side tour later, and an edge that became a bridge only after deletions is the one the rule must see.
Take a bridge whenever it is the lightest edge The rule is about connectivity of the remainder, not about weight. A lightest-edge rule is Kruskal and answers a different question.
Quote O(E) time That bound is Hierholzer’s, with constant-time deletion and no bridge test. Fleury pays for the tests.
Start at an even-degree vertex when two odd degrees exist The walk cannot be a single trail that covers every edge. One odd vertex would be left unserved as an endpoint.

A linear construction of the same tour is Hierholzer. Bridge finding as its own problem, on a static undirected graph, is the low-value depth-first search.


A working example

This Python fragment walks an Eulerian graph and refuses an edge that is a bridge of what remains, unless that edge is the only exit. The triangle 0-1-2 uses every edge. It prints 0 1 2 0.

from collections import defaultdict
adj = defaultdict(list)
edges = [(0, 1), (1, 2), (2, 0)]
for u, v in edges:
    adj[u].append(v)
    adj[v].append(u)

def reachable(src, blocked):
    seen = {src}
    stack = [src]
    while stack:
        u = stack.pop()
        for v in adj[u]:
            if (u, v) == blocked or (v, u) == blocked:
                continue
            if v not in seen:
                seen.add(v)
                stack.append(v)
    return seen

def is_bridge(u, v):
    before = len(reachable(u, None))
    adj[u].remove(v)
    adj[v].remove(u)
    after = len(reachable(u, None))
    adj[u].append(v)
    adj[v].append(u)
    return after < before

walk = [0]
while any(adj.values()):
    u = walk[-1]
    choices = list(adj[u])
    nxt = None
    for v in choices:
        if len(choices) == 1 or not is_bridge(u, v):
            nxt = v
            break
    adj[u].remove(nxt)
    adj[nxt].remove(u)
    walk.append(nxt)
assert walk[0] == walk[-1] and len(walk) == 4
print(*walk)

Where it is used

Fleury’s algorithm builds an Eulerian tour by refusing to cross a bridge too early. Street-routing and drawing problems that need such a tour use Hierholzer’s algorithm instead, which is linear. Fleury remains the version whose lesson is the bridge test.


When you can drop this algorithm

Drop Fleury in production code that only needs the tour. Hierholzer is faster and just as constructive. Keep Fleury when the teaching point is the bridge invariant itself, or when a bridge subroutine is already the primitive being demonstrated.


Why this stays published

We publish this so a search for Fleury’s algorithm returns the non-bridge preference and the quadratic cost of testing it on the remaining graph. Fleury’s algorithm builds an Eulerian tour by avoiding a bridge of the remaining graph unless no other edge is left, at O(E²) with a direct bridge test.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *