chapter

    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 be the number of distinct ways to reach step . A naive recursive program to compute makes 15 function calls. But only 6 distinct subproblems exist: through . The subproblem is solved twice, three times, and 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.

    overlapping subproblems memoization

    The recurrence and why caching gives the right answer

    Derivation

    The recurrence and why caching gives the right answer

    Assumption: a staircase has steps, and each move climbs 1 or 2 steps. Let be the number of distinct ways to reach step .

    steps moves of 1 or 2
    1. Any valid sequence of moves ending at step ends with either a 1-step from step or a 2-step from step .exhaustive cases for the last move
    2. These two cases are disjoint because the last move is either 1 step or 2 steps, never both.mutually exclusive final actions
    3. The number of ways ending with a 1-step equals , because the prefix is any valid way to reach step .bijection between prefixes and full paths
    4. The number of ways ending with a 2-step equals , by the same reasoning.bijection for the 2-step case
    5. Since the cases are disjoint and exhaustive, for .sum rule of counting
    6. Each is a well-defined integer, so computing it once and storing the answer is safe.deterministic subproblems
    7. Every future lookup returns the same value, and the total work becomes 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

    Valid for , with base cases and .

    Check

    For , . Direct enumeration gives , , . 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 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 and . The recurrence gives , , , , . But the correct value is . The error is that , not 0. There is exactly one way to climb zero steps: do nothing. With and , we get , , , , .

    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 and report 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.

    Explore complete test series, official past papers, and chapter-wise study tools.

    Why MastersUp

    Personalised first. High quality throughout.

    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 be the number of distinct ways to reach step . A naive recursive program to compute makes 15 function calls. But only 6 distinct subproblems exist: through . The subproblem is solved twice, three times, and 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.

    overlapping subproblems memoization

    The recurrence and why caching gives the right answer

    Derivation

    The recurrence and why caching gives the right answer

    Assumption: a staircase has steps, and each move climbs 1 or 2 steps. Let be the number of distinct ways to reach step .

    steps moves of 1 or 2
    1. Any valid sequence of moves ending at step ends with either a 1-step from step or a 2-step from step .exhaustive cases for the last move
    2. These two cases are disjoint because the last move is either 1 step or 2 steps, never both.mutually exclusive final actions
    3. The number of ways ending with a 1-step equals , because the prefix is any valid way to reach step .bijection between prefixes and full paths
    4. The number of ways ending with a 2-step equals , by the same reasoning.bijection for the 2-step case
    5. Since the cases are disjoint and exhaustive, for .sum rule of counting
    6. Each is a well-defined integer, so computing it once and storing the answer is safe.deterministic subproblems
    7. Every future lookup returns the same value, and the total work becomes 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

    Valid for , with base cases and .

    Check

    For , . Direct enumeration gives , , . 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 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 and . The recurrence gives , , , , . But the correct value is . The error is that , not 0. There is exactly one way to climb zero steps: do nothing. With and , we get , , , , .

    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 and report 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 , the number of distinct ways to reach the top, using dynamic programming with tabulation.

    First instinct

    Write a recursive function and call .

    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
    1. Identify subproblems. is the number of ways to reach step , for .define the state space
    2. Write the recurrence. for .transition between states
    3. Identify base cases. , .smallest instances
    4. Build the table bottom-up. .first inductive step
    5. .second inductive step
    6. .third inductive step
    7. .final target state

    The answer is .

    Check: Ways to reach step 6 equal ways via step 5 plus ways via step 4, which is .

    More notes in this unit