3

Given a weighted undirected graph G = (V, E) and a set of nodes P.

Given two nodes, n1 and n2.

I want to find two separate (non-overlapping) paths from n1 to n2, so that the sum of the weights of the two paths is minimum. And I paraphrase the problem to what the title describes, that is the minimum weighted cycle containing n1 and n2.

Obviously it's not correct to find a first minimum weighted path p1 from n1 to n2 and then remove the edges in p1 from the graph and then find a second minimum weighted path p2.

How can I find such a cycle?

Tian Wang
  • 31
  • 6
  • What have you got so far? – fuesika Sep 07 '15 at 09:43
  • @fuesika no idea yet..:( – Tian Wang Sep 07 '15 at 11:25
  • @fuesika It's a typical [min cost max flow](https://en.wikipedia.org/wiki/Minimum-cost_flow_problem) problem. You can also search for the answer of the [problem](https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1747) which describe a same situation as my problem. – Tian Wang Sep 08 '15 at 03:21

1 Answers1

1

The problem can be solved using flow networks. See the Edmonds-Karp algorithm: it involves computing augmenting paths at each step.

Find an augmenting path, like described in the Edmonds-Karp algorithm, using a breadth first search (if your graph is weighted, use Bellman-Ford).

Then, find another augmenting path in the same way. Eliminate the edges that appear in both paths (the flow algorithm will actually take care of this for you), and that will be your cycle. Basically, if you set all your edges to capacity 1, your cycle will consist of the edges you've pushed 1 flow over.


If you don't like the flow-based solution, here's another. It's basically the same thing, just not in terms of flow algorithms.

  1. Find a shortest path n1 -> n2 with any algorithm that can do this (Dijkstra if no negative costs, Bellman-Ford otherwise);

  2. Replace the edges of this shortest path with directed arcs directed from n2 towards n1. For example, if your shortest path is n1 -> x -> y -> z -> n2, replace the edge (n1, x) with the directed arc x -> n1, (x, y) with the directed arc y -> x etc.

    Also negate the costs of these arcs. For example, if (x, y) had cost c, the directed arc y -> x will have cost -c.

  3. Find a shortest path from n1 to n2 in this new graph. You'll have to use an algorithm that works with negative edges now.

  4. Remove the edges that appear in both of the shortest paths. What you're left with is the cycle you're after.

Community
  • 1
  • 1
IVlad
  • 43,099
  • 13
  • 111
  • 179
  • I checked on that and didn't found the exact mapping relationship from my problem to MAX FLOW problem you mentioned. If possible, could you please give us some examples? Thanks! – Tian Wang Sep 07 '15 at 12:00
  • @TianWang have you read the augmenting paths part? What problems are you having implementing the given pseudocode, limiting it to two steps and then removing the duplicate edges? – IVlad Sep 07 '15 at 12:09
  • @lVlad I'm sorry that I use the wrong word like "residual graph" and "cost" which may have misled you to think it has something to do with MAX FLOW problem.. I reedit my question and still can't figure out what "augmented path" has to do with my "least weighted path/cycle" :( – Tian Wang Sep 07 '15 at 12:59
  • @TianWang your problem statement did not mislead me. You want to find the the shortest path from `n1` to `n2` **and** back to `n1`, with no edges in common. This is what I answered. All you have to do is implement the Edmonds-Karp algorithm, with the Bellman-Ford algorithm instead of a Breadth First search. Also, where it says "forever" on Wikipedia, only execute that part twice. This will get you two augmenting paths. Remove the edges they have in common, and you'll be left with the edges of the cycle you're looking for. – IVlad Sep 07 '15 at 13:03
  • I posted an algorithm that doesn't use the words related to flow networks. It's the same thing, but maybe you'll understand it better. – IVlad Sep 07 '15 at 13:11
  • @lVlad Thanks for your detailed explanation and now I understand your algorithm. I'm now seeking for examples to make sure it's correct. I'll let you know my result later. :) – Tian Wang Sep 07 '15 at 13:50
  • 1
    @TianWang you can implement it for this problem: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1747 and see if it works. I got AC with it with 7 years ago. – IVlad Sep 07 '15 at 13:54
  • Actually It's a typical problem of [min cost max flow](https://en.wikipedia.org/wiki/Minimum-cost_flow_problem). – Tian Wang Sep 08 '15 at 03:24