I want to solve the TSP problem using a dynamic programming algorithm in Python.The problem is:
- Input: cities represented as a list of points. For example, [(1,2), (0.3, 4.5), (9, 3)...]. The distance between cities is defined as the Euclidean distance.
- Output: the minimum cost of a traveling salesman tour for this instance, rounded down to the nearest integer.
And the pseudo-code is:
Let A = 2-D array, indexed by subsets of {1, 2, ,3, ..., n} that contains 1 and destinations j belongs to {1, 2, 3,...n}
1. Base case:
2. if S = {0}, then A[S, 1] = 0;
3. else, A[S, 1] = Infinity.
4.for m = 2, 3, ..., n: // m = subproblem size
5. for each subset of {1, 2,...,n} of size m that contains 1:
6. for each j belongs to S and j != 1:
7. A[S, j] = the least value of A[S-{j},k]+the distance of k and j for every k belongs to S that doesn't equal to j
8.Return the least value of A[{1,2..n},j]+the distance between j and 1 for every j = 2, 3,...n.
My confusions are:
How to index a list using subset, that is how to implement line 5 in the pseudo-code efficiently.
You can encode sets as integers: i'th bit of the integer will represent the state of i'th city (i.e. do we take it in the subset or not).
For example, 3510 = 1000112 will represent cities {1, 2, 6}. Here I count from the rightmost bit, which represents city 1.
In order to index a list using such representation of a subset, you should create 2D array of length 2n:
This comes from the fact that with n-bit integer you can represent every subset of {1, 2, ..., n} (remember, each bit corresponds to exactly one city).
This representation gives you a number of nice possibilities:
This is how I would implement your pseudocode (warning: no testing was done):
Besides having all needed functionality to implement your pseudocode, this approach is going to be faster than with tuples\dicts.