Bird
Raised Fist0

What is the space complexity of Kahn's Algorithm for topological sorting a graph with V vertices and E edges?

medium🪤 Complexity Trap Q6 of Q15
Topological Sort - Course Schedule II (Topological Order)
What is the space complexity of Kahn's Algorithm for topological sorting a graph with V vertices and E edges?
AO(V^2) due to adjacency matrix representation
BO(E) only for the adjacency list
CO(V + E) for adjacency list, in-degree array, and queue
DO(V) only for the in-degree array and queue
Step-by-Step Solution
Solution:
  1. Step 1: Consider graph representation

    Adjacency list uses O(V + E) space, adjacency matrix uses O(V^2).
  2. Step 2: Consider auxiliary structures

    In-degree array and queue use O(V) space.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Adjacency list plus arrays -> O(V + E) [OK]
Quick Trick: Adjacency list plus arrays -> O(V + E) space [OK]
Common Mistakes:
MISTAKES
  • Forgetting adjacency matrix uses O(V^2)
  • Assuming adjacency list always used
Trap Explanation:
PITFALL
  • Candidates forget that adjacency matrix representation drastically increases space.
Interviewer Note:
CONTEXT
  • Tests awareness of graph representation impact on space.
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