Let be a weighted directed acyclic graph with edges and vertices. Given and a source vertex in , which one of the following options gives the worst case time complexity of the fastest algorithm to find the lengths of shortest paths from to all vertices that are reachable from in
GATE CSE 2026Algorithms › Growth of Functions & Complexity AnalysisVerified
GATE CSE 2026 PYQ: Let G G G be a weighted directed acyclic graph with m m m edges and n n n vertices. Given G G G and a source…
- ACorrect answer
- B
- C
- D
Answer
(A) Θ ( m + n ) \Theta(m+n) Θ ( m + n )
Approach
A directed acyclic graph has a topological ordering. Shortest paths from a single source can be computed by first topologically sorting the vertices, then relaxing each edge exactly once in that order. No priority queue is needed because vertices are processed in an order that guarantees all predecessors of a vertex have already been finalized.
Because G is a DAG, we can topologically sort it in linear time and then relax edges once in topological order. This avoids the extra log factor of Dijkstra or the repeated relaxations of Bellman-Ford, giving $\Theta(m+n)$ worst-case time. Therefore option A is correct.
Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.