Dynamic Programming Optimization
Dynamic Programming Optimization
in Optimization
A Complete Guide with ELI5 Explanations,
Worked Examples, and Practice Problems
Those who cannot remember the past are condemned to repeat it.
adapted for Dynamic Programming
Contents
1 What is Optimization? 3
10 Algorithmic Steps 12
11 Graphical Interpretation 13
11.1 Multi-Stage Decision Network . . . . . . . . . . . . . . . . . . . . . . . . 13
11.2 State Transition Diagram . . . . . . . . . . . . . . . . . . . . . . . . . . 14
14 Common Mistakes 18
15 Computational Complexity 19
15.1 The Curse of Dimensionality . . . . . . . . . . . . . . . . . . . . . . . . . 20
17 Practice Problems 21
17.1 Easy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
17.2 Medium . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
17.3 Hard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
18 Exam-Level Questions 23
19 Selected Solutions 24
Dynamic Programming in Optimization 3
1 What is Optimization?
⋆ ELI5 Insight
Imagine you have a box of crayons and a colouring book. You want to colour the
prettiest picture possible, but you only have limited time before dinner. Optimization
is choosing which pages to colour and which crayons to use so that you get
the best result with the resources you have.
In mathematical terms, optimization is the process of nding the best solution from
a set of feasible alternatives.
Denition 1.1 (Optimization Problem). An optimization problem has the general form
min f (x)
x∈X
subject to gi (x) ≤ 0, i = 1, . . . , m,
hj (x) = 0, j = 1, . . . , p,
where
x is the decision variable (what we choose),
f (x) is the objective function (what we want to minimize or maximize),
gi , hj are constraint functions (the rules we must follow),
X is the feasible set.
Σ Math Translation
Remark 1.1. We write min for minimization; for maximization, simply replace min with
max (or equivalently minimize −f (x)).
You are climbing a staircase with 10 steps. You can take either 1 step or 2 steps
at a time. You want to know: How many dierent ways can I reach the top?
You could try every possible combinationbut that is exhausting! Instead, notice
this:
The number of ways to reach step 10 depends only on how many ways you can
reach step 8 and step 9 (because from either of those you can jump to 10).
The number of ways to reach step 9 depends only on steps 7 and 8.
Dynamic Programming in Optimization 4
The term was coined by Richard Bellman in the 1950s. Despite the name, it has
nothing to do with programming in the software senseprogram here means a plan
or schedule.
Imagine you are planning a road trip from City A to City E, passing through
intermediate cities B, C, D. At every city you choose which road to take next. Each
road has a cost (fuel, tolls, time).
2
B2 C2 5
5 2
6 D E
3 4
A B1 C1 3
Key Concept
Because of optimal substructure, we can safely build global optima from local optima.
Because of overlapping sub-problems, storing solutions (memoization / tabulation)
gives us a huge speed-up compared to brute force.
⋆ ELI5 Insight
Suppose you found the cheapest route from your home to the airport, passing
through the highway and then the tunnel. The Principle of Optimality says: the part
of your route from the highway onward must also be the cheapest way to get from
the highway to the airport.
If it weren't, you could swap in a cheaper highway-to-airport route and your total trip
would be even cheapercontradicting the fact that your original route was optimal!
Proof sketch (by contradiction). Let π ∗ = (d∗1 , d∗2 , . . . , d∗N ) be an optimal policy for an
N -stage problem starting in state s1 .
Suppose the sub-policy (d∗k , d∗k+1 , . . . , d∗N ) is not optimal for the sub-problem starting
∗ ∗
in the state sk that results from applying (d1 , . . . , dk−1 ). Then there exists a sub-policy
(dˆk , . . . , dˆN ) with a strictly better objective value from sk onward. Replacing the tail of
π ∗ yields a new policy
π̂ = (d∗1 , . . . , d∗k−1 , dˆk , . . . , dˆN )
whose total objective value is strictly better than that of π∗. This contradicts the opti-
∗
mality of π . Hence the sub-policy must be optimal.
Σ Math Translation
Let f ∗ (s) denote the optimal cost-to-go from state s. The Principle of Optimality
implies the recursive structure
n o
f ∗ (sk ) = min c(sk , dk ) + f ∗ (s ) ,
dk ∈Dk (sk ) | {zk+1}
immediate cost optimal future cost
| {z }
where sk+1 = T (sk , dk ) is the state transition. This is the Bellman equation, the
engine of all DP algorithms.
Dynamic Programming in Optimization 6
The Principle of Optimality converts one giant N -decision problem into N single-
decision problems, each of which is easy. We solve them in order (forward or back-
ward), and the stored optimal values guarantee that every local decision is globally
consistent. No backtracking is ever needed.
⋆ ELI5 Insight
Think of Linear Programming as lling out a single giant form where every rule
(constraint) is a straight line and you slide along those lines to nd the best corner.
Dynamic Programming is more like playing a video game level by levelyou make
the best move at each level, remembering what happened before.
Table 1: DP vs LP at a glance
Σ Math Translation
Remark 4.1. LP and DP are not competitors; they are complementary. Some problems
(e.g., stochastic inventory) use DP at the top level and LP inside each stage.
⋆ ELI5 Insight
Before you build a LEGO castle using the level-by-level trick (DP), check:
1. Can you split the build into stages (layers)?
2. At each layer, does the best way to continue depend only on what the current
layer looks likenot on how you got there?
3. Do the same what does this layer look like? questions pop up again and
again ?
4. Can you describe what this layer looks like with a small label (the state )?
5. Is there a clear way to go from one layer to the next?
If yes to all, DP will work beautifully.
Key Concept
Properties 12 guarantee correctness (we are not missing the true optimum). Prop-
erty 3 guarantees eciency (we save time by not re-solving). Properties 45 give us
the machinery (states and recurrences) to implement the algorithm.
6.1 Stages
Denition 6.1 (Stage). A stage is a point in the problem at which a decision must be
made. Stages are indexed k = 1, 2, . . . , N and often correspond to time periods, locations,
or items being considered.
⋆ ELI5 Insight
Think of stages as checkpoints in a race. At each checkpoint you decide which path
to take to the next checkpoint.
6.2 States
Denition 6.2 (State). A state sk at stage k is a complete description of the system's
conditionit contains all the information needed to make optimal decisions from stage
k onward, without knowing how the system arrived at sk .
⋆ ELI5 Insight
The state is your save le in a video game. If someone loads your save le, they
can play optimally from that point without knowing anything about what you did
before.
Σ Math Translation
The future depends on the present state and current decision, not on the past.
Example 6.2. In the shortest-path problem, the state at stage k is simply which node
you are at. Your past route does not matteronly your current node determines what
you can do next.
Dk (B) = {C1 , C2 , C3 }.
Dk (sk ) Tk (sk , dk )
Stage k State sk Decision dk State sk+1
Remark 6.1. Choosing good states is the art of DP. States that are too detailed lead to
the curse of dimensionality; states that are too coarse lose the Markov property.
N
X
fk∗ (sk ) = min ci (si , di ).
dk ,dk+1 ,...,dN
di ∈Di (si ) i=k
⋆ ELI5 Insight
Imagine you are on stage 3 of a 5-stage journey. The value function f3∗ (s3 ) answers:
If I play perfectly from here onward, what is the least total cost I will pay from stage 3
to the nish?
It is like asking a GPS: What is the shortest remaining distance to the destination
from right here?
Σ Math Translation
The value function converts an N -stage optimization into a single number for every
(k, sk ) pair:
fk∗ : Sk −→ R, k = 1, 2, . . . , N.
fN∗ (sN ) is the boundary condition (cost at the last stage).
f1∗ (s1 ) is the answer to the whole problem.
3. Policy extraction: Once all fk∗ are known, the optimal decisions are obtained by
The value function reduces the search space dramatically. Instead of optimizing
QN ∗
over all k=1 |Dk | possible decision sequences, we evaluate fk for each state at each
stagea much smaller computation.
⋆ ELI5 Insight
The best I can do from here = the cheapest `one-step cost plus best-I-can-do-from-
the-next-place' .
You try every allowed action at this stage, compute cost of this action + already-
known future cost, and pick the smallest.
Σ Math Translation
fk∗ (sk ) = min c (sk , dk ) + ∗
fk+1 (sk+1 ) .
dk |k {z
optimal cost from stage k immediate cost at stage k optimal cost from stage k+1
| {z } } | {z }
The recurrence exploits the Principle of Optimality: because the optimal tail from
∗
sk+1 is already stored as fk+1 , we only need to search over one decision dk at each
stage rather than over all future decision sequences. This is what makes DP ecient.
fk∗ (sk ) ∗
= min ck (sk , dk ) + fk+1 (Tk (sk , dk )) , k = N, N −1, . . . , 1. (2)
dk ∈Dk (sk )
⋆ ELI5 Insight
Backward DP is like planning a trip from the destination back to your house.
1. You start at the nish line and note: cost from here to nish = 0.
2. You step one city back and ask: What is the cheapest way to the nish from
here?
3. Keep stepping back until you reach home.
9.2 Forward DP
Denition 9.2 (Forward Recursion). Start at the rst stage and work toward the last.
Dene gk∗ (sk ) as the optimal cost to reach state sk :
∗
The answer is min gN +1 (sN +1 ).
sN +1
⋆ ELI5 Insight
Forward DP is the natural way you might think about it: start from home and ask
at every city, What is the cheapest way to get here ?
Dynamic Programming in Optimization 12
9.3 Comparison
Table 2: Forward vs backward DP
Backward DP Forward DP
Both directions produce the same optimal value because the Principle of Optimality
holds in both directions. Choose whichever direction makes the boundary condition
easier to dene or the state-space simpler to enumerate.
10 Algorithmic Steps
Here is a step-by-step recipe that works for any DP problem.
Key Concept
1. Identify the stages. What sequential decisions must be made? Label them
k = 1, 2, . . . , N .
2. Dene the state. What information at stage k fully determines the future?
Call it sk ∈ Sk .
3. Dene the decision. What choices are available at stage k in state sk ? Call
the decision dk ∈ Dk (sk ).
4. Write the state transition. sk+1 = Tk (sk , dk ).
5. Write the stage cost/reward. ck (sk , dk ).
6. Write the Bellman equation.
fk∗ (sk ) = ∗
min ck (sk , dk ) + fk+1 (Tk (sk , dk )) .
dk ∈Dk (sk )
9. Extract the optimal policy. Starting from s1 , follow the recorded decisions:
d∗1 → s2 → d∗2 → · · · → sN → d∗N .
Dynamic Programming in Optimization 13
⋆ ELI5 Insight
Remark 10.1. For forward recursion, swap steps 78: set the boundary at stage 1 and
solve forward from stage 1 to stage N.
11 Graphical Interpretation
Visualizing DP problems as graphs is one of the most powerful ways to build intuition.
Figure 1: A four-stage decision network. Node S is the source (start), T is the sink
(terminal). Each arc label is the cost of that decision.
⋆ ELI5 Insight
This picture is like a board game. You start at S, and at each column you pick
which node to jump to. The numbers on the arrows are the tolls you pay. DP nds
the path with the smallest total toll.
Dynamic Programming in Optimization 14
States s′2
d = α, c = 4
s2
d = γ, c = 2
s′3
Figure 2: State transition diagram for one stage of a DP problem. Each arrow shows a
decision d, the resulting next state, and the immediate cost c.
Σ Math Translation
In graph-theoretic terms:
Nodes = (stage, state) pairs.
Arcs = decisions; arc weight = stage cost ck (sk , dk ).
DP = nding the shortest (or longest) path in this DAG.
Because the graph is a DAG (no cycles), the shortest path can be found in O(|nodes|+
|arcs|) timewhich is exactly what the Bellman equation does.
2 7 5
1
2 4 8
3
6 3
1 4 3 2 6 10
5 3 4
3 6 9
3
4 4 7
Stage 1: {1}
Stage 2: {2, 3, 4}
Stage 3: {5, 6, 7}
Stage 4: {8, 9}
Stage 5: {10}
Stage 5 (Boundary).
Dynamic Programming in Optimization 16
Stage 4.
Stage 3.
Stage 2.
Stage 1.
⋆ ELI5 Insight
We started at the nish and asked how far am I? for every node near the end. Then
we stepped one column left and asked the same question, using answers we already
found. After four steps backward we reached the start and could read o the cheapest
route forwards : 1 → 3 → 5 → 8 → 10 costing just 11.
0 0 0 0
1 3 5 4
2 5 8 6
3 7 9 9
4 8 12 11
5 9 13 13
13.2 DP Formulation
Stages: k = 1, 2, 3 (one per zone).
State: sk = remaining budget when entering zone k . So s1 = 5.
Decision: xk = amount invested in zone k , 0 ≤ xk ≤ sk .
Transition: sk+1 = sk − xk .
Recurrence (backward, maximization):
s2 x2 =0 x2 =1 x2 =2 x2 =3 x2 =4 x2 =5 f2∗ x∗2
0 0 0 0
1 4 5 5 1
2 6 9 8 9 1
3 9 10 12 9 12 2
4 11 13 14 13 12 14 2
5 13 15 17 15 16 13 17 2
s1 x1 =0 x1 =1 x1 =2 x1 =3 x1 =4 x1 =5 f1∗ x∗1
5 17 17 19 19 13 9 19 2 (or 3)
⋆ ELI5 Insight
5+3−1
We had $5 to spread across three shops. Instead of trying all
3−1
= 21 combi-
nations, we rst gured out the best way to use any leftover money in Shop 3, then
combined that with Shop 2, and nally Shop 1. Only three small tables instead of
one huge enumeration!
14 Common Mistakes
△ Common Mistake
Mistake 1: Wrong state denition. Choosing a state that does not capture
enough information to satisfy the Markov property. If future costs depend on how
you arrived at a state (not just which state you are in), your DP will give wrong
answers.
Fix: Augment the state until the Markov property holds.
Dynamic Programming in Optimization 19
△ Common Mistake
△ Common Mistake
△ Common Mistake
△ Common Mistake
△ Common Mistake
15 Computational Complexity
⋆ ELI5 Insight
Imagine you have a bookshelf with S books (states) on each of N shelves (stages),
and for each book you must ip through D pages (decisions) to decide the best one.
The total work is roughly N ×S ×D page-ips. Compare this with brute force, which
N
would be D (trying every combination)exponentially worse!
The space complexity is O(N · |S|) to store all value-function tables, or O(|S|) if only
two consecutive stages need to be kept in memory.
Σ Math Translation
1 M Yes
2 M2 Usually
3 M3 Challenging
≥5 M 5+ Often intractable
2. Option Pricing. Binomial-tree models for pricing American options use backward
DP.
All of these problems share the two DP prerequisites: optimal substructure and over-
lapping sub-problems. Recognizing this structure is the key skill that lets you apply
DP broadly.
17 Practice Problems
17.1 Easy
E1. Staircase. You climb a staircase of n steps. At each step you may go up 1 or 2
stairs. In how many distinct ways can you reach the top? Formulate as a DP and
solve for n = 6.
E2. Coin Row. A row of n coins with values v1 , v2 , . . . , vn is laid out. You may pick
coins, but you cannot pick two adjacent coins. Maximize the total value. Formulate
the recurrence.
E3. Minimum Cost Path. Given a 3 × 3 grid where each cell has a positive cost, nd
the minimum cost path from the top-left corner to the bottom-right corner. You
can only move right or down.
Dynamic Programming in Optimization 22
Cost grid:
1 3 1
1 5 1
4 2 1
17.2 Medium
M1. 0/1 Knapsack. A knapsack has capacity W = 7. Items:
1 2 10
2 3 14
3 4 18
4 5 22
Find the maximum value subset using DP. Show the full table.
xi r1 r2 r3
0 0 0 0
1 4 2 6
2 6 5 8
3 7 6 10
4 8 8 11
M3. Shortest Path. Consider the network below. Use backward DP to nd the shortest
path from node 1 to node 7.
2 6 4
3 2
2 4
1 6 7
4
5 5
3 3 5
17.3 Hard
H1. Equipment Replacement. A machine is purchased at the start of year 1. At the
beginning of each year k = 1, 2, 3, 4 you may keep the machine (paying maintenance
cost mk that increases with age) or replace it (paying purchase price P and resetting
age to 0). Given:
P = 10, salvage value at end of year 4 depends on age, m = (2, 4, 7, 11) for ages
1, 2, 3, 4. Salvage values: age 1: 8, age 2: 5, age 3: 3, age 4: 1.
H2. ProductionInventory. Demand over 4 periods is d = (3, 5, 4, 2). You can pro-
duce up to 6 units per period at cost cp = 2 per unit, plus a xed setup cost of
K=8 whenever production is positive. Holding cost is h=1 per unit per period.
Initial inventory is 0; nal inventory must be 0. Minimize total cost.
18 Exam-Level Questions
Q1. (Theory, 10 marks.) State and prove Bellman's Principle of Optimality. Illus-
trate with an example of a 3-stage shortest-path problem.
0 0 0 0 0
1 5 4 6 3
2 9 7 10 7
3 11 9 13 9
4 14 12 15 12
5 16 14 17 14
6 17 16 19 16
Q3. (Shortest Path, 15 marks.) Given the following network with 8 nodes and the
arc costs shown, nd the shortest path from node 1 to node 8 using forward DP.
Draw the network, dene stages, and show all computation tables.
Q5. (Critical Thinking, 12 marks.) A student claims: DP always gives the global
optimum faster than any other method. Critically evaluate this statement. Discuss
19 Selected Solutions
Solution to E1 (Staircase)
Let W (n) = number of ways to climb n stairs.
Recurrence: W (n) = W (n − 1) + W (n − 2), with W (1) = 1, W (2) = 2.
n 1 2 3 4 5 6
W (n) 1 2 3 5 8 13
Row 1 1 4 5
Row 2 2 7 6
Row 3 6 8 7
Minimum cost path: (1, 1) → (2, 1) → (2, 2) → (2, 3) → (3, 3)? Let us check:
(1, 1) → (1, 2) → (1, 3) → (2, 3) → (3, 3) = 1 + 3 + 1 + 1 + 1 = 7.
Optimal cost = 7. Path: (1, 1) → (1, 2) → (1, 3) → (2, 3) → (3, 3).
Node Calculation f∗ d∗
7 boundary 0
6 4+0=4 4 →7
5 → 6 : 7 + 4 = 11; → 7 : 3 + 0 = 3 3 →7
4 → 6 : 2 + 4 = 6; → 7 : 6 + 0 = 6 6 → 6 (tie)
3 → 4 : 8 + 6 = 14; → 5 : 1 + 3 = 4 4 →5
2 → 4 : 5 + 6 = 11; → 5 : 3 + 3 = 6 6 →5
1 → 2 : 4 + 6 = 10; → 3 : 2 + 4 = 6 6 →3
Dynamic Programming in Optimization 26
2 1 3
1→
− 3→
− 5→
− 7, Total cost = 2 + 1 + 3 = 6.
Node Calculation g∗
1 boundary 0
∗
2 g (1) + 4 = 4 4
3 g ∗ (1) + 2 = 2 2
4 min(g ∗ (2) + 5, ∗
g (3) + 8) = min(9, 10) 9
5 min(g ∗ (2) + 3, g ∗ (3) + 1) = min(7, 3) 3
6 min(g ∗ (4) + 2, g ∗ (5) + 7) = min(11, 10) 10
7 min(g ∗ (4) + 6, g ∗ (5) + 3) = min(15, 6) 6
8 min(g ∗ (6) + 1, g ∗ (7) + 4) = min(11, 10) 10
Tracing back: 8 ← 7 ← 5 ← 3 ← 1.
2 1 3 4
1→
− 3→
− 5→
− 7→
− 8, Total cost = 2 + 1 + 3 + 4 = 10.
End of Document