c++ - Finding shortest circuit in a graph that visits X nodes at least once -


even though i'm still beginner, love solving graph related problems (shortest path, searches, etc). faced problem :

given non-directed, weighted (no negative values) graph n nodes , e edges (a maximum of 1 edge between 2 nodes, edge can placed between 2 different nodes) , list of x nodes must visit, find shortest path starts node 0, visits x nodes , returns node 0. there's @ least 1 path connecting 2 nodes.

limits 1 <= n <= 40 000 / 1 <= x <= 15 / 1 <= e <= 50 000

here's example :

enter image description here

the red node ( 0 ) should start , finish of path. must visit blue nodes (1,2,3,4) , return. shortest path here :

0 -> 3 -> 4 -> 3 -> 2 -> 1 -> 0 total cost of 30

i thought using dijkstra find shortest path between x (blue) nodes , greedy picking closest unvisited x (blue) node, doesn't work (comes 32 instead of 30 on paper). later noticed finding shortest path between pairs of x nodes take o(x*n^2) time big nodes.

the thing find circuits eulerian circuit allows visiting each node once (and don't need that). solveable dijkstra or there other algorithm solve this?

here solution fast enough:
1)run shortest path search algorithm every blue node(this can done in o(x * (e log n))) compute pairwise distances.
2)build new graph 0 vertex , blue vertices only(x + 1 vertices). add edges using pairwise distances computed during first step.
3)the new graph small enough use dynamic programming solution tsp(it has o(x^2 * 2^x) time complexity).


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -