I want to implement the Travelling Salesman Problem (Dynamic Programming) in C. I have the following pseudocode:
** Ignoring the base cases**
** C[i][j] is cost of the edge from i to j**
for m = 2,3,4..n:
for each set S of size m which is subset of {1,2,3...n}:
for each J in S, j ≠ 1:
A[S][j] = min of { A[S-{j}][k] + C[k][j] } for all k is in S and k ≠ j:
return min of { A[{1,2,3...n},j] + C[j][1] } for all j from 2 to n
A[S][j] stores the shortest path from 1 to j which visits all vertices in S exactly once. (S includes 1 and j).
The time complexity is O(n22n).
My problem is that in this pseudocode they have used sets as array indices and the time complexity indicates that the lookup for a set without an element j (S - {j}
) takes constant time.
What I have thought of is using a 3D array indexed by m,i and j. Where 'i' points to a set at stored in a different array of sets indexed by m,i.
But the problem is that I cannot do the lookup A[S-{j}[k]]
in constant time.
My question is that how do I implement an array indexed by a 'set' without changing the time complexity of the original algorithm.