Deniz Cetinalp
Deniz Cetinalp

Reputation: 911

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.

Upvotes: 1

Views: 4578

Answers (1)

Brainless
Brainless

Reputation: 1758

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 !

Upvotes: 4

Related Questions