Longest path in graph

后端 未结 4 1656
一生所求
一生所求 2021-01-24 00:24

Since last 2 days,i\'m trying to find some logic for calculating longest path in graph.I know i can find it easily for DAGs and in general it is polynomial time algorithm.Formal

4条回答
  •  遥遥无期
    2021-01-24 01:07

    Dijkstra's can't be used on graphs with negative weights - Wiki article on Dijkstra's

    Dijkstra's algorithm, conceived by Dutch computer scientist Edsger Dijkstra in 1956 and published in 1959,1 is a graph search algorithm that solves the single-source shortest path problem for a graph with non-negative edge path costs ...

    So you can't negate all edge weights and use Dijkstra's, what you can do is negate all edge weights and use Bellman-Ford algorithm - Wiki article on Bellman-Ford

    The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all of the other vertices in a weighted digraph.1 It is slower than Dijkstra's algorithm for the same problem, but more versatile, as it is capable of handling graphs in which some of the edge weights are negative numbers

    EDIT: The shortest path (with the most negative value) is then the longest path in your original graph.

    NOTE: if you have positive cycles in your graph, you will not find a solution since the longest path doesn't exist in such a graph.

提交回复
热议问题