0

this is the recursion formula for problem :

C(i,S) = min { d(i,j) + C(j,S-{j}) }

In fact, when I tried to implement it as a code, the following code came to my mind:

int TSP(i, S){
    if(S.size == 0)
    return dist(start_vertex,i)
min = inf
cost = inf
  for(int j=0;j<S.size;j++)
     {
      cost = dist(i,S[j])+TSP(j,S-{j});
      if(cost < min)
       min = cost;
      }
global_cost+=min;
return min;
}

Because this for compares n times to find the minimum, it means its recursion as:

T(n) = nT(n-1)+n ==> T(n) = O(n!)

Because each step we compare to find the minimum size of size S is, of course, the code is factorial. So what does it have to do with the subset and So why the complexity of time in the form of (n^2*2^n)? And what is the proof of its time complexity?

Satar
  • 37
  • 7
  • dp table size is n * 2^n and in each f-tion call we spend n time in a for loop, hence time compleixty is n * n * 2^n – Photon Dec 12 '20 at 11:26
  • @Photon How is a 2^n obtained? what is dp table? – Satar Dec 12 '20 at 11:55
  • just google some articles on TSP dp approach, your pseudo code is not dp its just recursion, for dp you need to use table to save previous computations results so they do not get recomputed every time – Photon Dec 12 '20 at 12:16
  • @Photon is this code recomputed every time? That is, it recalculates the duplicate mode it has already calculated? Can you give an example? – Satar Dec 12 '20 at 15:27
  • your code is not dp at all it just picks first node, then repeats for the set of remaining nodes so ofc complexity will be factorial, dynamic programming uses different approach, theres lots of good explanations online that you can watch for better understanding (i.e. https://www.youtube.com/watch?v=-JjA4BLQyqE) – Photon Dec 12 '20 at 15:33

0 Answers0