Variation of traveling salesman

Started by
3 comments, last by Storyyeller 12 years, 6 months ago
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?
I trust exceptions about as far as I can throw them.
Advertisement
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.
I guess I didn't ask that question very well. So a better question: what is the most efficient way to solve the problem?
I trust exceptions about as far as I can throw them.

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


Right...

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.

Although:
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.
Not every edge in the graph. Every edge in a particular subset of edges.
I trust exceptions about as far as I can throw them.

This topic is closed to new replies.

Advertisement