Jump to content
  • Advertisement
Sign in to follow this  

Variation of traveling salesman

This topic is 2540 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a weighted graph. The problem I am trying to solve is, given any subset of vertices and edges, and a start and end point, to find the exact length of the shortest path which goes through every required vertex and edge at least once. The size is small enough (at most 8 vertices and at most 6 edges) that dynamic programming seems appropriate to me.

However, the problem is, I can't figure out a good way to implement a dynamic algorithm. The problem is that the given points and edges are only a minimal requirement, and the optimal path could potentially visit other vertices and edges as well. So if I have a data structure memoizing the results for any given pair of subsets, a potentially exponential number of entries have to be updated at every step.

So what should I do? How can I solve this problem most efficiently?

Share this post

Link to post
Share on other sites
For dynamic programming, an array is as efficient as it gets (given sufficient memory, obviously).

While I would suggest top contestants from Google's CodeJam, their code tends to be unreadable. The notorious fib example:
int value[MAX];

// init values to, say, -1

value[0] = 0; // initial state
value[1] = 1;

int fib(int n) {
if (value[n] == -1) {
value[n] = fib(n-1) + fib(n-2);
return value[n];

Obvious pitfalls include properly sizing the array, integer overflows and such, but algorithm remains the same.

For n parameters, the array will become n-dimensional. See some very basic examples here.

Share this post

Link to post
Share on other sites

I guess I didn't ask that question very well.


Re-reading, it's TSP. The variations don't change that. And DP solution does indeed require exponential space.

So a better question: what is the most efficient way to solve the problem?[/quote]

Same as for any TSP.

shortest path which goes through every required vertex and edge at least once[/quote]
If it really needs to visit every *edge*, then it's not TSP. It would be Eulerian path. By consequence, all connected vertices would be visited at least once.

And if it visits all edges, then any path will do, since the shortest path is just sum of all edge lengths. Then you can simply determine whether a path exists and emit the sum of edge lengths.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!