Reputation: 987
There is a given set of cities lets say.. A,B,C,D,E,F,G. The problem is to find the minimum cost path that covers the cities A,B,C,F. It is essential that the path covers the cities A,B,C,F. The path can, (but does not have to) go through any of the other given cities (D,E,G). Repeating a path is allowed. The path should start and end at A. How should i go about tackling a problem along similar lines?
Upvotes: 1
Views: 3072
Reputation: 14224
That's a variant of Travelling Salesman Problem (TSP) in disguise.
You can see that, if you mark every city as "needed to be covered" (I'll call those "interesting" henceforth). The variant of TSP, where you are allowed to visit a node more than once, is still NP-complete.
So, knowing that the complexity of every exact solution to your problem would be exponential in the number of interesting cities, you can procede as follows:
First, precompute the shortest paths between interesting cities. This can be done with Dijkstra's algorithm run from every interesting city or Floyd-Warshall algorithm. Then either try every permutation of the order of visiting interesting cities; or use some existing TSP solver or heuristic algorithm.
So the simplest implementation goes like this:
If you have V total cities and N interesting cities, the runtime of this implementation will be about O(V3 + N!·N)
Upvotes: 3