Brown Analytics, LLC

A traveling salesperson heuristic in NlogN time

By repurposing a common machine learning algorithm, we can get a fast solution to a notoriously difficult problem.

We can use KMeans clustering to divide our points recursively. If K = 4, we start with a square that divides all points into four quadrants. Each vertex can then be divided into four clusters, and we find the shortest path that integrates those four clusters into the existing square. The process repeats until the clusters ultimately converge into the points themselves. The KMeans algorithm is O(N), and it’s repeated an average of log_{K}(N) times.

I ran a few tests on files from the University of Waterloo’s TSP database, and this algorithm offered solutions which were often within 20 percent of the estimated lowest bound.

Country | N | Lower Limit | RC Solution | % Inc |
---|---|---|---|---|

Argentina | 6,723 | 837,377 | 997,901 | 19.2 |

Burma | 33,708 | 959,011 | 1,132,858 | 18.1 |

Canada | 4,663 | 1,290,319 | 1,546,099 | 19.8 |

China | 71,009 | 4,565,452 | 5,365,123 | 17.5 |

Djibouti | 38 | 6,656 | 7,148 | 7.4 |

Egypt | 7,146 | 172,350 | 211,163 | 22.5 |

Ireland | 8,246 | 206,171 | 241,133 | 17 |

Finland | 10,639 | 520,527 | 610,253 | 17.2 |

Greece | 9,882 | 300,899 | 351,438 | 16.8 |

Honduras | 7,103 | 176,940 | 203,458 | 15 |

Italy | 16,862 | 557,315 | 659,180 | 18.3 |

Japan | 9,847 | 491,924 | 588,964 | 19.7 |

Kazakhstan | 9,976 | 1,061,387 | 1,267,801 | 19.4 |

Luxembourg | 634 | 11,340 | 12,969 | 14.4 |

Morocco | 14,185 | 427,246 | 505,261 | 18.3 |

Oman | 1,979 | 86,891 | 112,025 | 28.9 |

Nicaragua | 2,373 | 96,132 | 111,163 | 15.6 |

Panama | 4,951 | 114,831 | 135,980 | 18.4 |

Qatar | 194 | 9,352 | 10,494 | 12.2 |

Rwanda | 866 | 26,051 | 29,765 | 14.3 |

Sweden | 24,978 | 855,597 | 1,009,072 | 17.9 |

Tanzania | 6,117 | 394,609 | 464,427 | 17.7 |

Vietnam | 22,775 | 569,288 | 669,286 | 17.6 |

Western Sahara | 29 | 27,603 | 31,716 | 14.9 |

Yemen | 7,663 | 238,314 | 286,335 | 20.2 |

Zimbabwe | 929 | 95,345 | 110,531 | 15.9 |

Though, instead of taking hours or days to complete, each were solved in less than 5 minutes. The time to complete each of these countries are plotted on the following chart:

This algorithm has not been compared to other NlogN algorithms (suggestions welcome); however, it has been compared to the Smallest Insertion (SI) algorithm, which take N² time. Running 1000 tests on random nodes when N=100, recursive clustering is better than SI more than 59 percent of the time, despite SI taking considerably longer.

My testing can be found in this Jupyter notebook.

Here is a link to the code on GitHub, and here are three high-level functions the program offers:

import recursive_clustering as rc # Create 100 random points and draw solution with K = 4 rc.solve_random(N=100, K=4) # Given a numpy array, draw the solution rc.solve_array(all_nodes, K=4) # given a two-column CSV of X and Y, draw the solution rc.solve_file('testFiles/usa115475.csv', K=5, draw=False)

All these functions return a dictionary, the tour length, and the wait time. The dictionary has the following structure:

path_d = { ID0: { 'center': [X, Y], 'connections': [ID#, ID#], 'subnodes': [[x, y]....[x, y]], 'hasBeenSplit': True }, ID1: ... }

This dictionary contains all clusters and their subnodes. If you wanted to access only the final nodes, you’d look only for keys whose subnodes list has a length one or haven’t been split. Or you can go to the last ID and follow the connections in a circle. Suggestions for better implementations are welcome.

The algorithm considers all possible permutations of K when it integrates the new K clusters into the existing shape. With that in mind, a larger K can very quickly become slower than N². For points between 1,000 and 10,000, K = 5 seems to offer the shortest path. K = 5 is also consistently faster than all other K values, probably because it’s 120 permutations are outweighed by a faster convergence.

Developing some kind of statistical model to find the optimal value of K given the number of subnodes might be a straightforward, worthwhile area of research going forward. Currently K is static throughout the whole process; however, my intuition tells me that altering K as a function of its subnodes would decrease tour lengths and runtime.

Additionally, although I haven’t yet run any tests, this algorithm would theoretically run in NlogK(N) time on higher-dimensional TSP as well.

Originally published on Medium on March 9, 2019.

Questions? Mistakes? Feel free to email me. Check out my other projects.