Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 267
Ari Avatar asked Nov 20 '18 18:11

Ari


People also ask

Is finding the longest path in A graph NP-hard?

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.

Is longest path in DAG NP-hard?

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.

Is the shortest path problem P or NP?

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).

Is finding shortest path NP?

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.


2 Answers

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
like image 137
Berthur Avatar answered Nov 15 '22 06:11

Berthur


  • 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.
like image 30
KGhatak Avatar answered Nov 15 '22 06:11

KGhatak