Data Structures & AlgorithmsModerateTCS NQT · graph theory

Problem: Why does BFS find shortest paths in an unweighted graph in O(V + E) time?

  1. A. It relaxes every edge repeatedly until no change
  2. B. It sorts edges by weight first
  3. C. It needs a priority queue with decrease-key
  4. D. It explores vertices in nondecreasing distance order, and each vertex and edge is processed once

Correct answer: D. It explores vertices in nondecreasing distance order, and each vertex and edge is processed once

Queue order keeps distances nondecreasing, so the first time a vertex is reached gives its shortest distance. Each vertex enters the queue once and each edge is scanned once.

More Coding Interview Problems questions

Exams that ask this topic