The Chinese Postman Problem
A mail carrier has to walk along every street on their route and return to the post office. A snowplough, a street sweeper and a road inspector have the same job. What is the shortest route that covers every edge of a weighted graph at least once and returns to the start? This is the Chinese postman problem (also called the route inspection problem), first posed by the Chinese mathematician Kwan Mei-Ko in 1962.
Key ideas
Section titled “Key ideas”When no edges need repeating
Section titled “When no edges need repeating”If every vertex of a connected graph is even, the graph has an Eulerian circuit: a closed route using every edge exactly once. That route is clearly the shortest possible, so
Why odd vertices force repeats
Section titled “Why odd vertices force repeats”At an odd vertex, the route can’t pair every arrival with a departure along a new edge, so some edges at that vertex must be travelled twice. Repeating an edge is the same as adding a copy of it to the graph. We want to add copies so that every vertex becomes even, as cheaply as possible.
Adding copies along a path from one odd vertex to another changes the degree of those two end vertices by (making them even) and of every vertex in between by (keeping them even). So the cheapest fix is to pair up the odd vertices and repeat the shortest path between each pair. The new graph has an Eulerian circuit, and that circuit is the postman’s route.
The algorithm
Section titled “The algorithm”- List the odd vertices. If there are none, the answer is the total weight.
- List every way of pairing them up.
- With odd vertices, say X and Y, there’s only one pairing: XY.
- With odd vertices W, X, Y, Z, there are three: WX and YZ; WY and XZ; WZ and XY.
- For each pairing, find the shortest path between each pair (by inspection, checking possible routes) and add the lengths.
- Choose the pairing with the smallest total. Repeat the edges on those shortest paths.
- Shortest route length total weight of all edges the smallest pairing total. Find an actual route as an Eulerian circuit of the graph with the repeated edges added.
The IB course uses graphs with up to four odd vertices.
A variation: start and finish at different vertices
Section titled “A variation: start and finish at different vertices”Sometimes the route doesn’t have to return to its start. Then two odd vertices can be the start and finish, and they don’t need pairing.
- With odd vertices: start at one, finish at the other. Nothing is repeated, so the length is just the total weight.
- With odd vertices: you still pair two of them. Repeat the single shortest of the six possible odd-to-odd paths, and start and finish at the other two odd vertices.
Worked examples
Section titled “Worked examples”Example 1: Two odd vertices
Section titled “Example 1: Two odd vertices”A road inspector must drive along every road in a network and return to the depot at A. The roads, with lengths in km, are AB , AC , BC , BD , CD , CE , DE , DF and EF . Find the length of the shortest inspection route.
Solution. Total length of all roads:
Degrees: A , B , C , D , E , F . The odd vertices are B and E, so there’s one pairing: BE.
Shortest path from B to E (there’s no direct road): B–D–E is , B–C–E is , and B–C–D–E is . The shortest is B–D–E, km.
Repeat BD and DE. The shortest route is km.
Example 2: Four odd vertices
Section titled “Example 2: Four odd vertices”Find the shortest route that travels along every edge of the graph above at least once and returns to its starting point. Write down one such route starting at A.
Solution. Total weight: .
Degrees: A , B , C , D , E , F . There are four odd vertices: A, B, D, E.
Find the shortest path between each pair of odd vertices:
| Pair | Shortest path | Length |
|---|---|---|
| AB | A–B | 4 |
| DE | D–F–E | 9 |
| AD | A–D (or A–C–D) | 9 |
| BE | B–E | 8 |
| AE | A–B–E | 12 |
| BD | B–C–D | 8 |
Now compare the three pairings:
| Pairing | Total |
|---|---|
| AB and DE | |
| AD and BE | |
| AE and BD |
The best pairing is AB and DE, so repeat AB, DF and FE. The shortest route has length
With the repeats, every vertex has degree , so there’s an Eulerian circuit. One route from A:
Check: it uses edges (the original edges plus the repeats), and its length is .
Example 3: Finishing somewhere else
Section titled “Example 3: Finishing somewhere else”For the graph in Example 2, the route may now start and finish at different vertices. Find the length of the shortest route, and where it must start and finish.
Solution. Two of the odd vertices can be the start and finish; the other two must be joined by a repeated shortest path. From the table in Example 2, the shortest odd-to-odd path is AB, length .
So repeat AB, and start at D and finish at E (or the reverse). The shortest route is
Common mistakes
Section titled “Common mistakes”Repeating the direct edge when a shorter path exists. In Example 2, D and E aren’t even adjacent, and in other graphs the direct edge between two odd vertices can be longer than a route through another vertex. Always check the alternatives.
Checking only one pairing. With four odd vertices there are three pairings, and the best one isn’t always the one with the single shortest path. Work out all three totals.
Forgetting the total weight. The answer is the total of all the edges plus the repeated paths, not just the repeats.
Pairing even vertices. Only the odd vertices need fixing. Vertices in the middle of a repeated path stay even.
Mixing up the closed and open versions. If the route must return to its start, pair all the odd vertices. If it can finish elsewhere, two odd vertices become the ends and only one pair gets a repeated path.
Practice
Section titled “Practice”1. (Warm-up) A graph has edges AB , BC , CA , CD , DE and EC . Find the length of the shortest closed route that travels every edge.
Solution
Degrees: A , B , C , D , E . All even, so there’s an Eulerian circuit and nothing needs repeating:
2. (Warm-up) A graph has exactly four odd vertices, P, Q, R and S. List all the possible pairings.
Solution
PQ and RS; PR and QS; PS and QR. (P must be paired with one of the other three, and that decides the second pair.)
3. (Warm-up) A connected graph has total weight . Its only odd vertices are X and Y, and the shortest path between them has length . What is the length of the shortest closed route that travels every edge?
Solution
Repeat the shortest X–Y path: .
4. (Core) A park has paths PQ , PR , QR , QS , RS , RT and ST (in hundreds of metres). A ranger must walk every path and return to the start. Find the length of the shortest route, and which path(s) are walked twice.
Solution
Total: .
Degrees: P , Q , R , S , T . Odd vertices Q and S.
Shortest Q–S path: direct QS is ; Q–R–S is . So repeat QS.
Shortest route: hundred metres, which is km, with path QS walked twice.
5. (Core) A graph has edges AB , AC , BC , BD , CD , CE and DE . Find the length of the shortest closed route that travels every edge, and say which edges are repeated.
Solution
Total: .
Degrees: A , B , C , D , E . Odd vertices B and D.
Shortest B–D path: direct BD is , but B–C–D is . So repeat BC and CD.
Shortest route: .
6. (Core) A graph has edges AB , AC , BC , BD , CD , AE , CE and DE . Find the length of the shortest closed route that travels every edge.
Solution
Total: .
Degrees: A , B , C , D , E . Odd vertices A, B, D, E.
Shortest paths: AB (A–C–B, shorter than the direct ), DE , AD (A–C–D or A–C–B–D), BE (B–C–E or B–D–E), AE , BD .
| Pairing | Total |
|---|---|
| AB and DE | |
| AD and BE | |
| AE and BD |
Repeat AE and BD. Shortest route: .
7. (Core) For the graph in Question 6, the route may start and finish at different vertices. Find the length of the shortest route that travels every edge, and where it starts and finishes.
Solution
Repeat only the single shortest odd-to-odd path, which is BD (), and start and finish at the other two odd vertices, A and E.
Shortest route: , starting at A and finishing at E (or the reverse).
8. (Challenge) In the graph of Example 2, the weight of edge AB is changed from to , where . For which values of is pairing AB and DE still the best choice? Find the length of the shortest closed route in terms of for those values.
Solution
The shortest A–B path is now the smaller of and A–C–B . Compare the three pairings when :
- AB and DE: .
- AD and BE: B–E is still , and A–D is the smaller of and A–B–C–D . The total is the smaller of and .
- AE and BD: B–D is still (B–C–D), and A–E is the smaller of (A–C–E) and (A–B–E). The total is the smaller of and .
Since always, AB and DE is best exactly when , that is, . (At it ties with AD and BE; for , AD and BE is better.)
For : total weight is , so the shortest route is
Check: gives , matching Example 2.
9. (Challenge) Explain why, in the Chinese postman algorithm, the repeated edges should form shortest paths joining the odd vertices in pairs, and why the resulting route is the shortest possible.
Solution
Think of the repeated edges as extra copies added to the graph. A closed route covering every edge exactly once in the new graph exists only if every vertex is even. The extra copies must therefore add an odd number of edges at each odd vertex and an even number at each even vertex. Any set of extra edges with this property can be split into trails that join the odd vertices in pairs (plus possibly some closed circuits, which only add length). So every possible route repeats, at the least, a set of paths joining the odd vertices in pairs. The cheapest way to join a given pair is a shortest path, and checking every pairing finds the cheapest overall. The route can’t be shorter than the total of all edges plus this cheapest set of repeats, and the Eulerian circuit of the new graph achieves exactly that length, so it’s the shortest route.