Solving the Chinese Postman Problem
Learn the fundamental algorithm to solve the Chinese Postman problem using R. Also explore example images.
Join the DZone community and get the full member experience.Join For Free
Some pre-Halloween post today. It started actually while I was in Barcelona: kids wanted to go back to some store we've seen the first day, in the gothic part, and I could not remember where it was. And I said to myself that would be quite long to do all the street of the neighborhood. And I discovered that it was actually an old problem. In 1962, Meigu Guan was interested in a postman delivering mail to a number of streets such that the total distance walked by the postman was as short as possible. How could the postman ensure that the distance walked was a minimum?
A very close notion is the concept of traversable graph, which is one that can be drawn without taking a pen from the paper and without retracing the same edge. In such a case the graph is said to have an Eulerian trail (yes, from Euler's bridges problem). An Eulerian trail uses all the edges of a graph. For a graph to be Eulerian all the vertices must be of even order.
An algorithm for finding an optimal Chinese Postman route is:
- List all odd vertices.
- List all possible pairings of odd vertices.
- For each pairing find the edges that connect the vertices with the minimum weight.
- Find the pairings such that the sum of the weights is minimized.
- On the original graph add the edges that have been found in Step 4.
- The length of an optimal Chinese postman route is the sum of all the edges added to the total found in Step 4.
- A route corresponding to this minimum weight can then be easily found.
For the first steps, we can use the codes from Hurley and Oldford's Eulerian tour algorithms for data visualization and the PairViz package. First, we have to load some R packages.
Then use the following function from StackOverflow, then consider some networks with 12 nodes.
To plot that network, use:
Then we convert it to some traversable graph by adding 5 vertices as shown below:
We cut those 5 vertices in two parts, and therefore, we add 5 artificial nodes.
We get the following graph where all nodes have an even number of vertices!
Our network is now the following (new nodes are small because actually, they don't really matter, it's just for computational reasons).
Now we can get the optimal path or:
Let us try now on a real network of streets. Like Missoula, Montana.
I will not try to get the shapefile of the city, I will just try to replicate the photography above.
If you look carefully, you will see some problem: 10 and 93 have an odd number of vertices (3 here), so one strategy is to connect them (which explains the grey line).
But actually, to be more realistic, we start in 93, and we end in 10. Here is the optimal (shortest) path, which goes through all vertices.
Now we are ready for Halloween and to go through all the streets in the neighborhood!
Published at DZone with permission of Arthur Charpentier, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.