# tsp_random

tsp_random, a Python code which seeks a solution of the Traveling Salesperson Problem (TSP), by accepting a table of city-to-city distances, and randomly generating round trips that visit every city, returning the tour of shortest length.

The user must prepare a file beforehand, containing the city-to-city distances. The program will request the name of this file, and then read it in as a matrix d. An example of such a file is:

```        0  3  4  2  7
3  0  4  6  3
4  4  0  5  8
2  6  5  0  6
7  3  8  6  0
```
The distance file d should be square, symmetric, and have a zero diagonal.

A tour of n cities can be represented as a permutation p on the integers 1 through n. The cost of the tour, that is, the length, is the sum

```        cost = sum ( 1 <= i <= n ) ( d(p(i),p(i+1)) )
```
where p(n+1) is understood to mean p(1).

In the sampling method, we simply generate sample_num permutations, each of which represents a tour, and return the permutation which has observed to have the shortest length. Since, except for small problems, there are typically an enormous number of possible tours, this method is unlikely to produce optimal results, but it may give some idea of the range of length of typical tours.

### Languages:

tsp_random is available in a MATLAB version and an Octave version and a Python version.

### Related Data and Programs:

change_diophantine, a Python code which sets up a Diophantine equation to solve the change making problem, which counts the number of ways a given sum can be formed using coins of various denominations.

change_dynamic, a Python code which uses dynamic programming to solve the change making problem, which counts the number of ways a given sum can be formed using coins of various denominations.

change_greedy, a Python code which uses the greedy method to seek a solution to the change making problem, which tries to match a given amount by selecting coins of various denominations.

change_polynomial, a Python code which uses a polynomial multiplication algorithm to count the ways of making various sums using a given number of coins.

combo, a Python code which includes many combinatorial routines.

football_dynamic, a Python code which uses dynamic programming to count the ways of achieving a given score in football.

knapsack_01_brute, a Python code which uses brute force to solve small versions of the 0/1 knapsack problem;

knapsack_greedy, a Python code which uses a greedy algorithm to estimate a solution of the knapsack problem;

mcnuggets, a Python code which counts M(N), the number of ways a given number N of Chicken McNuggets can be assembled, given that they are only available in packages of 6, 9, and 20.

partition_brute, a Python code which uses a brute force method to find solutions of the partition problem, in which a set of integers must be split into two subsets with equal sum.

partition_greedy, a Python code which uses a greedy algorithm to seek a solution of the partition problem, in which a given set of integers is to be split into two groups whose sums are as close as possible.

satisfy_brute, a Python code which uses brute force to find all assignments of values to a set of logical variables which make a complicated logical statement true.

subset_sum, a Python code which seeks solutions of the subset sum problem.

subset_sum_brute, a Python code which uses brute force to solve the subset sum problem, to find a subset of a set of integers which has a given sum.

tsp_brute, a Python code which reads a file of city-to-city distances and solves the traveling salesperson problem, using brute force.

tsp_greedy, a Python code which reads a file of city-to-city distances, picks a starting city, and then successively visits the nearest unvisited city.

### Reference:

1. Gerhard Reinelt,
TSPLIB - A Traveling Salesman Problem Library,
ORSA Journal on Computing,
Volume 3, Number 4, Fall 1991, pages 376-384.

### Source Code:

• five.txt, a 5 city distance matrix, whose minimal tour has length 19.
• fifteen.txt, a 15 city distance matrix, whose minimal tour has length 291.
• fortyeight.txt, a 48 city distance matrix, whose minimal tour has length 33,523.
• seventeen.txt, a 17 city distance matrix, whose minimal tour has length 2,085.

Last revised on 29 October 2022.