问题
Given: A directed acyclic graph with weighted edges, where a node can have multiple parents.
Problem: For each child of root node, find a minimum-cost(sum of weights) path from such a child to some leaf which can be reached. A node can only be present in one such min-cost paths.
Example graph:
In the above graph, for node 2, all the available paths are:
2 -> 5
2 -> 1 -> 9 -> 6
2 -> 1 -> 10 -> 6
Among which 2 -> 1 -> 10 -> 6 has minimum cost of 3.5
Similarly, for node 4, all the available paths are:
4 -> 11 -> 8
4 -> 7 -> 10 -> 6
Among which 4 -> 7 -> 10 -> 6 has minimum cost of 3.0
The result currently is:
For node 2, the path is 2 -> 1 -> 10 -> 6 : Cost 3.5
For node 4, the path is 4 -> 7 -> 10 -> 6 : Cost 3.0
For node 3, there is no such path.
I have written a code which does this. Now, if such min-cost paths didn't have any node in common, the algorithm would stop and give me min-cost paths for all children of root node.
However, if there exists a node in common, I have to retain it only in one of them. The reason is that normally such multi-parent nodes are due to noisy data. A node is supposed to belong to only one parent. I am trying to keep such a node in a path which has minimum cost. So here, node 10 belongs to min-path of node 4 which has cost of 3.0, compared to min-path of node 2 which has cost of 3.5. Same logic with node 6 too. So, I will just compare the costs to dis-associate some of the multi-parent nodes. Dis-association doesn't mean the edge will be removed. All I do is that save best parent for each node within node's data-structure. For example, Node 10 will have an entry saying "best parent is node 7" and Node 6 will have an entry "best parent is node 10". I can actually remove the edge itself but I may want the whole graph structure to remain intact for any future computations.
So, the logic looks like this:
Do:
For each child node of the root:
Find out min-cost path. Store that path and the cost.
If conflicting paths exist:
Compare the costs of conflicting paths and save "best parent" for each node.
While there were conflicting paths;
Questions:
Does this logic makes sense? I am worried that this iterative way of eliminating conflicts may not converge for some graphs. For example, while re-calculating the min-path for node 2, if 2 -> 5 is found to be the min-path now and assume if node 5 is being used in some other node's min-path during first iteration, then I would have to re-assign "best parent" for node 5 as node 2 and re-iterate. In a nut-shell, everytime I try to fix some node's min-path, I may change the other's. Can such an algorithm converge to some solution? If yes, what will its complexity be?
Is there a way to eliminate such conflicts before computing the min-cost paths in the first place?
回答1:
It's dynamic programming.
First reverse all the edges to make a new graph, we call it newG.
- In newG, the node without parent has the value 0.
- for every node which it have parents in newG, calculate it's parent's value, then choose the minimal value parent, it must be the part of the result.
- when ask the path from the origin gtaph, the answer is the same in the newG.(may be the edges in the answer is in reverse order).
Time O(n)
来源:https://stackoverflow.com/questions/25005850/directed-acyclic-graph-with-multi-parent-nodes