This link mentions:
A longest path between two given vertices s and t in a weighted graph G is the same thing as a shortest path in a graph −G derived from G by changing every weight to its negation. Therefore, if shortest paths can be found in −G, then longest paths can also be found in G.
So why is finding the longest path an NP-hard problem if this transformation can reduce it to the shortest path problem?
After the transformation, we have have these cases:
-G
has a negative cycle, in which case the shortest path in -G
cannot be found-G
does not have a negative cycle, in which case Floy-Warshall or Bellman-Ford algorithm can find the shortest path in -G
in polynomial timeQuestions:
Is it correct to say if there is no negative cycle, the problem of finding longest path is not NP-hard?
Is it correct to say in the presence of negative cycles, there is still a longest simple path
between nodes which is NP-hard to be found?
If so, is it more accurate to say finding the longest simple path in a graph is NP-hard?
If so, because of -G
transformation is it also correct to say that finding the shortest simple path in a graph is also NP-hard?
Edit
This link explains the confusion about longest path problem in more details: https://hackernoon.com/shortest-and-longest-path-algorithms-job-interview-cheatsheet-2adc8e18869
In contrast to the shortest path problem, which can be solved in polynomial time in graphs without negative-weight cycles, the longest path problem is NP-hard and the decision version of the problem, which asks whether a path exists of at least some given length, is NP-complete.
In fact, the Longest Path problem is NP-Hard for a general graph. However, the longest path problem has a linear time solution for directed acyclic graphs. The idea is similar to linear time solution for shortest path in a directed acyclic graph., we use Topological Sorting.
The shortest path on the other hand is a different one, it asks what is the shortest way from point A to point B, and it is in P because there is a polynomial time algorithm that solves it (Dijkstra's algorithm, Bellman-Ford, BFS for non weighted graphs).
Finding shortest paths in time-dependent networks with forbidden waiting is NP-hard. It's weakly NP-hard and pseudopolynomially solvable for integer travel time functions. It's strongly NP-hard for travel time functions on rationals. It's strongly NP-hard for piecewise linear functions with integer breakpoints.
The confusion here is that the Longest Path Problem generally asks for the longest simple path, i.e., the longest path without repeated vertices. For this reason, it can be reduced to the Hamiltonian Path problem, which is known to be NP-hard.
Bellman-Ford and similar algorithms, on the other hand, compute the shortest path in a graph (note: without simple), i.e. vertices can be repeated.
So your four questions:
-G
, a longest path does not exist at all in G
, because you can just continue walking around the cycle forever. A longest simple
path might still exist, but with or without negative cycle, the problem can be reduced to Hamiltonian Path and is therefore still NP-hard.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With