Data Structures & AlgorithmsEasyInfosys · Capgemini pattern

Which algorithm finds shortest paths (by number of edges) from one source in an UNWEIGHTED graph in O(V + E) time?

  1. A. Breadth-first search
  2. B. Depth-first search
  3. C. Prim's algorithm
  4. D. Floyd–Warshall

Correct answer: A. Breadth-first search

BFS explores vertices in increasing distance order, so the first time it reaches a vertex is along a shortest path. DFS can reach a vertex by a long path first; Prim builds an MST, and Floyd–Warshall is O(V³) for all pairs.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic