10 Dynamic Programing DP
10 Dynamic Programing DP
OF ALGORITHMS
1
Dynamic Programing
Dynamic Programming (DP) is an optimization technique that breaks a large, complex
problem into interdependent subproblems, computes each subproblem exactly once, and
caches their results for reuse in subsequent computations.
Core Ideas
Dynamic
Feature Brute-Force Divide & Conquer
Programming
Subproblem sharing None Rare Heavy (cached)
Time complexity Exponential Often exponential Typically polynomial
Overlapping
Typical use cases Tiny inputs Independent parts
subproblems
DP Design Steps
1. State Definition
•Identify the parameters that define each subproblem.
•Example (Knapsack):
•dp[i][j]=maximum value using the first i items with capacity j
2. Recurrence Relation
• Fibonacci Sequance
• 0-1 Knapsack Problem
• Longest Common Subsequence (LCS)
Fibonacci Sequence
Let’s examine the Fibonacci example step by step, illustrating both how and why
Dynamic Programming (DP) improves over naïve recursion, and exploring
optimizations in time and space.
Problem Statement
Compute the 𝑛th Fibonacci number, where
Given items with weights wi and values vi, and a capacity W, the 0-1 Knapsack asks for the
maximum total value of a subset whose weights sum to at most W. DP defines dp[i][j] as
the best value using the first iii items within capacity j. Filling this table takes O(nW) time,
and space can be cut to O(W) by using a single array in reverse. It’s widely applied in
resource allocation, budgeting, and cargo loading where discrete choices must be optimized
under a limit.
LCS finds the longest sequence appearing (not necessarily contiguously) in both strings A
and B. Define dp[i][j] as the LCS length of prefixes A[1..i], B[1..j]B. If A[i]=B[j], take
dp[i−1][j−1]+1; otherwise take the max of dropping one character. The table fills in O(mn)
time and O(mn) space. LCS is fundamental in diff/merge tools, version control, and
bioinformatics for identifying shared patterns.
Coin Collecting Problem
Problem Statement:
Several coins are placed in cells of an n x m board, no more than one coin per cell.
A robot, located in the upper left cell of the board, needs to collect as many of the coins as
possible and bring them to the bottom right cell.
On each step, the robot can move either one cell to the right or one cell down from its
current location. When the robot visits a cell with a coin, it always picks up that coin.
Design an algorithm to find the maximum number of coins the robot can collect and a path
it needs to follow to do this.
Let’s create Dynamic Programing Table
Example
Let’s create Dynamic Programing Table
Example Problem
An airline must schedule a single aircraft for the day.You are given n flight offers; each
flight j has:
•Departure time sj
•Arrival time fj (fj>sj))
•Profit pj
Since the aircraft cannot operate more than one flight at the same time, select a subset
of non-overlapping flights to maximize total profit.
Data Set
or 0 if no such i exists.
j sj fj prev[j] Reason
1 05:45 07:30 0 —
Flight 1 arrives at 07:30 > 06:20 ⇒
2 06:20 08:10 0
none
3 07:00 09:15 0 F1, F2 arrive after 07:00
F2 (08:10) ≤ 09:10, but F3 arrives
4 09:10 11:20 2
at 09:15 > 09:10
5 10:30 12:05 3 F3 (09:15) ≤ 10:30
6 12:15 14:00 5 F6 (12:05) ≤ 12:15
7 14:05 16:30 6 F8 (14:00) ≤ 14:05
2. DP Definition & Recurrence
3. Fill the DP Table
Include = Exclude =
j prev[j] pj dp[j]
pj+dp[prev[j]] dp[j−1]
0 — — — — 0
1 0 420 420 + 0 = 420 0 420
2 0 380 380 + 0 = 380 420 420
3 0 510 510 + 0 = 510 420 510
4 2 600 600 + 420 = 1020 510 1020
5 3 550 550 + 510 = 1060 1020 1060
6 5 480 480 + 1060 = 1540 1060 1540
7 6 800 800 + 1540 = 2340 1540 2340
4. Result & Backtracking
•Maximum Profit:
•dp[7]=2340 USD..
•Selected Flights:
Backtrack from j=7j=7j=7:
• dp[7] came from “include” ⇒ select Flight 7, jump to j=prev[7]=6
• dp[6] came from “include” ⇒ select Flight 6, jump to j=5
• dp[5] came from “include” ⇒ select Flight 5, jump to j=3
• dp[3] came from “include” ⇒ select Flight 3, jump to j=0
•Chosen: Flights 3, 5, 6, 7
Below you will see a new DP scenario called “Film Festival Theater Planning”. You have a single
theater and movies of different lengths will be shown throughout the day. Each movie has a
start-end time and an expected number of viewers. Your goal is to maximize the total number of
viewers by selecting movies that do not overlap.
j Film Title Start – End Expected Viewers
1 “Dawn of Dreams” 09:00–10:30 200
2 “Midday Melody” 10:00–11:15 150
3 “Noon Noir” 10:45–12:30 300
4 “Afternoon Aura” 12:00–13:30 250
5 “Sunset Sonata” 13:00–14:00 100
6 “Evening Epic” 13:30–15:00 180
7 “Night Narrative” 14:30–16:00 220
Include = viewers_j +
j prev[j] viewers_j Exclude = dp[j-1] dp[j]
dp[prev[j]]
0 — — — — 0
1 0 200 200 + 0 = 200 0 200
2 0 150 150 + 0 = 150 200 200
3 1 300 300 + 200 = 500 200 500
4 2 250 250 + 200 = 450 500 500
5 3 100 100 + 500 = 600 500 600
6 4 180 180 + 500 = 680 600 680
7 5 220 220 + 600 = 820 680 820
Task 4: Pseudocode
Task 4: Pseudocode
HOMEWORK Task 5: Java Implemention