1

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.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
2147483647
  • 1,177
  • 3
  • 13
  • 33
  • 1
    This is why it's such an incredibly bad idea to use math theory as basis to build effective, readable computer programs. Look at the textual description of the problem, then think of how to implement the actual algorithm. And trash that pseudo code, it does more harm than good. – Lundin Feb 22 '13 at 07:19
  • It does sound like you are trying to re-invent [Prim's algorithm](http://en.wikipedia.org/wiki/Prim%27s_algorithm). – Lundin Feb 22 '13 at 07:23
  • @Lundin This is a **way** more efficient solution to trying to brute force TSP, and anything more efficient is an approximation. – Bernhard Barker Feb 22 '13 at 07:24
  • @Dukeling What is? I have seen no C code yet. It would be silly to compare various algorithms before they are written, tested and benchmarked. – Lundin Feb 22 '13 at 07:26
  • 1
    @Lundin This is a well-known algorithm for solving TSP. Simply looking at the big-O complexity clearly shows the difference. – Bernhard Barker Feb 22 '13 at 07:29
  • @Lundin this is not invented by me its called the Held–Karp algorithm, Prim's Algorithm runs in polynomial time TSP is an NP complete so there is no solution better than exponential time. – 2147483647 Feb 22 '13 at 07:31
  • Well if it is an existing algorithm, surely there already exists effective implementations of it. Did you try Google? – Lundin Feb 22 '13 at 07:48
  • @Lundin I had found one here on stackowerflow, but i did not understand his implementation (where he was using the method Dukeling described), after reading Dukeling's answer I understod it. – 2147483647 Feb 22 '13 at 08:12

1 Answers1

3

Let each path be represented by a binary string, where each bit represents whether or not a city is visited.

So

(123456)
 011001

means city 2, 3 and 6 are visited.

You use the above as array index.

When you want to look-up the path without a city, just set that bit to 0 and use the output as index.

The first city will always be visited so you really don't need a bit for that city.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
  • We will require a character array for storing bits which is again using a char[] for indexing an array, but array indices can only be integers so it is as same as using a set(non integer object) as an array index – 2147483647 Feb 22 '13 at 07:21
  • 1
    @A.06 You don't use a character array, you use an `int` or `long` (assuming you're limited to 32 or 64 cities) (and mod (`%`) and bit-shift (`<<`) to set the bits). – Bernhard Barker Feb 22 '13 at 07:22
  • Or you use a character array but use bitwise operators to access individual bits, then there is no restriction over how many items there can be. – Lundin Feb 22 '13 at 07:29
  • 1
    Even 32 cities is a bit large problem, requiring at least ~512GB of memory. – Aki Suihkonen Feb 22 '13 at 08:03
  • It wil run really slowly for 32 cities, i dont intend to run it for more than 20 cities – 2147483647 Feb 23 '13 at 02:21