GATE / CS ARCHIVE

GATE CSE 2026 SET 2 / QUESTION 37

Linear-time shortest paths in a DAG

Algorithms · Graph Algorithms · Full GATE CSE 2026 Set 2 paper

GATE 2026 Set 2, Q37 2 marks · MCQ
Report issue

Graph Algorithms

Question 37, GATE 2026: A weighted directed acyclic graph has n vertices and m edges, with a specified source s. Find the fastest worst-case complexity to compute shortest-path distances from s to reachable vertices: A Θ(m+n); B Θ(m+n log n); C Θ(nm); D Θ(n³).
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Givens and goal

A weighted directed acyclic graph has n vertices and m edges, with a specified source s. Find the fastest worst-case complexity to compute shortest-path distances from s to reachable vertices: A Θ(m+n); B Θ(m+n log n); C Θ(nm); D Θ(n³).

Approach

Use an order in which every predecessor is settled before its successors.

Step-by-step solution

1. A DAG has a topological ordering: each edge u→v goes forward in that order. Compute one using indegrees or DFS in O(m+n) time with adjacency lists.
2. Initialize d[s]=0 and d[v]=∞ for all other vertices. Process vertices in topological order; for each outgoing edge u→v of weight w, set d[v]=min(d[v],d[u]+w) when d[u] is finite.
3. When u is processed, every possible final edge of a path into u comes from an earlier vertex. By induction, all those predecessor distances are final, so d[u] is the shortest distance. Negative edge weights do not break this argument because there are no cycles or backward dependencies.
4. Each vertex is initialized/processed once and each edge relaxed once, totaling O(m+n). Worst-case instances require reading relevant edge weights and producing vertex distances, giving a matching linear lower bound in the standard input model.
5. Thus A is correct. Dijkstra’s priority queue and Bellman-Ford’s repeated passes are unnecessary when acyclicity supplies the processing order.

Original paper, page 25 · Answer key