learngraphtheory.org

Guest User

Using app without sign in

Cover of the book Graph Theory Through Problems

Practice book

Graph Theory Through Problems

300 Exercises with Hints, Worked Algorithms and Complete Solutions

By Mohammed Islam Hadjoudj

Learn graph theory by solving problems: 300 exercises, from routine checks to challenging proofs, each with a complete solution.

Available inKindlePaperback

13 chapters

Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on Amazon
Algorithm Selection

Select an algorithm and generate steps to begin visualization

Fleury's Algorithm Calculator

Every edge once, never burning a bridge too early

Euler trail or circuit, edge by edge, never crossing a bridge of the remaining graph unless forced

Time: O(E²)
Space: O(V + E)
Use Case: Drawing without lifting the pen, covering every street once
Showing the example graph for this algorithm.
Algorithm Execution

Select an algorithm and generate steps to begin visualization

About Fleury's Algorithm

Fleury's algorithm finds an Euler trail: a walk that uses every edge of a graph exactly once, like drawing a figure without lifting the pen. If the walk returns to its start, it is an Euler circuit. Published by M. Fleury in 1883, it is the classic textbook method, simple enough to follow by hand on paper.

How it works

A connected graph has an Euler trail exactly when zero or two of its vertices have odd degree. The walk starts at one of the two odd vertices, or anywhere if all degrees are even. At each step it crosses an edge from the current vertex and deletes it, with one rule: never cross a bridge of the remaining graph, an edge whose removal would cut it in two, unless it is the only edge left at that vertex. Crossing a bridge too early would leave the edges on this side unreachable. Since every step needs a bridge test, the algorithm takes O(E²) time; Hierholzer's algorithm finds the same trails in O(E).

Applications

Euler trails plan routes that must cover every street or link once, such as mail and snow-plough rounds, guide drawing machines and plotters, assemble DNA fragments with de Bruijn graphs, and test every transition of a network or circuit. Fleury's algorithm is mostly taught for its clear rule, and it pairs naturally with Hierholzer's algorithm, which this site also visualizes.

Pseudocode

Fleury's algorithm traces an Euler trail one edge at a time and deletes each edge it crosses. Its single rule is never to cross a bridge of the remaining graph unless there is no other choice.

Fleury(graph):
    check: connected, and 0 or 2 vertices of odd degree
    u = an odd vertex if there is one, else any vertex
    trail = [u]
    while u has edges left:
        choose an edge (u, v) that is not a bridge
            of the remaining graph, unless it is the only one
        delete (u, v)
        u = v,  trail.append(u)
    return trail

Crossing a bridge splits the remaining edges into two parts, and the trail can never come back to the part it leaves. Refusing to cross a bridge while another edge is available therefore keeps all remaining edges reachable from the current vertex, and the walk can only stop once every edge has been used.

Worked example, step by step

Trace an Euler trail from C on the example this page loads: a triangle and a square joined by a single edge, designed so that the first edge in label order is the one to avoid.

Example graph: Undirected edges C-A (the link), B-C, C-D and B-D (the triangle), and A-E, E-G, G-F and F-A (the square). C and A have degree 3, every other vertex degree 2.

  1. Check the conditions. The graph is connected and has exactly two odd vertices, C and A, so an Euler trail exists and must run from one to the other. The trail starts at C.
  2. Refuse the bridge. C has three edges, and in label order C-A comes first. It is a bridge: crossing it now would leave B and D unreachable forever. Since C has other edges, C-A is skipped.
  3. Around the triangle. The trail takes C-B, then B-D, then D-C, deleting each edge, and is back at C after three edges.
  4. Cross the bridge. Now C-A is the only edge left at C. Crossing a bridge is allowed when it is the last option, so the trail moves to A.
  5. Around the square. The square has no bridges, so the trail takes the edges in label order: A-E, E-G, G-F and F-A. All eight edges are used.

The Euler trail is C-B-D-C-A-E-G-F-A, eight edges from one odd vertex to the other. A naive walk that took the first edge in label order would have crossed C-A immediately, walked around the square and stopped at A with the triangle untouched, which is exactly the failure Fleury's rule prevents.

Complexity, and where it comes from

Time: O(E^2) · Space: O(V + E)

The trail takes E steps. At each step the candidate edge must be tested for being a bridge of the remaining graph, which takes a DFS or a connectivity check in O(V + E). The total is O(E (V + E)), which is O(E^2) for connected graphs. Dynamic connectivity structures can lower this in theory, but nobody does that in practice because Hierholzer's algorithm builds an Euler circuit or trail in O(E) without any bridge tests. Fleury is taught because its rule is easy to state and to check by hand.

When to use Fleury's Algorithm, and when not to

Fleury and Hierholzer produce the same kind of answer. The choice depends on whether you want clarity or speed.

AlternativePrefer it whenCost
Hierholzer's algorithmAny real implementation. It walks until stuck, then splices in sub-circuits from vertices with unused edges.O(E)
Euler's theorem checkYou only need to know whether a trail or circuit exists: connectivity plus counting odd degrees.O(V + E)
Chinese postmanThe graph has more than two odd vertices, so some edges must be repeated, and you want the cheapest way to do it.O(V^3)
Hamiltonian path searchEvery vertex must be visited once, rather than every edge. That problem is NP-complete.exponential

Common pitfalls

  • Starting at an even vertex. When exactly two vertices are odd, the trail must start at one of them and end at the other. Starting elsewhere strands at least one edge, however carefully bridges are avoided.
  • Testing bridges on the original graph. The bridge test is about the graph that remains after the deletions so far. An edge that was not a bridge at the start, like the last edge out of a nearly finished region, can become one later.
  • Counting deleted vertices in the connectivity check. Vertices whose edges are all used are no longer part of the remaining graph. If the bridge test treats them as components that must stay reachable, it rejects edges that are actually safe.
  • Applying it to directed graphs unchanged. In a directed graph the condition is in-degree equal to out-degree at every vertex, except one start with one extra outgoing edge and one end with one extra incoming edge. The bridge test must also respect directions.

Frequently asked questions

What is Fleury's algorithm?
It is a method, published by Fleury in 1883, for finding an Euler trail or circuit: a walk that uses every edge exactly once. It follows edges one at a time, deleting each one, and never crosses a bridge of the remaining graph unless no other edge is available.
When does an Euler path exist?
In a connected undirected graph, an Euler circuit exists when every vertex has even degree, and an Euler trail that is not a circuit exists when exactly two vertices have odd degree. The trail then starts at one odd vertex and ends at the other.
Why does Fleury avoid bridges?
A bridge is the only remaining connection between two groups of edges. Crossing it before the current side is finished would leave those edges behind with no way to return, so the trail would end before using them.
What is the time complexity of Fleury's algorithm?
O(E^2) with a straightforward bridge test at each of the E steps. Hierholzer's algorithm solves the same problem in O(E), which is why it is used in practice.
What is the difference between Fleury and Hierholzer?
Fleury looks ahead and avoids bridges so that it never gets stuck. Hierholzer does not look ahead: it walks until it gets stuck, then inserts detours from earlier vertices that still have unused edges. Hierholzer is faster, Fleury is easier to carry out by hand.

Related algorithms: Eulerian Path (Undirected), Chinese Postman, Bridge Finding, Depth-First Search

Interactive Controls

Zoom Controls

30%
Nodes: 7
Edges: 8