Bird
Raised Fist0

What is the time complexity of Kahn's Algorithm for finding a topological order in a graph with V vertices and E edges?

medium🪤 Complexity Trap Q13 of Q15
Topological Sort - Course Schedule II (Topological Order)
What is the time complexity of Kahn's Algorithm for finding a topological order in a graph with V vertices and E edges?
AO(V * E) because each vertex processes all edges repeatedly.
BO(V^2) because in-degree updates require scanning all vertices.
CO(V + E) because each vertex and edge is processed exactly once.
DO(E log V) due to priority queue operations for ordering.
Step-by-Step Solution
Solution:
  1. Step 1: Identify operations on vertices and edges

    Each vertex is enqueued and dequeued once; each edge is visited once to decrement in-degree.
  2. Step 2: Sum total work

    Processing all vertices and edges once leads to O(V + E) time complexity.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Linear time in graph size is standard for BFS topological sort [OK]
Quick Trick: Each edge and vertex processed once -> O(V + E) [OK]
Common Mistakes:
MISTAKES
  • Assuming nested loops cause O(V*E)
  • Confusing in-degree updates as scanning all vertices
  • Thinking priority queues are used by default
Trap Explanation:
PITFALL
  • O(V*E) looks plausible if one thinks edges are processed multiple times; O(E log V) is a confusion with Dijkstra's algorithm.
Interviewer Note:
CONTEXT
  • Checks if candidate understands graph traversal complexity and common traps.
Master "Course Schedule II (Topological Order)" in Topological Sort

3 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Topological Sort Quizzes