Skip to content
Official PYQ · 2026

GATE CS 2026 Set 2 Q27 — Shortest Paths (Dijkstra, Bellman-Ford)

Explain with AI

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}?

Choose one option
View worked solution

Answer: A

The single-source shortest path (SSSP) problem on a weighted Directed Acyclic Graph (DAG) can be solved efficiently using topological sorting in Θ(m+n)\Theta(m + n) time.

The algorithm proceeds as follows:

  1. Topological Sort: Find a topological ordering of the vertices of GG. This takes Θ(m+n)\Theta(m + n) time using Depth-First Search (DFS) or Kahn's algorithm.
  2. Initialization: Set the distance to the source vertex ss as dist[s]=0dist[s] = 0, and for all other vertices u≠su \neq s, set dist[u]=∞dist[u] = \infty. This takes Θ(n)\Theta(n) time.
  3. Relaxation: Process each vertex uu in the topological order. For each outgoing edge (u,v)(u, v) with weight w(u,v)w(u, v), update the distance:dist[v]=min⁡(dist[v],dist[u]+w(u,v))dist[v] = \min(dist[v], dist[u] + w(u, v))Since each vertex and edge is processed exactly once, this step takes Θ(m+n)\Theta(m + n) time.

Therefore, the total worst-case time complexity of the fastest algorithm is Θ(m+n)\Theta(m + n).

Correct Option: A

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Shortest Paths (Dijkstra, Bellman-Ford) questions