Bird
Raised Fist0

What is the time complexity of the optimal approach that uses the reversed graph and Kahn's algorithm to find eventual safe states in a graph with V vertices and E edges?

medium🪤 Complexity Trap Q13 of Q15
Topological Sort - Find Eventual Safe States
What is the time complexity of the optimal approach that uses the reversed graph and Kahn's algorithm to find eventual safe states in a graph with V vertices and E edges?
AO(V + E) because each edge and vertex is processed once during graph reversal and BFS
BO(V^2) because we process each node and its neighbors repeatedly
CO(V * E) due to nested loops over vertices and edges
DO(E log V) because of sorting the safe nodes at the end
Step-by-Step Solution
Solution:
  1. Step 1: Analyze graph reversal and in-degree computation

    Reversing the graph and computing in-degree requires iterating over all edges once, O(E).
  2. Step 2: Analyze BFS processing

    Each node is enqueued and dequeued at most once, and each edge is considered once when decrementing in-degree, total O(V + E).
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Linear time in vertices and edges matches BFS and graph reversal [OK]
Quick Trick: Each edge and vertex processed once in BFS + reversal [OK]
Common Mistakes:
MISTAKES
  • Assuming nested loops cause O(V*E)
  • Confusing sorting cost as dominant
  • Forgetting BFS processes each edge once
Trap Explanation:
PITFALL
  • O(V^2) or O(V*E) seem plausible if one imagines repeated processing, but BFS and reversal are linear.
Interviewer Note:
CONTEXT
  • Checks if candidate understands graph traversal complexity and avoids common overestimation.
Master "Find Eventual Safe States" 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