简体   繁体   中英

Longest path on weighted undirected graph

I know this is exponential. I already implemented a method to find the shortest path using Dijkstra's algorithm. Is it possible to modify the method to find the longest path instead? If I make all the weights negative, shouldn't this work. All the weights on my current graph are positive. Also there should be no repeating paths.

I know the Bellman Ford algorithm works with negative weights, but im hoping I can just modify my existing shortest path method.

If the graph is undirected, then the longest path has infinite length, because you can visit an edge forward and backward as many times as you want. Therefore you should put some more conditions, like : a node can only be visited once, or the graph must be directed.

Making all weights negative and running Dijkstra will make infinite loop. It is in fact equivalent to what I just explained above.

For more information, I invite you to read about these : http://en.wikipedia.org/wiki/Topological_sorting http://en.wikipedia.org/wiki/Travelling_salesman_problem

Good luck !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM