DynamicProgramming
DynamicProgramming
Fibonacchi Numbers
𝑓𝑖𝑏 𝑛 = 𝑓𝑖𝑏 𝑛 − 1 + 𝑓𝑖𝑏 𝑛 − 2
• Write a program to find the nth fibonacchi number.
def Fibonacci(n):
if n == 0: return 0
if n == 1: return 1
return Fibonacci(n-1) + Fibonacci(n-2)
• Runtime analysis
𝑇 𝑛 = 𝑇 𝑛 − 1 + 𝑇 𝑛 − 2 + 𝑂(1)
𝑇 𝑛 = Ω(2𝑛/2 )
What’s going on? That’s a lot of
repeated
Consider Fib(8) computation!
8
6 7
4 5 5 6
2 3 3 4 3 4 4 5
0 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4
0 1
1 2 1 2 ……
0 1 0 1 1 2 0 1 0 1 0 1 1 2
0 1 0 1 0 1 0 1
Fibonacchi Numbers
• How to avoid repeated computations?
• Store already computed results
def Fibonacci(n):
local fib[n+1]
fib[0] = 0
fib[1] = 1
for i -> 2 to n:
fib[i] = fib[i-1] + fib[i-2]
return fib[n]
What did we do?
Dynamic Programming!!!
Dynamic Programming
• It is an algorithm design paradigm
• like divide-and-conquer is an algorithm design paradigm.
• Usually, it is for solving optimization problems
• E.g., shortest path, minimum/maximum profit, longest sequences
• (Fibonacci numbers aren’t an optimization problem, but they are a good
example of DP anyway…)
Elements of dynamic programming
1. Optimal Sub-structure Property
• Big problems break up into sub-problems.
• The solution to a problem can be expressed in terms of solutions to
smaller sub-problems.
• Fibonacci:
𝑓𝑖𝑏 𝑛 = 𝑓𝑖𝑏 𝑛 − 1 + 𝑓𝑖𝑏(𝑛 − 2)
Elements of dynamic programming
2. Overlapping Sub-Problem Property
• The sub-problems overlap.
• Fibonacci:
• Both fib[i+1] and fib[i+2] directly use fib[i].
• And lots of different fib[i+x] indirectly use fib[i].
• This means that we can save time by solving a sub-problem just once
and storing the answer.
Elements of dynamic programming
1. Optimal substructure.
• Optimal solutions to sub-problems can be used to find the optimal
solution of the original problem.
2. Overlapping subproblems.
• The subproblems show up again and again
6 7
4 5 5 6
2 3 3 4 3 4 4 5
0 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4
1 2 etc
0 1 1 2
0 1 0 1 1 2 0 1 0 1 0 1 1 2
0 1 0 1 0 1 0 1
13
Top-down Approach
Nodes Pruned
8
6 7
4 5 5 6
2 3 3 4 3 4 4 5
0 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4
1 2 etc
0 1 1 2
0 1 0 1 1 2 0 1 0 1 0 1 1 2
0 1 0 1 0 1 0 1
14
Bottom-up Approach 8
7
• Solve the small problems first
• fill in fib[0],fib[1]
6
• Then bigger problems
• fill in fib[2] 5
•…
4
• Then bigger problems
• fill in fib[n-1] 3
• fill in fib[n] 1
0
Rod-cutting Problem
• Given
• A rod of length n
• A table of prices 𝑝𝑖 for 𝑖 = 1, 2, … , 𝑛,
• Determine the maximum revenue 𝑟𝑛 obtainable by cutting up the rod
and selling all the pieces.
• For example,
Rod-cutting Problem
Rod-cutting Problem
• An optimal solution involving k pieces,
• Each piece has length 𝑖1 , 𝑖2 , 𝑖3 , … , 𝑖𝑘
• 𝑛 = 𝑖1 + 𝑖2 + 𝑖3 + ⋯ + 𝑖𝑘
• The optimal revenue, 𝑟𝑛 = 𝑝𝑖1 + 𝑝𝑖2 + ⋯ + 𝑝𝑖𝑘
Rod-cutting Problem
• Once an initial cut is made,
• The two resulting smaller pieces will be cut independently
• Smaller instance of the rod-cutting problem
• Optimal Sub-structure Property
• Different pieces can be cut into same length pieces (on not)
• Overlapping Sub-structure Property
Rod-cutting Problem
• Assuming an initial cut is made,
• 𝑟𝑛 = max(𝑝𝑛 , 𝑟1 + 𝑟𝑛−1 , 𝑟2 + 𝑟𝑛−2 , … 𝑟𝑛−1 + 𝑟1 )
Runtime O(2𝑛−1 )
Rod-cutting Problem (Top-down Approach)
Rod-cutting Problem (Bottom-up Approach)
Runtime 𝑂(𝑛2 )
Rod-cutting Problem (Bottom-up Approach)
• Reconstruct the choices that led to the optimal solution
• Example,
• 𝐴1 , 𝐴2 , 𝐴3 with dimensions 10 * 100, 100 * 5, 5 * 50
• ((𝐴1 , 𝐴2 ), 𝐴3 ) perfoms a total of 7500 scalar multiplication
• (𝐴1 , (𝐴2 , 𝐴3 )) perfoms a total of 75000 scalar multiplication
Matrix Chain Multiplication
• Parenthesizing resolves ambiguity in multiplication order
• Fully parenthesized chain of matrices
• Either a single matrix
• Or the product of two fully parenthesized matrix products, surrounded by
parentheses
• Example,
• < 𝐴1 , 𝐴2 , 𝐴3 , 𝐴4 > can be parenthesized in 5 distinct ways.
• (𝐴1 , (𝐴2 , (𝐴3 , 𝐴4 ))), (𝐴1 , ((𝐴2 , 𝐴3 ), 𝐴4 )), ((𝐴1 , 𝐴2 ), (𝐴3 , 𝐴4 )),
((𝐴1 , (𝐴2 , 𝐴3 )), 𝐴4 ), (((𝐴1 , 𝐴2 ), 𝐴3 ), 𝐴4 )
Matrix Chain Multiplication
• Given a chain of n matrices, 𝐴1 , 𝐴2 , … , 𝐴𝑛
• Matrix 𝐴𝑖 has dimensions 𝑝𝑖−1 ∗ 𝑝𝑖
• Goal: Fully parenthesize the product to minimize the number of scalar
multiplications
• Let, the first split occurs between kth and (k+1)st matrices
𝑚 𝑖, 𝑗 = 𝑚 𝑖, 𝑘 + 𝑚 𝑘 + 1, 𝑗 + 𝑝𝑖−1 ∗ 𝑝𝑘 ∗ 𝑝𝑗
• We need to consider all such splits, i.e., all values of k
Matrix Chain Multiplication
• The optimal parenthesization
• Split the product 𝐴𝑖:𝑗 between 𝐴𝑘 and 𝐴𝑘+1 for some value of 𝑖 ≤ 𝑘 < 𝑗
𝑚 𝑖, 𝑗 = 𝑚 𝑖, 𝑘 + 𝑚 𝑘, +1 𝑗 + 𝑝𝑖−1 ∗ 𝑝𝑘 ∗ 𝑝𝑗
• We need to consider all such splits, i.e., all values of k
5 2500 1000 0
6 3500 5000 0
Matrix Chain Multiplication
𝐴1 𝐴2 𝐴3 𝐴4 𝐴5 𝐴6
30 * 35 35 * 15 15 * 5 5 *10 10 * 20 20 * 25
j\i 1 2 3 4 5 6
1 0
2. Overlapping subproblems.
• The subproblems show up again and again
Optimal Substructure Property
• Solution to sub-problems are included in the optimal solution
• Rod-cutting
• Solution to smaller pieces are also part of the solution to the entire rod
• S1 = ACCGGTCGAGTGCGCGGAAGCCGGCCGAA
• S2 = GTCGTTCGGAATGCCGTTGCTCTGTAAA
Longest Common Subsequence
• A strand of DNA consists of a string of molecules called bases
• Adenine, Cytosine, Guanine, and Thymine
• ACGT
𝑐𝑜𝑠𝑡 𝑀
= 𝑐𝑜𝑠𝑡𝑚𝑖𝑠𝑚𝑎𝑡𝑐ℎ + 𝑐𝑜𝑠𝑡𝑔𝑎𝑝 + 𝑐𝑜𝑠𝑡𝑔𝑎𝑝
𝑥𝑖 ,𝑦𝑗 ∈𝑀 𝑥𝑖 𝑦𝑗
𝑢𝑛𝑚𝑎𝑡𝑐ℎ𝑒𝑑 𝑢𝑛𝑚𝑎𝑡𝑐ℎ𝑒𝑑
Sequence Alignment
• Given two sequences
• 𝑥1 𝑥2 … 𝑥𝑛 and 𝑦1 𝑦2 … 𝑦𝑛