Shortest path through a given vertex
Moderator: Board moderators
Shortest path through a given vertex
How to find a shortest simple path from v to u that passes
through a vertex w
through a vertex w
mf can you explain how to reduce this problem to min-cost max-flow ?
Giorgi Saghinadze
http://acm.uva.es/problemset/usersjudge.php?user=32393
http://acm.uva.es/problemset/usersjudge.php?user=32393
For each vertex x of the original graph, add vertices x_in and x_out to the flow network, and an edge (x_in, x_out) of capacity 1 and cost 0. (This is a standard trick to add capacities on vertices.)
For each edge (x, y) of the original graph of cost w, add edges (x_out, y_in) and (y_out, x_in) of cost w and capacity 1 to the flow network.
Add a source vertex s, and sink t to the network, and edge (s, w_out) of cost 0, capacity 2, and edges (u_in, t), (v_in, t) of cost 0, capacity 1.
And finally try to find a minimum-cost maximum-flow between s and t. If its value is 2, then the solution is possible, and the flow cost is the cost of shortest path from u to v thru w.
For each edge (x, y) of the original graph of cost w, add edges (x_out, y_in) and (y_out, x_in) of cost w and capacity 1 to the flow network.
Add a source vertex s, and sink t to the network, and edge (s, w_out) of cost 0, capacity 2, and edges (u_in, t), (v_in, t) of cost 0, capacity 1.
And finally try to find a minimum-cost maximum-flow between s and t. If its value is 2, then the solution is possible, and the flow cost is the cost of shortest path from u to v thru w.
There's no way to go through w explicitly, that would only give flow of 1mf wrote:That's not going to work, max flow could give you two disjoint paths: from w to itself, and from u to v.sclo wrote:we need to have a connection to sink at w, and connection to source at w.
What I mean is this:
source->u_in->u_out->w_in->sink
source->w_out->v_in->v_out->sink
additional arcs are:
u_out->v_in
Notice that w_in->w_out is not in the graph.
So how can this problem be fixed?mf wrote:There could be a directed cycle in graph, which includes w, and if you have a source at w_out, and a sink at w_in, max flow could push flow from w_out to w_in along this cycle. And if there's also some other path from u to v, max flow will find it too, and give a flow of value 2.