1

If I am right, the problem of single source longest path for any general graph is NP-hard.

Can we negate all the edge weights of a DAG and run Dijstra's or Bellman Ford algorithm to get single source longest path?

If yes, Dijstra's can't run on a graph with negative edge weights, then how come it gives us a result will all negative edge weights?

Berthur
  • 4,300
  • 2
  • 14
  • 28
  • 1
    https://stackoverflow.com/questions/37253739/intuition-behind-the-algorithm-to-compute-single-source-shortest-path-for-every/37266168#37266168 – Matt Timmermans Jul 18 '22 at 17:09

1 Answers1

1

Yes, you are right. Your proposed solution can be used to find a longest path in a DAG.

However, notice that the longest path problem for a DAG is not NP-hard. It is NP-hard for the general case where the graph might not be a DAG. There is some interesting, relevant discussion on Wikipedia's article on the Longest path problem.

As for your second question, Dijkstra's algorithm is defined for non-negative edge weights. The implementation might give you a result anyway, but it is no longer guaranteed to be correct.

Berthur
  • 4,300
  • 2
  • 14
  • 28
  • "For a DAG, the longest path from a source vertex to all other vertices can be obtained by running the shortest-path algorithm on −G.'' This is what wikipedia link says. Does your answer mean that the shortest path algorithm that should be applied can be only Bellman Ford's and not Dijstra's? – Sumukh Bhat Jul 18 '22 at 14:04
  • 1
    Sumukh Yes, with Bellman-Ford you can find the shortest path on -G and solve your problem. You can not use Dijkstra's out-of-the-box, because Dijkstra's algorithm is not guaranteed to work when there are negative edge weights present. However, there are modifications you can do to make use of Dijkstra's algorithm if you want (see https://en.wikipedia.org/wiki/Johnson%27s_algorithm) – Berthur Jul 18 '22 at 14:09