开发者

Find the longest path in a directed cyclic graph from a source s to a destination f. Assume no positive weight cycles exists

i have to Find the longest path in a directed cyclic graph from a source s to a destination f. Assume no positive weight cycles exists even though no positive weight cycles exist, cycles of 0 or negative weights do exist. Can someone suggest an algorithm for finding the longest path in this开发者_如何学运维 case. please cite source if possible.

thanks


Just negate your edge weights and run a shortest path algorithm (e.g., Bellman-Ford).

Zero-weight cycles could be an issue. You'll need to break ties on your paths by picking the shortest one (in length, not in weight). One way to do that is to make your weights be a pair (-(original weight), 1), add them pairwise, and do lexicographic ordering.

See also Longest Path between two vertices


I am not sure if this will work (need to check it) but... you can do something similar to:

Let min = min weight on the graph.
max = max weight on the graph.
Set new weights for all edges = wNew(e) = max - (wOld(e)-min).

Now there are negative wights and the weights are in reverse order (meaning if w(e1) was bigger than w(e2) it is now smaller).

What if we now search for the shortest path?


If you got a DCG, you can just loop forever before you get to your target, that would be the "longest path". In that case, the question is incomplete, and the algorithm probably looks different depending on the specifics.

This sounds like homework.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜