-1

What's a good algorithm that solves the single-source-shortest path problem for a given acyclic (no cyclec) graph in time O(m + n).

My attempt was to do Dijkstra's Algorithm with a fibonacci heap, but that's O(m + nlogn).

amit
  • 166,614
  • 24
  • 210
  • 314
Barney Chambers
  • 2,402
  • 5
  • 34
  • 70

1 Answers1

1

It can be done using topological sort and Dynamic Programming.

First, topological sort the graph. That's O(n+m).

Then, follow the recursive formula:

D(source) = 0
D(u) = infinity    if u is before source in the topological sort
D(u) = min { D(v) + w(v,u) | for each edge (v,u) }

Using DP techniques for the above recursive formula is O(n+m) as well

Since you have a topological sort of the graph, the Dynamic Programming will go in order of the topological sort, and when you process some node - all dependencies have already been calculated.

amit
  • 166,614
  • 24
  • 210
  • 314