algorithmgraph-theory

Why finding the longest path in a graph is NP-hard


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:

Questions:

  1. Is it correct to say if there is no negative cycle, the problem of finding longest path is not NP-hard?

  2. 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?

  3. If so, is it more accurate to say finding the longest simple path in a graph is NP-hard?

  4. 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://medium.com/hackernoon/shortest-and-longest-path-algorithms-job-interview-cheatsheet-2adc8e18869


Solution

  • 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:

    1. No. If there is a negative cycle in -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.
    2. Indeed! (If the graph is undirected.)
    3. Yup
    4. Yup