Dijkstra's algorthm modification

匿名 (未验证) 提交于 2019-12-03 09:21:58

问题:

I am aware of the Dijkstra's shortest path algorithm. However, if I were to modify it so that instead of finding the shortest path it would find the longest path using a greedy algorithm. What would I have to do to the code below:

Here is what Im using:

as a compare function to select the correct node in the shortest path version:

 if (Cost(potential_node) > Cost(current_node) + cost(source , current_node)) then  cost (potential_node) = cost(current_node) + cost (source, current_node)

However, to get to the flip side this is not working:

 if (Cost(potential_node) < Cost(current_node) + cost(source , current_node)) then  cost (potential_node) = cost(current_node) + cost (source, current_node)

A bit confused, would really appreciate some feedback

回答1:

The longest path problem is NP-Hard, and thus there is no known polynomial solution.

The suggested modification does not work, because when dijkstra's algorithm marks a node as "closed" it means - there will never be a shorter path to it. The claim is not true when trying to do it for longest path, if you closed a node - it doesn't mean there is no longer path to it.

Recall that this is exactly what we prove on Dijkstra's algorithm at each step (more "relaxations" will not find any shorter path), but if you find a current longest path to a vertex using the midification - it doesn't mean it is indeed the longest one - there might be one longest path not yet explored.


Edit - counter example when it doesn't work (forgive me I am a terrible ascii artist)

        A       /   \      /     \     1       2      /         \   B-----5---> C V = {A,B,C} ;  E = { (A,B,1), (A,C,2), (B,C,5) }

Now, starting from A, this approach will first find C as the "longest path" and close it. From now on - there is no changes to C according to Dijkstra's algorithm, so you will conclude that the longest path from A to C is of length 2, while the path A->B->C is longer.



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