Bird
Raised Fist0

What is the worst-case time complexity of enumerating all paths from source to target in a DAG with n nodes using DFS with memoization?

medium🪤 Complexity Trap Q5 of Q15
Topological Sort - All Paths From Source to Target (DAG)
What is the worst-case time complexity of enumerating all paths from source to target in a DAG with n nodes using DFS with memoization?
AO(n^2)
BO(n!)
CO(V + E + P), where P is the total number of paths
DO(2^n)
Step-by-Step Solution
Solution:
  1. Step 1: Understand DFS with memoization

    Memoization caches results for each node, avoiding repeated work.
  2. Step 2: Analyze complexity components

    Time includes traversing all vertices and edges (V + E) plus enumerating all paths (P).
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Memoization reduces repeated work but must still output all paths [OK]
Quick Trick: Memoization time = graph traversal + all paths output [OK]
Common Mistakes:
MISTAKES
  • Confusing exponential paths with traversal time
  • Ignoring output size in complexity
Trap Explanation:
PITFALL
  • Candidates often pick O(2^n) ignoring memoization's pruning effect on repeated subproblems.
Interviewer Note:
CONTEXT
  • Tests understanding of memoization impact on time complexity in path enumeration.
Master "All Paths From Source to Target (DAG)" 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