The Chinese Postman Problem (also called Route Inspection) asks for the shortest closed walk that travels every edge of a graph at least once and returns to the start. It is the natural cousin of the Traveling Salesman, but about edges instead of vertices — and unlike TSP it is solvable in polynomial time.
Problem. Given a connected, undirected, weighted graph, find the minimum total distance of a route that starts and ends at the same vertex and uses every edge at least once.
Example: corners A, B, C, D with streets A–B (4), A–C (2), A–D (3), B–C (5), B–D (1). The sum of all
streets is 15, and the cheapest postman route is 15 + 4 = 19.
The slow way first
If every vertex already had an even degree, the answer would just be the sum of all edge weights — a graph with all-even degrees has an Euler circuit, a closed walk that uses every edge exactly once with zero repetition. No street is ever retraced, so you pay each one exactly once.
The trouble is the odd-degree vertices. You enter and leave a vertex in pairs, so a vertex you visit an odd number of half-times forces you to retrace some street. Trying every possible set of retraced streets is exponential, so we need the structure underneath.
The idea: make every vertex even by duplicating cheap paths
There is a clean theorem: a connected graph has an even number of odd-degree vertices, and you can always pair them up. For each pair, duplicating the shortest path between them flips both endpoints to even degree. Choosing the pairing that minimizes total duplicated distance — a minimum-weight perfect matching on the odd vertices — gives exactly the extra distance the postman must pay. After duplication every vertex is even, so an Euler circuit exists.
So the answer is always sum(all edges) + (cheapest set of duplicated paths to even out the odd vertices).
Walk through it
Step through the animation. First we sum the five streets: 4 + 2 + 3 + 5 + 1 = 15. Then we read off the degrees — A=3, B=3, C=2, D=2 — and the odd vertices light up red: A and B. There is just one odd pair, so the matching is forced: duplicate the cheapest A-to-B path, which costs 4. We add that and walk the now-even graph as an Euler circuit. Final answer: 15 + 4 = 19.
Pseudocode
total = sum of every edge weight
compute degree of each vertex
odd = list of vertices whose degree is odd
if odd is empty:
return total # graph is already Eulerian
dist = all-pairs shortest paths
extra = minimum-weight perfect matching of the odd vertices, using dist
return total + extraThe Python solution
def chinese_postman(n, edges):
total = sum(w for _, _, w in edges)
deg = degrees(n, edges)
odd = [v for v in range(n) if deg[v] % 2 == 1]
if not odd:
return total # already Eulerian
dist = all_pairs_shortest(n, edges)
extra = min_weight_matching(odd, dist)
return total + extratotalis the sum of all edge weights — the unavoidable floor, since every street is walked at least once.degholds each vertex degree;oddcollects the vertices with an odd degree, the only ones that block an Euler circuit.- If there are no odd vertices the graph is already Eulerian, so
totalitself is the answer. distis the all-pairs shortest-path table, so a matched pair can be joined by their cheapest route (not just a direct edge).min_weight_matchingpairs the odd vertices to minimize total duplicated distance; addingextramakes every vertex even and yields the optimal route.
Complexity
| Case | Time | Notes |
|---|---|---|
| Sum edges + degrees | O(E) (moderate) | one pass over edges |
| All-pairs shortest paths | O(V³) (moderate) | Floyd-Warshall |
| Min-weight matching | O(k³) (moderate) | k = number of odd vertices |
O(V²) (moderate)The odd-vertex count k is usually small, but the matching is the conceptually hard part. Everything else is standard shortest-path work, which keeps the whole problem polynomial — the key contrast with the NP-hard Traveling Salesman.
When this pattern shows up
Whenever a problem says "traverse every edge / every road / every connection and come back," think Euler circuit and parity of degrees. The fix for odd vertices is always the same move: pair them and duplicate the cheapest connecting paths.
Do not pair odd vertices with their direct edge blindly. The cheapest way to join two odd vertices may route through other vertices, so you must use shortest-path distances, not raw edge weights, when computing the matching.
Practice
A graph has odd-degree vertices A and B only, with shortest A-to-B distance 4, and all edges summing to 15. What is the postman route length?
1. When is the postman route simply the sum of all edge weights?
2. Why do we focus on odd-degree vertices?
3. What does duplicating a shortest path between two odd vertices accomplish?
4. Why must we use shortest-path distances for the matching, not direct edges?