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

Chinese Postman Calculator

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

Time: O(V³)
Space: O(V²)
Use Case: Mail delivery, garbage collection, street sweeping, inspection routes
Showing the example graph for this algorithm.
Algorithm Execution

Select an algorithm and generate steps to begin visualization

About Chinese Postman

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.

How it works

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.

Applications

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.

Pseudocode

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.

Worked example, step by step

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.

  1. Find the odd vertices. A and D have degree 2. B, C, E and F each have degree 3, so four vertices are odd and an Euler circuit is impossible as the graph stands.
  2. Shortest distances. Between the odd vertices: B to F is 8 by B-A-F, shorter than the chord of 9. C to E is 8 by C-D-E. B to C and E to F are 9. B to E and C to F are 17.
  3. Choose the pairing. Four vertices can be paired in three ways: B-F with C-E costs 8 + 8 = 16, B-C with E-F costs 18, and B-E with C-F costs 34. The first wins.
  4. Duplicate the paths. Copying B-A-F and C-D-E adds the edges A-B, A-F, C-D and D-E a second time, 16 in total. Every vertex now has degree 4, which is even.
  5. Walk the Euler circuit. An Euler circuit of the doubled graph is A-F-B-A-F-E-D-C-E-D-C-B-A, twelve edges in which A-F, A-B, C-D and D-E are each walked twice.

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.

Complexity, and where it comes from

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.

When to use Chinese Postman, and when not to

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.

AlternativePrefer it whenCost
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 postmanStreets are one way. In-degree and out-degree must be balanced, which is a min-cost flow problem.polynomial
Rural postmanOnly some edges must be covered, for example only the streets with houses. This version is NP-hard.NP-hard
Mixed postmanSome streets are one way and others two way. Also NP-hard, so heuristics are used.NP-hard
Travelling salesmanThe requirement is to visit every vertex, not every edge.NP-hard

Common pitfalls

  • Duplicating direct edges instead of shortest paths. The cost of pairing two odd vertices is the shortest path between them, which may avoid the edge that joins them. In the example the chords cost 9 each while the detours cost 8.
  • Pairing odd vertices greedily. Matching the two closest odd vertices first, then the next two, can lock in a poor overall pairing. The problem needs a true minimum weight perfect matching, or exhaustive search when there are few odd vertices.
  • Ignoring connectivity. If the edges do not all lie in one connected component, no single route can cover them. Check connectivity, ignoring isolated vertices, before computing anything.
  • Using the undirected method on one way streets. Parity arguments do not apply to directed graphs, where the condition is in-degree equal to out-degree. The directed case is solved with min-cost flow, and the mixed case is NP-hard.

Frequently asked questions

What is the Chinese postman problem?
It asks for the shortest closed walk that traverses every edge of a graph at least once. It is named after Kwan Mei-Ko, who posed it in 1962 for a postman who must walk every street of a district and return to the post office.
How do you solve the Chinese postman problem?
Find the vertices of odd degree, compute shortest path distances between them, pair them up with a minimum weight perfect matching, duplicate the edges of the chosen shortest paths, and follow an Euler circuit of the resulting graph.
Why are there always an even number of odd vertices?
The sum of all degrees is twice the number of edges, so it is even. A sum of integers is even only if it contains an even number of odd terms, so odd-degree vertices always come in pairs and can always be matched.
What is the difference between the Chinese postman problem and the TSP?
The postman must traverse every edge, the travelling salesman must visit every vertex. The undirected postman problem is solvable in polynomial time through matching, while the TSP is NP-hard.
What are applications of the Chinese postman problem?
Mail and parcel delivery, refuse collection, street sweeping, snow ploughing, road gritting, meter reading and the inspection of roads, pipelines or power lines: any task where every street or segment must be covered and repeated travel should be minimal.

Related algorithms: Eulerian Path (Undirected), Dijkstra's Algorithm, Hopcroft-Karp Matching, Christofides

Interactive Controls

Zoom Controls

37%
Nodes: 6
Edges: 8