Masterclass: The Chinese Postman Problem (Route Inspection)
Imagine you are a mail carrier starting your shift at the main post office. Your job isn't just to visit a few specific houses; you have to deliver letters to every single street in the neighborhood.
Gas is expensive, and walking is tiring. Naturally, you want to plan a route that:
- Starts and ends at the post office (a closed loop).
- Traverses every street at least once.
- Keeps your total distance (or time/cost) to an absolute minimum.
This classic optimization challenge is formally known as the Chinese Postman Problem (or the Route Inspection Problem). First formulated by the Chinese mathematician Kwan Mei-Ko in 1960, it sits at the heart of network routing, snowplow scheduling, and garbage collection algorithms.
The Fundamental Distinction: TSP vs. CPP
Before diving into the solution, let's clear up a common source of confusion in Graph Theory.
- Traveling Salesman Problem (TSP): Focuses on Vertices (Houses/Cities). You want to visit every node at least once with minimal cost. This problem is NP-Hard—it gets exponentially difficult as the network grows.
- Chinese Postman Problem (CPP): Focuses on Edges (Streets/Roads). You want to traverse every edge at least once with minimal cost. This problem can be solved in Polynomial Time—meaning computers can solve it efficiently even for massive maps!
Graph Theory Foundation: Euler's Insight
To solve this problem, we represent the city as an undirected connected graph , where:
- Vertices (): Intersections or landmarks.
- Edges (): Roads connecting the intersections.
- Edge Weights: The distance or cost to travel along each road.
The Magic of Vertex Degrees
The degree of a vertex, written as , is the number of edges connected to it.
In 1736, Leonhard Euler proved a fundamental rule about graphs:
A graph contains a closed path that visits every edge exactly once (an Eulerian Circuit) if and only if every single vertex has an EVEN degree.
Why? Think about it intuitively: every time you enter an intersection along an edge, you must leave it along a different unused edge. Entrance and exit always come in pairs (2, 4, 6, etc.).
If every vertex in your network has an even degree, your job is easy! You can cover every street exactly once without any backtracking.
What If Some Vertices Have Odd Degrees?
In the real world, city street maps are rarely perfect Eulerian graphs. You will almost certainly encounter intersections connected to an odd number of streets (e.g., a 3-way intersection).
If an intersection has 3 streets attached:
- You enter on Street 1.
- You leave on Street 2.
- You enter on Street 3... and now you are stuck!
To escape, you are forced to re-traverse a street you've already covered.
The Core Strategy
To solve the Chinese Postman Problem on a graph with odd vertices:
- Identify all odd-degree vertices.
- Pair them up in a way that minimizes the total cost of the paths between them.
- "Duplicate" the edges along those shortest paths. Adding a duplicate edge turns an odd-degree vertex into an even-degree vertex!
- Find the Eulerian Circuit on this new augmented graph (multigraph).
The Master Algorithm Workflow
graph TD
A[Start: Graph G with weighted edges] --> B{Are all vertex degrees EVEN?}
B -- YES --> C[Find Eulerian Circuit using Hierholzer's Algorithm]
B -- NO --> D[Identify all Odd-Degree Vertices]
D --> E[Calculate Shortest Paths between all pairs of Odd Vertices]
E --> F[Find Minimum Weight Perfect Matching]
F --> G[Duplicate edges along matched Shortest Paths in G]
G --> H[Graph is now Eulerian: All degrees are EVEN]
H --> C
C --> I[Output Minimum Cost Tour]
Step-by-Step Mathematical Algorithm
Let's break down the formal steps required to algorithmically solve any Chinese Postman Problem.
Step 1: Degree Check
Compute for every .
- If no odd-degree vertices exist: Total Cost = Sum of all edge weights in .
- If odd-degree vertices exist: Proceed to Step 2.
(Note: By the Handshaking Lemma, the number of odd-degree vertices in any graph is always even, meaning they can always be paired up perfectly!)
Step 2: Extract Odd Vertices
Collect all odd-degree vertices into a set .
Step 3: Find Minimum Weight Matching
- Compute the shortest path distance between every pair of vertices in (using Dijkstra's or Floyd-Warshall algorithm).
- Construct a complete graph using vertices in , where edge weights equal these shortest path distances.
- Find a Minimum Weight Perfect Matching (pairing up all odd vertices such that the sum of path distances is as small as possible).
Step 4: Augment the Graph
For every paired match, duplicate the edges along their shortest path in the original graph . This converts into a multigraph where every vertex now has an even degree.
Step 5: Construct Eulerian Circuit
Run Hierholzer's Algorithm on to yield the optimal postman route.
Worked Example
Case 1: The Ideal Scenario (All Even Degrees)
Consider a simple triangular neighborhood with three intersections: , , and .
- Road : Weight = 2
- Road : Weight = 3
- Road : Weight = 4
Degree Check:
- (connected to , ) -> EVEN
- (connected to , ) -> EVEN
- (connected to , ) -> EVEN
Since all vertices have an even degree, an Eulerian Circuit exists directly!
- Optimal Route:
- Total Minimum Cost:
Algorithm Implementation (Python)
Here is how the Chinese Postman workflow translates into clean code logic:
import networkx as nxdef solve_chinese_postman(G): # Step 1: Find all nodes with odd degree odd_nodes = [v for v, degree in G.degree() if degree % 2 != 0] # Base Case: Graph is already Eulerian if len(odd_nodes) == 0: eulerian_circuit = list(nx.eulerian_circuit(G)) total_cost = sum(G[u][v]['weight'] for u, v in G.edges()) return eulerian_circuit, total_cost # Step 2 & 3: Find minimum weight matching for odd nodes # Build complete graph of odd nodes with shortest path distances odd_node_pairs = [] for i in range(len(odd_nodes)): for j in range(i + 1, len(odd_nodes)): u, v = odd_nodes[i], odd_nodes[j] dist = nx.shortest_path_length(G, u, v, weight='weight') odd_node_pairs.append((u, v, dist)) # Match odd nodes with minimal cost path sum matching = find_min_weight_matching(odd_node_pairs) # Step 4: Duplicate edges along shortest paths in a Multigraph augmented_graph = nx.MultiGraph(G) for u, v in matching: shortest_path = nx.shortest_path(G, u, v, weight='weight') for i in range(len(shortest_path) - 1): p1, p2 = shortest_path[i], shortest_path[i+1] weight = G[p1][p2]['weight'] augmented_graph.add_edge(p1, p2, weight=weight) # Step 5: Find Eulerian Circuit on augmented graph optimal_route = list(nx.eulerian_circuit(augmented_graph)) total_cost = sum(data['weight'] for u, v, data in augmented_graph.edges(data=True)) return optimal_route, total_costKey Takeaways
- Edge-centric Problem: Unlike TSP (which visits nodes), CPP visits every edge at least once.
- Degree Parity is Everything: If all vertex degrees are even, the solution is simply the sum of all edge weights (Eulerian Circuit).
- Odd Vertices Drive Extra Cost: Odd vertices force re-traversal. Minimizing the duplicated paths via Minimum Weight Perfect Matching gives the optimal solution.
- Efficiency: Unlike TSP, the Chinese Postman Problem is computationally efficient and solvable in polynomial time .
Mini Practice Question
Question: Why does an all-even connected graph make the Chinese Postman Problem trivial to solve?
Click to reveal the solution
Answer: When a connected graph has only even-degree vertices, it satisfies the necessary and sufficient conditions for the existence of an Eulerian Circuit.
This means you can start at any vertex, traverse every single edge exactly once, and return to the starting vertex without repeating a single road. Because no edge needs to be duplicated or re-visited, the minimum tour cost is simply the sum of all edge weights in the graph, requiring zero extra optimization steps!