Skip to main content

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…

Let GG be a weighted directed acyclic graph with mm edges and nn vertices. Given GG and a source vertex ss in GG, which one of the following options gives the worst case time complexity of the fastest algorithm to find the lengths of shortest paths from ss to all vertices that are reachable from ss in G?{G}?

  1. A
    Θ(m+n)\Theta(m+n)
    Correct answer
  2. B
    Θ(m+nlog⁡(n))\Theta(m+n \log (n))
  3. C
    Θ(nm)\Theta(n m)
  4. D
    Θ(n3)\Theta\left(n^{3}\right)

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.