Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
1.40/5 (4 votes)
See more:
Here is the pseucode for TSP using dynamic programming, my problem is i don't know how to implement D[n][subset of v - {v1}], or i don't know how to implement the loops in real code:

C++
void travel (int n,
            const number W [] [],
            index P [] [],
            number & minlength)
{
   index i, j, k;
   number D [1 .. n] [subset of V - {v1}];

   for (i = 2; i <= n; i++)
          D [i] [⊘] = W[i] [1];

   for (k = 1; k <= n - 2; k++)
      for (all subsets A ⊆ V - {v1} containing k vertices)
           for (i such that i ≠ 1 and vi is not in A)
           {
               D [i] [A] = minimum (W [i] [j] + D [j] [A - {vj}]);
               j: [vj ∊ A
               P[i] [A] = value of j that gave the minimum;
          }
     D [1] [V - {v1}] = minimum (W[1] [j] + D[j] [V - {v1, vj}]);
     2 ≤ j ≤ n
     P[1] [V - {v1}] = value of j that gave the minimum;
     minlength = D[1] [V - {v1}];
}
Posted
Updated 12-Nov-15 22:31pm
v3
Comments
Sergey Alexandrovich Kryukov 21-May-15 8:21am    
"I don't know" does not make a question.
—SA

1 solution

If you don't know how to make loops, it means that you no nothing about programming. In turn, it makes you not qualified even for asking questions. Why? Because anyone can read in a manual how to program loops in C++. You did not even try. If you did not do that, it makes doubtful that you can learn how to do it from the answer. But who wants to waste time?

So, please sit down and learn the subject at least to the point where you can ask qualified questions.

http://www.cplusplus.com/doc/tutorial/control[^],
http://www.cplusplus.com/forum/beginner/120231[^].

And so on…

Sorry for not writing a code sample for you; I did it on purpose: to really help you to learn things. Answering such "questions" as yours is often works as an effective block for learning; I don't want it.

—SA
 
Share this answer
 
Comments
rachel3 21-May-15 8:38am    
wow, i don't understand why some ppl enjoy making sarcasm!
i know how to make a loop you didn't even read my question.
and besides if you don't wanna help, then don't bother yourself posting stupid answers!
Sergey Alexandrovich Kryukov 21-May-15 8:44am    
This is not sarcasm at all. This is the attempt to really help you. There is nothing enjoyable here.

If you know how to make loops, don't write "i don't know how to implement the loops in real code" and don't blame anyone for not reading the code. If you simply asked for clarifications and explained what you are confused with, I would gladly have helped.

—SA
rachel3 21-May-15 8:50am    
when i say i don't know how to implement THE loops in real code i mean these loops..
for (all subsets A ⊆ V - {v1} containing k vertices)
for (i such that i ≠ 1 and vi is not in A)
Philippe Mori 21-May-15 12:44pm    
The easy way would be to add extra conditions at the beginning of the block to skip any item you don't want to process.
Sergey Alexandrovich Kryukov 21-May-15 12:52pm    
Right...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900