Data Structures & AlgorithmsModerateTCS NQT · graph basics

Problem: Which graph algorithm checks whether a directed graph is a DAG in O(V + E)?

  1. A. Prim's algorithm
  2. B. Kahn's algorithm (or DFS with three colours)
  3. C. Bellman-Ford
  4. D. Floyd-Warshall

Correct answer: B. Kahn's algorithm (or DFS with three colours)

A DAG is exactly a graph where Kahn's algorithm processes all vertices or a DFS finds no back edge.

More Coding Interview Problems questions

Exams that ask this topic