简体   繁体   中英

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:

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

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

The confusion here is that the Longest Path Problem generally asks for the longest simple path, ie, 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 ), ie 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
  • First, Longest-path problem is in general NP-Hard (in fact NP-Complete) even when there is no negative edges. I have tried to prove it from basics, here ; also for the proof of NP-complete, you might like to check this one .
  • Second, for various categories of Graphs, scientists have come up with polynomial time solution to this. DAG, and tree are two such kinds.
  • Third, for DAG, while one option is to find shortest distance in a transformed graph, -G (this is mentioned in Sedgewick book, 4th Ed), there is also another alternate, see here . Both run in theta(E+V) time.
  • Finally, for answers to your questions 1 thru 3, I'll stick to Berthur's answer . But for your question 4, note that we do not do a transformation like -G, for any arbitrary graph G. If we at all do so, then it is a DAG. In fact, we often prove longest-path problem as NPC by referring to Hamiltonian path - which has nothing to do with weights, let alone negative weights.

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