Guest User
Using app without sign in

Practice book
Graph Theory Through Problems300 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.
Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on AmazonSelect an algorithm and generate steps to begin visualization
The shortest walk that covers every edge
Shortest closed walk that covers every edge: pair the odd vertices, repeat the paths between them, walk an Euler circuit
Select an algorithm and generate steps to begin visualization
The Chinese Postman Problem, also called route inspection, asks for the shortest closed walk that travels along every edge of a graph at least once. A postman has to walk every street of a district and return to the post office, repeating as little as possible. The Chinese mathematician Kwan Mei-Ko posed it in 1962, and Edmonds and Johnson solved it in polynomial time in 1973.
If every vertex has even degree, the graph has an Euler circuit, which uses every edge exactly once, and that is the answer. Otherwise the vertices of odd degree, always an even number of them, are the obstacle: a closed walk leaves every vertex as often as it enters it. The algorithm computes the shortest distances between the odd vertices and pairs them up so that the total distance is as small as possible, a minimum-weight perfect matching. Walking the shortest path between each pair twice makes every degree even, and an Euler circuit of the enlarged graph, found with Hierholzer's algorithm, is the shortest route. Its length is the total edge weight plus the weight of the matching.
Route inspection plans mail delivery, garbage collection, street sweeping, snow ploughing and meter reading, where every street must be covered. It is also used to inspect pipelines, power lines and railway tracks, and to test every link of a website or every transition of a state machine. In graph theory courses it follows Euler's theorem and shows how shortest paths and matchings work together.
The Chinese postman problem asks for the shortest closed walk that uses every edge at least once. The solution makes every degree even as cheaply as possible and then follows an Euler circuit.
ChinesePostman(graph):
O = the vertices of odd degree // always an even number
if O is empty: return EulerCircuit(graph)
D = shortest path distances between all pairs in O
M = minimum weight perfect matching on O, weights D
for each pair (u, v) in M:
duplicate every edge on a shortest u - v path
return EulerCircuit(graph with the duplicates)A closed walk through every edge exists exactly when every degree is even, by Euler's theorem. Any extra traversal adds 2 to the degree of the vertices it passes through and 1 to its two ends, so the repeated edges must form paths that pair up the odd vertices. The cheapest such set of paths is a minimum weight perfect matching on shortest path distances, which Edmonds and Johnson showed in 1973 makes the problem polynomial.
Find the shortest postman route on the example this page loads, a hexagon with two long chords, starting and ending at A.
Example graph: Undirected edges A-B (4), B-C (9), C-D (4), D-E (4), E-F (9), F-A (4), and chords B-F (9) and C-E (9). The edges weigh 52 in total.
The shortest postman route costs 52 + 16 = 68. The tempting shortcut of simply duplicating the two chords B-F and C-E would cost 18, two more than necessary, because the matching must use shortest path distances, not direct edges. The choice of starting vertex does not change the cost of a closed route.
Time: O(V^3) · Space: O(V^2)
Finding the odd vertices is O(V + E). With k odd vertices, k runs of Dijkstra give their pairwise distances in O(k (E + V log V)). The minimum weight perfect matching on k vertices costs O(k^3) with the blossom algorithm, and the Euler circuit is O(E) with Hierholzer. Since k is at most V, the total is O(V^3) in the worst case, dominated by the matching. Small instances, like the example, can simply try every pairing: the number of perfect matchings on k vertices is (k - 1)(k - 3)...1, which is 3 for k = 4 but already over two million for k = 16.
The undirected Chinese postman problem is one of the few routing problems solvable in polynomial time. Small changes to the question make it hard again.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Euler circuit (Hierholzer) | Every vertex already has even degree, so no edge needs repeating and the route weighs exactly the sum of the edges. | O(E) |
| Directed postman | Streets are one way. In-degree and out-degree must be balanced, which is a min-cost flow problem. | polynomial |
| Rural postman | Only some edges must be covered, for example only the streets with houses. This version is NP-hard. | NP-hard |
| Mixed postman | Some streets are one way and others two way. Also NP-hard, so heuristics are used. | NP-hard |
| Travelling salesman | The requirement is to visit every vertex, not every edge. | NP-hard |
Related algorithms: Eulerian Path (Undirected), Dijkstra's Algorithm, Hopcroft-Karp Matching, Christofides