Finding the shortest path in a graph between 2 nodes that goes through a subset of nodes

亡梦爱人 提交于 2019-12-10 11:28:57

问题


I'm trying to find out an efficient way of finding the shortest path between 2 nodes in a graph with positive edge costs that goes trough a subset of nodes.

More formally:

Given a graph G = (U, V) where U is the set of all nodes in the graph and V is the set of all edges in the graph, a subset of U called U' and a cost function say:

    f : UxU -> R+  
    f(x, y) = cost to travel from node x to node y if there exists an edge 
              between node x and node y or 0 otherwise,

I have to find the shortest path between a source node and a target node that goes trough all the nodes in U'.

The order in which I visit the nodes in U' doesn't matter and I am allowed to visit a node more than once.

My original idea was to make use of Roy-Floyd algorithm to generate the cost matrix. Then, for each permutation of the nodes in U' I would be computing the cost between the source and the target like this: f(source_node, P1) + f(P1, P2) + ... + f(Pk, target) saving the configuration for the lowest cost and then reconstructing the path.

The complexity for this approach is O(n3 + k!) O(n3 + k*k!), where n is the number of nodes in the graph and k the number of nodes in the subset U', which is off limits since I'll have to deal with graphs with maximum n = 2000 nodes out of which maximum n - 2 nodes will be part of the U' subset.


回答1:


This is a generalization of travelling salesman. If U' == U, you get exactly TSP.

You can use the O(n^2 * 2^n) TSP algorithm, where the exponential factor for full scale TSP (2^n) will reduce to k = |U'|, so you'd get O(n^2 * 2^k).

This has the DP solution to TSP.

http://www.lsi.upc.edu/~mjserna/docencia/algofib/P07/dynprog.pdf




回答2:


Combine the source node s and target node t into a single node z, and define the node set U'' := U' union {z}, with the distances to and from z defined by f(z,x) := f(s,x) and f(x,z) := f(x,t) for all x in U \ {s, t}. Compute shortest paths between all nodes of U'' and let f'(x,y) be the shortest distances, or infinity when there is no appropriate path. Voila, you now have a travelling salesman problem on the complete directed graph with vertices U'' and edge weights f'.



来源:https://stackoverflow.com/questions/7678789/finding-the-shortest-path-in-a-graph-between-2-nodes-that-goes-through-a-subset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!