Dynamic Programming Notes for Cmi M.sc. and Ph.d. Computer Science
Dynamic Programming notes for CMI M.Sc. and Ph.D. Computer Science: 37 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice
Dynamic Programming notes
Why recomputing subproblems wastes time
Intuition
Why recomputing subproblems wastes time
Dynamic programming stores each answer the first time it is computed, converting exponential-time recursion into polynomial time when subproblems recur.
The problem it solves
A staircase has 6 steps. You can climb 1 or 2 steps at a time. Let W(n) be the number of distinct ways to reach step n. A naive recursive program to compute W(6) makes 15 function calls. But only 6 distinct subproblems exist: W(1) through W(6). The subproblem W(4) is solved twice, W(3) three times, and W(2) five times. Each recomputation wastes time proportional to the size of its subtree.
The idea made exact
The next time the same subproblem appears, the program reads the stored value in constant time. The 15 recursive calls collapse to 6 computations and 9 table lookups. The exact principle is this: when a problem decomposes into subproblems and the same subproblems recur, cache every answer. This converts exponential-time recursion into polynomial time whenever the number of distinct subproblems is polynomial.
Time=distinct subproblems+lookups
overlapping subproblemsmemoization
The recurrence and why caching gives the right answer
Derivation
The recurrence and why caching gives the right answer
Assumption: a staircase has n steps, and each move climbs 1 or 2 steps. Let W(k) be the number of distinct ways to reach step k.
n stepsmoves of 1 or 2
Any valid sequence of moves ending at step k ends with either a 1-step from step k−1 or a 2-step from step k−2.exhaustive cases for the last move
These two cases are disjoint because the last move is either 1 step or 2 steps, never both.mutually exclusive final actions
The number of ways ending with a 1-step equals W(k−1), because the prefix is any valid way to reach step k−1.bijection between prefixes and full paths
The number of ways ending with a 2-step equals W(k−2), by the same reasoning.bijection for the 2-step case
Since the cases are disjoint and exhaustive, W(k)=W(k−1)+W(k−2) for k≥3.sum rule of counting
Each W(k) is a well-defined integer, so computing it once and storing the answer is safe.deterministic subproblems
Every future lookup returns the same value, and the total work becomes O(n) instead of exponential.linear number of distinct states
This decomposition is an instance of Bellman's principle: the solution to a problem is built from solutions to its subproblems.
Result
W(k)=W(k−1)+W(k−2)
Valid for k≥3, with base cases W(1)=1 and W(2)=2.
Check
For k=3, W(3)=W(2)+W(1)=2+1=3. Direct enumeration gives (1,1,1), (1,2), (2,1). Three ways. Matches.
Taking the empty base case as zero instead of one
Wrong belief
Taking the empty base case as zero instead of one
The base case W(0) equals 0 because there are zero ways to climb zero steps.
Why it feels right
Zero steps seems to mean zero ways, just as zero items means zero value in many counting problems.
Smallest counterexample
Set W(0)=0 and W(1)=1. The recurrence W(k)=W(k−1)+W(k−2) gives W(2)=1, W(3)=2, W(4)=3, W(5)=5, W(6)=8. But the correct value is W(6)=13. The error is that W(0)=1, not 0. There is exactly one way to climb zero steps: do nothing. With W(0)=1 and W(1)=1, we get W(2)=2, W(3)=3, W(4)=5, W(5)=8, W(6)=13.
Counter-rule
The base case for counting an empty sequence of choices is 1, not 0. An empty product is 1, and an empty sequence of moves is one valid way.
How it shows up in options
Options will use W(0)=0 and report W(6)=8 instead of 13.
34 more cards in this chapter
Free preview ends here
Login to view the complete notes
Creating an account is free. You get the rest of this chapter, step-by-step solutions, and a study plan built around the topics you are actually weak at.
Most platforms hand everyone the same content. Here the content moves with your performance, topic by topic.
Built around you, not around a syllabus PDF
Every answer you give moves your topic-level intelligence rate. The next question, the next revision card and tomorrow's plan all change with it.
Revision that hits your weak spots
We only revise topics you have actually attempted and are still below the safe bar on — never the same chapter on repeat.
Questions calibrated to the real exam
Each question carries a measured toughness. You are served a rung above your current level, so practice keeps stretching you.
Notes written for recall, not for volume
Full lesson cards for first study, curated short-note cards for the last mile — with derivations, traps and exam patterns marked.
One place for everything
Notes, chapter practice, previous-year questions, test series and full-length papers — all feeding one picture of your preparation.
Honest progress
No vanity streaks. Progress here means chapters mastered and accuracy that held up on harder questions.
Unlock the whole course
Full notes and short notes, the complete question bank with worked solutions, mock tests, full-length papers, and an adaptive plan that rebuilds itself as you improve.
Dynamic Programming Notes for CMI M.Sc. and Ph.D. Computer Science
Dynamic Programming notes for CMI M.Sc. and Ph.D. Computer Science: 37 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice questions.
Why recomputing subproblems wastes time
Intuition
Why recomputing subproblems wastes time
Dynamic programming stores each answer the first time it is computed, converting exponential-time recursion into polynomial time when subproblems recur.
The problem it solves
A staircase has 6 steps. You can climb 1 or 2 steps at a time. Let W(n) be the number of distinct ways to reach step n. A naive recursive program to compute W(6) makes 15 function calls. But only 6 distinct subproblems exist: W(1) through W(6). The subproblem W(4) is solved twice, W(3) three times, and W(2) five times. Each recomputation wastes time proportional to the size of its subtree.
The idea made exact
The next time the same subproblem appears, the program reads the stored value in constant time. The 15 recursive calls collapse to 6 computations and 9 table lookups. The exact principle is this: when a problem decomposes into subproblems and the same subproblems recur, cache every answer. This converts exponential-time recursion into polynomial time whenever the number of distinct subproblems is polynomial.
Time=distinct subproblems+lookups
overlapping subproblemsmemoization
The recurrence and why caching gives the right answer
Derivation
The recurrence and why caching gives the right answer
Assumption: a staircase has n steps, and each move climbs 1 or 2 steps. Let W(k) be the number of distinct ways to reach step k.
n stepsmoves of 1 or 2
Any valid sequence of moves ending at step k ends with either a 1-step from step k−1 or a 2-step from step k−2.exhaustive cases for the last move
These two cases are disjoint because the last move is either 1 step or 2 steps, never both.mutually exclusive final actions
The number of ways ending with a 1-step equals W(k−1), because the prefix is any valid way to reach step k−1.bijection between prefixes and full paths
The number of ways ending with a 2-step equals W(k−2), by the same reasoning.bijection for the 2-step case
Since the cases are disjoint and exhaustive, W(k)=W(k−1)+W(k−2) for k≥3.sum rule of counting
Each W(k) is a well-defined integer, so computing it once and storing the answer is safe.deterministic subproblems
Every future lookup returns the same value, and the total work becomes O(n) instead of exponential.linear number of distinct states
This decomposition is an instance of Bellman's principle: the solution to a problem is built from solutions to its subproblems.
Result
W(k)=W(k−1)+W(k−2)
Valid for k≥3, with base cases W(1)=1 and W(2)=2.
Check
For k=3, W(3)=W(2)+W(1)=2+1=3. Direct enumeration gives (1,1,1), (1,2), (2,1). Three ways. Matches.
Taking the empty base case as zero instead of one
Wrong belief
Taking the empty base case as zero instead of one
The base case W(0) equals 0 because there are zero ways to climb zero steps.
Why it feels right
Zero steps seems to mean zero ways, just as zero items means zero value in many counting problems.
Smallest counterexample
Set W(0)=0 and W(1)=1. The recurrence W(k)=W(k−1)+W(k−2) gives W(2)=1, W(3)=2, W(4)=3, W(5)=5, W(6)=8. But the correct value is W(6)=13. The error is that W(0)=1, not 0. There is exactly one way to climb zero steps: do nothing. With W(0)=1 and W(1)=1, we get W(2)=2, W(3)=3, W(4)=5, W(5)=8, W(6)=13.
Counter-rule
The base case for counting an empty sequence of choices is 1, not 0. An empty product is 1, and an empty sequence of moves is one valid way.
How it shows up in options
Options will use W(0)=0 and report W(6)=8 instead of 13.
Computing the staircase values by tabulation
Problem
Computing the staircase values by tabulation
A staircase has 6 steps. You can climb 1 or 2 steps at a time. Compute W(6), the number of distinct ways to reach the top, using dynamic programming with tabulation.
First instinct
Write a recursive function W(n)=W(n−1)+W(n−2) and call W(6).
Trap reveal
Naive recursion makes 15 calls for 6 distinct values. The exam expects you to show the DP table, not the recursion tree.
Show the elegant path
Identify subproblems. W(k) is the number of ways to reach step k, for k=1,2,…,6.define the state space
Write the recurrence. W(k)=W(k−1)+W(k−2) for k≥3.transition between states
Identify base cases. W(1)=1, W(2)=2.smallest instances
Build the table bottom-up. W(3)=W(2)+W(1)=2+1=3.first inductive step
W(4)=W(3)+W(2)=3+2=5.second inductive step
W(5)=W(4)+W(3)=5+3=8.third inductive step
W(6)=W(5)+W(4)=8+5=13.final target state
The answer is W(6)=13.
Check: Ways to reach step 6 equal ways via step 5 plus ways via step 4, which is W(5)+W(4)=8+5=13.