0
0
DSA Typescriptprogramming~10 mins

DP vs Recursion vs Greedy Choosing the Right Tool in DSA Typescript - Visual Comparison

Choose your learning style9 modes available
Concept Flow - DP vs Recursion vs Greedy Choosing the Right Tool
Start Problem
Is problem optimal substructure?
Is problem overlapping subproblems?
Use DP (Memoization or Tabulation)
Can greedy choice property be applied?
Use Greedy Algorithm
Solve Problem Efficiently
End
This flow shows how to decide between DP, recursion, and greedy by checking problem properties step-by-step.
Execution Sample
DSA Typescript
function fib(n: number): number {
  if (n <= 1) return n;
  return fib(n-1) + fib(n-2);
}
Simple recursive Fibonacci function showing overlapping subproblems and exponential calls.
Execution Table
StepCall StackCurrent CallActionResult/ReturnNotes
1fib(4)fib(4)Calls fib(3) and fib(2)PendingStart with fib(4)
2fib(4) -> fib(3)fib(3)Calls fib(2) and fib(1)Pendingfib(3) needs fib(2) and fib(1)
3fib(4) -> fib(3) -> fib(2)fib(2)Calls fib(1) and fib(0)Pendingfib(2) calls base cases
4fib(4) -> fib(3) -> fib(2) -> fib(1)fib(1)Base case returns 11fib(1) = 1
5fib(4) -> fib(3) -> fib(2) -> fib(0)fib(0)Base case returns 00fib(0) = 0
6fib(4) -> fib(3) -> fib(2)fib(2)Returns fib(1)+fib(0)1fib(2) = 1 + 0 = 1
7fib(4) -> fib(3) -> fib(1)fib(1)Base case returns 11fib(1) = 1
8fib(4) -> fib(3)fib(3)Returns fib(2)+fib(1)2fib(3) = 1 + 1 = 2
9fib(4) -> fib(2)fib(2)Calls fib(1) and fib(0) againPendingRepeated calls show overlapping subproblems
10fib(4) -> fib(2) -> fib(1)fib(1)Base case returns 11fib(1) = 1
11fib(4) -> fib(2) -> fib(0)fib(0)Base case returns 00fib(0) = 0
12fib(4) -> fib(2)fib(2)Returns fib(1)+fib(0)1fib(2) = 1 + 0 = 1
13fib(4)fib(4)Returns fib(3)+fib(2)3fib(4) = 2 + 1 = 3
14----All calls resolved, recursion ends
💡 All recursive calls completed, final result fib(4) = 3
Variable Tracker
VariableStartAfter Step 4After Step 6After Step 8After Step 12Final
Call Stack Depth143220
fib(1) ResultN/A11111
fib(0) ResultN/A00000
fib(2) ResultN/APending1111
fib(3) ResultN/APendingPending222
fib(4) ResultN/APendingPendingPendingPending3
Key Moments - 3 Insights
Why does fib(2) get called multiple times in the recursion?
Because the recursion does not remember previous results, fib(2) is recomputed each time it is needed, as shown in steps 3 and 9 in the execution_table.
When should we choose a greedy algorithm over DP or recursion?
Choose greedy only if the problem has the greedy choice property and optimal substructure, meaning local optimal choices lead to global optimum. Otherwise, DP or recursion is safer.
How does DP improve over plain recursion?
DP stores results of subproblems (memoization or tabulation) to avoid repeated work, unlike recursion which recomputes, as seen by repeated calls in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result returned by fib(3) at step 8?
A3
B1
C2
DPending
💡 Hint
Check the 'Result/Return' column at step 8 in the execution_table.
At which step does the recursion start to return base case values?
AStep 1
BStep 4
CStep 9
DStep 13
💡 Hint
Look for 'Base case returns' in the 'Action' column in execution_table.
If we use DP (memoization), how would the execution_table change?
AFewer repeated calls like fib(2), reducing steps
BMore base case calls
CMore calls to fib(0)
DNo change in call stack depth
💡 Hint
Refer to repeated fib(2) calls at steps 3 and 9 in execution_table.
Concept Snapshot
DP vs Recursion vs Greedy Quick Guide:
- Recursion: simple, solves by calling itself, but may repeat work
- DP: recursion + memoization/tabulation to avoid repeats
- Greedy: picks best local choice, fast if problem fits
- Choose DP if overlapping subproblems + optimal substructure
- Choose Greedy if greedy choice property holds
- Otherwise, use plain recursion for simple or small problems
Full Transcript
This lesson shows how to choose between dynamic programming, recursion, and greedy algorithms by checking problem properties. Recursion solves problems by calling itself but can repeat work, as seen in the Fibonacci example where fib(2) is called multiple times. Dynamic programming improves recursion by storing results to avoid repeats. Greedy algorithms pick the best local choice and work well if the problem has the greedy choice property. The execution table traces recursive calls for fib(4), showing call stack growth and returns. Key moments clarify why repeated calls happen and when to pick each approach. The visual quiz tests understanding of recursion steps and DP benefits.