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