Dynamic Programming

Dynamic programming solves problems with overlapping subproblems and optimal substructure by storing each subproblem’s result once; it can be top-down (memoised recursion) or bottom-up (tabulation), turning exponential recursion into polynomial time.

Key formulas & points

Skim these first — then read the full notes below.

  • Top-down memoisation versus bottom-up tabulation
  • A rolling array saves space when only the previous row is needed
  • Defining the state correctly is the hardest step

Topic details

Introduction

This topic covers the DP paradigm. You identify when a problem has overlapping subproblems, define the state and recurrence, choose memoisation or tabulation, and optimise space with rolling arrays — practising on knapsack, longest common subsequence and similar staples.

Key relations & formulas

Formulas (Indian textbook notation)

  • DPapplieswhenthereisoptimalsubstructureandoverlappingsubproblemsDP applies when there is optimal substructure and overlapping subproblems

Formulas (Indian textbook notation)

  • 01knapsack:dp[i][w]=max(exclude,includeifitfits)\frac{0}{1} knapsack: dp[i][w] = max(exclude, include if it fits)

Formulas (Indian textbook notation)

  • LCS:dp[i][j]=dp[i1][j1]+1ifcharsmatch,elsemaxofneighboursLCS: dp[i][j] = dp[i-1][j-1] + 1 if chars match, else max of neighbours

Notation and sign conventions

Relation 1 —
DPapplieswhenthereisoptimalsubstructureandoverlappingsubproblemsDP applies when there is optimal substructure and overlapping subproblems

Formulas (Indian textbook notation)

  • DPapplieswhenthereisoptimalsubstructureandoverlappingsubproblemsDP applies when there is optimal substructure and overlapping subproblems
Write this relation with symbols exactly as in Clrs Algorithms — Standard reference before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.
Relation 2 —
01knapsack:dp[i][w]=max\frac{0}{1} knapsack: dp[i][w] = max

Formulas (Indian textbook notation)

  • 01knapsack:dp[i][w]=max(exclude,includeifitfits)\frac{0}{1} knapsack: dp[i][w] = max(exclude, include if it fits)
Write this relation with symbols exactly as in Clrs Algorithms — Standard reference before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.
Relation 3 —
LCS:dp[i][j]=dp[i1][j1]+1ifcharsmatch,elsemaxofneighboursLCS: dp[i][j] = dp[i-1][j-1] + 1 if chars match, else max of neighbours

Formulas (Indian textbook notation)

  • LCS:dp[i][j]=dp[i1][j1]+1ifcharsmatch,elsemaxofneighboursLCS: dp[i][j] = dp[i-1][j-1] + 1 if chars match, else max of neighbours
Write this relation with symbols exactly as in Clrs Algorithms — Standard reference before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.

Concept in depth

Dynamic programming is recursion that remembers. Where naive recursion recomputes the same subproblems exponentially (as in Fibonacci), DP computes each once and reuses it, collapsing the cost to the number of distinct states times the work per state. The two implementations are equivalent in result: top-down memoisation adds a cache to natural recursion and computes only the states it needs, while bottom-up tabulation fills a table in dependency order and often allows space optimisation. The genuine difficulty is not coding but modelling — choosing a state that captures exactly the information the recurrence needs, and writing that recurrence correctly.

Assumptions and validity limits

State assumptions explicitly before using any relation for dynamic programming — steady state, uniform properties, linear elastic material, ideal gas, incompressible flow, etc., as applicable.
Wrong assumptions invalidate the entire solution even when the formula is correct. In Algorithms viva and GATE descriptive questions, listing valid assumptions often earns separate marks.

Step-by-step problem approach

1. Read the question and list given data with SI units (common in Algorithms papers).
2. Draw a neat labelled diagram where applicable — examiners in Indian universities award diagram marks even when arithmetic slips.
3. Identify which relation from this topic applies to dynamic programming.
4. Use equation 1:
DPapplieswhenthereisoptimalsubstructureandoverlappingsubproblemsDP applies when there is optimal substructure and overlapping subproblems
.
5. Use equation 2:
01knapsack:dp[i][w]=max\frac{0}{1} knapsack: dp[i][w] = max
.
6. Substitute values, compute, and verify units and sign (direction).
7. State conclusion in one line — e.g. safe/unsafe, stable/unstable, feasible/infeasible.

Applications & exam relevance

Dynamic Programming appears in competitive programming and backend systems. In Indian it software curricula this topic is tested because it connects theory to design and analysis of algorithms.
GATE and semester exams often combine dynamic programming with earlier units — revise prerequisites before attempting mixed problems.
Industry interview panels sometimes ask: "Where did you use dynamic programming?" — answer with a lab, mini-project, or plant visit example if possible.

Common mistakes in exams

Students define an incomplete state that misses needed information, confuse memoisation with plain recursion (no cache), and get base cases or table order wrong. Writing code before the recurrence often leads to subtle indexing bugs.

Quick revision checklist

Before attempting dynamic programming problems, confirm you can:
1. Top-down memoisation versus bottom-up tabulation
2. A rolling array saves space when only the previous row is needed
3. Defining the state correctly is the hardest step
Revise the solved examples in Clrs Algorithms — Standard reference and one previous-year GATE or university paper for this unit.

Worked examples

Try the problem first — open the solution when you are ready to check.

0/1 knapsack cell

Problem

For capacity w = 5, item i has value 8 and weight 3; excluding it gives value 6. Compute dp[i][5].

Solution

Include: value 8 + dp[i−1][5−3]; exclude: dp[i−1][5] = 6. With the included branch yielding 8, dp[i][5] = max(8, 6) = 8. Always write both candidates before taking the max.

Conceptual check — Dynamic Programming

Problem

In a Algorithms semester or GATE paper you are asked: "State the main assumption, the governing relation, and one practical consequence of dynamic programming." What should a complete answer include?

Exams & GATE

Write the recurrence before filling the table — examiners award partial marks.

📖 Standard books (India)

  • Clrs AlgorithmsStandard reference

    Read: Syllabus unit

    Referenced in Indian B.Tech syllabus