3.7.6 · D1Algorithm Paradigms

Foundations — Dynamic programming — overlapping subproblems, optimal substructure

2,395 words11 min readBack to topic

This page assumes you have seen nothing. Before you can read the parent note Dynamic Programming, you must be fluent in the tiny alphabet below. We build each symbol on the one before it.


0. What is a "subproblem"? (the atom of everything)

Every symbol here orbits one word: subproblem.

Picture it as a nesting doll: the big doll (whole problem) contains smaller identical dolls (subproblems) inside it. You cannot understand DP until you see problems this way.

Figure — Dynamic programming — overlapping subproblems, optimal substructure

Why the topic needs it: every other symbol below is a way of naming, counting, or storing subproblems. No subproblem, no DP.


1. The function-call symbol

Picture a vending machine: press button , out drops one answer.

  • The letter is just a label — we could call it , , or "the-answer-getter."
  • The thing in the brackets, , is the input — the size or state of the problem.

2. Recursion: a machine that calls itself

Why a "" here? Because for Fibonacci the whole is literally the sum of two smaller answers. Different problems combine differently (we will see later) — the combining rule is the heart of each DP.

Picture recursion as a tree that grows downward: one call at the top splits into smaller calls, which split again, until every branch hits a base case (a leaf).

Figure — Dynamic programming — overlapping subproblems, optimal substructure

3. Reading the recursion tree — where "overlap" becomes visible

Look again at the tree figure (s02). Find every node labelled . In 's tree, appears twice, appears three times. Those repeats are the whole reason DP exists.

This gap — many nodes, few distinct — is the exploitable waste. Detailed tree-counting lives in Recursion Trees.

Why the topic needs it: DP's whole payoff is "solve the few distinct ones, skip all the repeats." You cannot see the payoff without seeing the repeats.


4. Big-O: the symbol that measures the waste

Picture two runners:

  • climbs a gentle ramp.
  • exponential growth shoots up a near-vertical cliff.
Figure — Dynamic programming — overlapping subproblems, optimal substructure

5. The "sticky note": a table / memo

Two symbols to read comfortably:

  • — a 1-D row of sticky notes (Fibonacci uses this).
  • — a 2-D grid, one axis per input. Knapsack uses = item index and = capacity.

Picture as a spreadsheet: rows are items, columns are capacities, each cell is one stored answer.

Figure — Dynamic programming — overlapping subproblems, optimal substructure

Why the topic needs it: the table is the memory that upgrades plain recursion into DP. Without it, §3's repeats are recomputed; with it, each distinct doll is solved once.


6. The symbol and "optimal"

Before the recurrence, meet its symbols. In the Knapsack Problem we have a list of items; item number has:

  • = the value of item (how much it is worth),
  • = the weight of item (how much room it eats),
  • = the capacity remaining in the backpack right now.

Contrast with Greedy Algorithms (which grabs the locally best option once and never reconsiders) and Divide and Conquer (whose subproblems are all distinct, so no sticky notes help). Other classic optimal-substructure problems: Longest Common Subsequence, Bellman-Ford.


The prerequisite map

Subproblem = smaller copy

Function notation F of n

Domain n in naturals with zero

Recurrence F calls smaller F

Base case stops recursion

Recursion tree picture

Overlapping vs distinct subproblems

Big-O measures the waste

Table stores each answer once

Memoization top down

Tabulation bottom up

max and optimal substructure

Dynamic Programming

Every arrow means "you need the left box before the right box makes sense." The topic sits at the bottom because it fuses all these foundations.


Equipment checklist

Cover the right side and answer out loud before opening the parent note.

What does mean in one sentence?
The answer to the problem when the input has size/value — a machine you feed and get one answer back.
What is the domain of Fibonacci's ?
The non-negative integers — no fractions, no negatives.
What is a subproblem?
A smaller copy of the same problem (a smaller nesting doll).
What is a base case and why is it required?
The smallest input whose answer you know directly; without it the recursion never stops (and it keeps from going below zero).
In a recursion tree, what does "overlapping subproblems" look like?
The same node label appearing in more than one place in the tree.
Distinct subproblems for , and how does the node count grow?
Only distinct ones, but the total node count grows like — exponential.
What does formally mean?
The running time stays below a fixed multiple of the yardstick function once is large enough — is a ceiling.
What is ?
A cell in a 2-D table storing the answer to the subproblem indexed by and .
Memoization vs tabulation in one line each?
Memoization = top-down recursion that checks the table first (lazy); tabulation = bottom-up loop filling smallest first (eager).
What does do, and when is it used in DP?
Returns the larger of and ; used when the problem chooses the best option (e.g. take-or-skip in knapsack).
What are , , , and in knapsack?
= value of item ; = its weight; = capacity left; = best value using items with room .
Why does the knapsack recurrence split into three cases?
To handle (no items), (item does not fit — skip, guarding against a negative index), and (choose the better of take/skip).
State optimal substructure in plain words.
The best answer to the whole can be built from best answers to its parts.
Why is the combining operator sometimes and sometimes ?
It matches what the problem asks — Fibonacci sums two answers, knapsack picks the best of two options.
Where does the master formula Time = (#subproblems)×(work each) come from?
From summing the fill-cost over every table cell; if each cell costs the same, the sum becomes count × work-per-cell.

Connections

  • Recursion — the machine DP is built on.
  • Recursion Trees — where overlap becomes visible.
  • Time Complexity Analysis — the meaning of .
  • Memoization vs Tabulation — the two table-filling styles.
  • Divide and Conquer — disjoint subproblems, so no caching help.
  • Greedy Algorithms — commits once, never reconsiders.
  • Knapsack Problem, Longest Common Subsequence, Bellman-Ford — optimal-substructure classics.