DynamicProgramming
DynamicProgramming
Fibonacchi Numbers
• Runtime analysis
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
1 2 ……
0 1 0 1 0 1 1 2 0 1 0 1 0 1 1 2 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:
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 0 1 1 2 0 1 0 1 1 2
0 1 0 1 1 2
0 1 0 1 0 1 130 1
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 0 1 1 2 0 1 0 1 1 2
0 1 0 1 1 2
0 1 0 1 0 1 140 1
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 ,
• 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
• Different pieces can be cut into same length pieces (on not)
• Overlapping Sub-structure Property
Rod-cutting Problem
• Assuming an initial cut is made,
Runtime
Rod-cutting Problem (Top-down
Approach)
Rod-cutting Problem (Bottom-up
Approach)
Runtime
Rod-cutting Problem (Bottom-up
Approach)
• Reconstruct the choices that led to the optimal solution
• Example,
• with dimensions 10 * 100, 100 * 5, 5 * 50
• perfoms a total of 7500 scalar multiplication
• 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,
• can be parenthesized in 5 distinct ways.
• ,,,
Matrix Chain Multiplication
• Given a chain of n matrices,
• Matrix has dimensions
• Goal: Fully parenthesize the product to minimize the number
of scalar multiplications
• Let, the first split occurs between kth and (k+1)st matrices
𝑛
• How many possible ways? Ω(2 )
Matrix Chain Multiplication
• Let, The minimum number of scalar multiplications needed
to compute the matrix
• We need to find
Matrix Chain Multiplication
• The optimal parenthesization
• Split the product between and for some value of
5 2500 1000 0
6 3500 5000 0
Matrix Chain Multiplication
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
• Given a sequence
• Another sequence is a subsequence of X
• If there exists a strictly increasing sequence indices of X such
that for all ,
Longest Common Subsequence
• A strand of DNA consists of a string of molecules called
bases
• Adenine, Cytosine, Guanine, and Thymine
• ACGT