Longest path between from a source to certain nodes in a DAG

谁都会走 提交于 2019-12-23 12:20:20

问题


How do i find the longest path from single source to all final destinations i.e. For source i1, Give longest path between i1 -> o1 and i1 -> o2.

The legends described in the above graph are as follows: (i1, i2) are start nodes (o1, o2) are end nodes (1-8) are sub graphs The edges may have +ive/-ive weights

The longest paths in this network are in the following order:

Worst path: i1 -> 1 -> 4 -> o1

Then, all paths i1 … -> … o1

Then i1 -> 5 -> 6 -> o2

Need a way to ignore selection of (i1 -> 3) or (3 -> 4) subnetworks even though they are longer than i1 -> 5


回答1:


Wikipedia to the rescue! Their page on this problem indicates that in the general case, your problem is NP-Complete. However, since you have a directed acyclic graph, you can invert all the edge weights and run the Bellman-Ford algorithm to solve it. The B-F algorithm normally computes single-source shortest paths in a graph. With inverted edge weights, it should produce the longest paths.




回答2:


I believe the following will give you the largest number of steps to reach the destination (not the actual path, just the number of steps). This doesn't take edge weights into account. This works by coalescing nodes starting from src until no neighbors are left except the destination node.

longestPath (src, dest) =
    if (neighbors(src) == [dest]) then 1
    else 1 + longestPath(newNode(concat (map neighbors (neighbors src))), dest)


来源:https://stackoverflow.com/questions/598174/longest-path-between-from-a-source-to-certain-nodes-in-a-dag

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