Data Structures & AlgorithmsVery hardICPC regional · graph theory

Problem: Which statement about shortest paths in a weighted directed acyclic graph (DAG) is correct?

  1. A. Dijkstra must be used, which is O(E log V), because topological order fails with any negative weight
  2. B. Bellman-Ford is needed since DAGs can contain negative cycles
  3. C. Shortest paths from one source can be found in O(V + E) by relaxing vertices in topological order, even when some edge weights are negative
  4. D. Only BFS works, because weights are ignored in a DAG

Correct answer: C. Shortest paths from one source can be found in O(V + E) by relaxing vertices in topological order, even when some edge weights are negative

Topological order processes each vertex after all its predecessors, so every vertex's distance is final when relaxed. The O(V + E) bound holds even for negative weights because no cycles exist.

More Coding Interview Problems questions

Exams that ask this topic