0% found this document useful (0 votes)
12 views49 pages

Lec 7&8 - Dynamic Programming

This document covers dynamic programming, including its definition, approaches (top-down and bottom-up), and applications such as the Fibonacci numbers, rod-cutting problem, matrix chain multiplication, and longest common subsequence. It emphasizes the importance of optimal substructure and overlapping subproblems in optimization problems. The document also provides examples and algorithms for solving these problems efficiently.

Uploaded by

laptop2112006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views49 pages

Lec 7&8 - Dynamic Programming

This document covers dynamic programming, including its definition, approaches (top-down and bottom-up), and applications such as the Fibonacci numbers, rod-cutting problem, matrix chain multiplication, and longest common subsequence. It emphasizes the importance of optimal substructure and overlapping subproblems in optimization problems. The document also provides examples and algorithms for solving these problems efficiently.

Uploaded by

laptop2112006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Algorithm Design and

Analysis
LECTURE 7 : DYNAMIC PROGRAMMING
Contents
▪ Fibonacci Numbers problem
▪ Dynamic programming Definition
▪ Rod-cutting problem
▪ Top-down approach (Memoization)
▪ Bottom-up approach
▪ Dynamic programming steps
▪ Elements of Dynamic Programming
▪ Matrix Chain Multiplication Problem
▪Longest Common Subsequence problem

ALGORITHM DESIGN AND ANALYSIS 2


Fibonacci Numbers
▪ The Fibonacci numbers are the elements of the int Fib(int n)
sequence: {
if (n <= 1)
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ......
return 1;
in his example of Breeding Rabbits. else
▪ Computing the nth Fibonacci number recursively: return Fib(n - 1) + Fib(n - 2);
➢ F(n) = F(n-1) + F(n-2) }
➢F(1) = 1

ALGORITHM DESIGN AND ANALYSIS 3


Fibonacci Numbers
Fib(5)
+

Fib(4) Fib(3)
+ +

Fib(3) Fib(2) Fib(2) Fib(1)


+ + +

Fib(2) Fib(1) Fib(1) Fib(0) Fib(1) Fib(0)


+

Fib(1) Fib(0)
Dynamic programming
▪ Dynamic programming is typically applied to optimization problems. In
such problem there can be many solutions. Each solution has a value, and we
wish to find a solution with the optimal value.
▪ Like the divide-and-conquer method, it solves problems by combining the
solutions to subproblems.
▪ Unlike divide and conquer, sub-problems are not independent. Sub-problems
may share sub-sub-problems.

ALGORITHM DESIGN AND ANALYSIS 5


Dynamic programming
▪ Each subproblem is solved only once, saving its solution.
▪ During the computation, if we need the solution to a subproblem, we
simply look it up rather than recomputing it.
▪ Two dynamic programming approaches:
➢Top-down approach (Memoization)
➢Bottom-up approach.

ALGORITHM DESIGN AND ANALYSIS 6


Fibonacci Numbers using dynamic programming
– Computing the nth Fibonacci number using a bottom-up approach:
– F(0) = 0 , F(1) = 1
– F(2) = 1+0 = 1
– …
– F(n) = F(n-1) + F(n-2)
• Efficiency:
– Time – O(n) - Space – O(n)

ALGORITHM DESIGN AND ANALYSIS 7


Dynamic programming vs Divide & Conquer

ALGORITHM DESIGN AND ANALYSIS 8


Rod-cutting problem
Problem: Given a rod of length n inches and a table of prices pi (i is in the range
1, 2, …, n), determine the maximum revenue rn obtained by cutting the rod and
selling the pieces. If no cutting gives the best price, we don’t cut at all.
Note that: there is no cost for cutting.

ALGORITHM DESIGN AND ANALYSIS 9


Rod-cutting problem
Given a rod of 4 inches, we have 8 possible ways of cutting the rod.
We can cut the rod of length n in 2n-1 different ways.

ALGORITHM DESIGN AND ANALYSIS 10


Rod-cutting problem
For our sample problem, we can determine the optimal revenue figures ri, for i =1, 2, 3……10
by inspection, with the corresponding optimal decompositions:

ALGORITHM DESIGN AND ANALYSIS 11


Rod-cutting problem
The optimal revenue from a rod of length n, rn

Where pn id the price with no cut at all, r1 + rn-1 is a cut so that we have rods of length 1
and n-1.

ALGORITHM DESIGN AND ANALYSIS 12


Rod-cutting problem
General formula:

Where pi is the price of rod of length i, rn-1 is the revenue from rod of length n-1.
Recursive approach
For n=4:
r4 = max (p1 + r3 ,
p2 + r2 ,
p3 + r1 ,
p4 + r0)

ALGORITHM DESIGN AND ANALYSIS 13


Recursive implementation
The running time of the recursive algorithm is exponential in n.
T(n) = 2n

ALGORITHM DESIGN AND ANALYSIS 14


Bottom-up approach

Θ(n2) i=1 q = max(q, p1 + r[4])


i=2 q = max(q, p2 + r[3])
Note that: subproblem i is smaller than subproblem i=3 q = max(q, p3 + r[2])
j, so the problems are solved in the order i=4 q = max(q, p4 + r[1])
0, 1, 2, 3,……n i=5 q = max(q, p5 + r[0])
ALGORITHM DESIGN AND ANALYSIS 15
Bottom-up approach
Example:

p1 + r4 = 1 + 10 = 11
p2 + r3 = 5 + 8 = 13
r5 = max p3 + r2 = 8 + 5 = 13
p4 + r1 = 9 + 1 = 10
p5 = 10
ALGORITHM DESIGN AND ANALYSIS 16
Elements of Dynamic Programming
When should we look for a dynamic programming solution to a problem?

Two key ingredients that an optimization problem must have :

1. Optimal substructure: a problem exhibits optimal substructure if an optimal solution to


the problem contains within it optimal solutions to subproblems.

2. Overlapping subproblems: When a recursive algorithm revisits the same problem


repeatedly, we say that the optimization problem has overlapping subproblems.

In other words: The number of unique subproblems < The number of the total subproblems.

ALGORITHM DESIGN AND ANALYSIS 17


Matrix Chain Multiplication Problem
Problem: We are given a sequence (chain) ‹ A1,A2, …. , An › of n matrices to be
multiplied, and we wish to compute the product A1 × A2 × A3 × · · · × An that minimizes
the number of scalar multiplications.

Number of Multiplications = 4 * 6 * 3 = 72

ALGORITHM DESIGN AND ANALYSIS 18


Matrix Chain Multiplication Problem
In what order should we multiply the matrices A1  A2  An ?

Parenthesize the product to get the order in which matrices are multiplied.

E.g.: A1  A2  A3 = ((A1  A2)  A3)

OR = (A1  (A2  A3))


Which one of these orderings should we choose?

The order in which we multiply the matrices has a significant impact on the cost of
evaluating the product.

ALGORITHM DESIGN AND ANALYSIS 19


Example
A1: 10 x 100 , A2: 100 x 5 , A3: 5 x 50
Solution 1 : ((A1  A2)  A3): A1  A2 = 10 x 100 x 5 = 5,000 multiplications

10 X 5
((A1  A2)  A3) = 10 x 5 x 50 = 2,500 multiplications
Total: 7,500 scalar multiplications
Solution 2 : (A1  (A2  A3)): A2  A3 = 100 x 5 x 50 = 25,000

100 X 50
(A1  (A2  A3)) = 10 x 100 x 50 = 50,000
Total: 75,000 scalar multiplications
ALGORITHM DESIGN AND ANALYSIS 20
Matrix Chain Parenthesization Problem
▪ We write: Aij for the product Ai × Ai+1 × · · · × Aj
▪ There is a final matrix multiplication: A1k × A(k+1)n , for some 1 ≤ k ≤ n − 1.
▪ we obtain the recurrence:

How many Parenthesizations P(n) are there when n=4?

ALGORITHM DESIGN AND ANALYSIS 21


Matrix Chain Parenthesization Problem
A Bound on the number of Parenthesizations:

➢ The number of parenthesizations grows as :


➢ The brute force method : 4n => Exponential
➢ A better way: use the dynamic-programming method to determine how to optimally
parenthesize a matrix chain.

ALGORITHM DESIGN AND ANALYSIS 22


Optimal Substructure
▪ Given a chain of matrices A1, A2, …, An, where Ai has dimensions pi-1x pi,

A1 × A2  Ai × Ai+1  An

p 0 x p1 p1 x p 2 pi-1 x pi pi x pi+1 pn-1 x pn


▪ The optimal solution can be defined in terms of optimal sub-problems.
▪ Suppose that an optimal parenthesization of Ai…j splits the product into:
(Ai…k) and (Ak+1…j) where ik<j
▪ We want to find at which k we can return the fewest number of multiplications.

ALGORITHM DESIGN AND ANALYSIS 23


Recursive Solution
▪ Let m[i, j] = the minimum number of multiplications needed to compute Ai…j
▪ Assume that the optimal parenthesization splits the product Ai  Aj at k:

pi-1pkpj
Ai…j = Ai…k Ak+1…j for i  k < j
m[i, k] m[k+1,j]

m[i, j] = m[i, k] + m[k+1, j] + pi-1pkpj


min # of multiplications min # of multiplications # of multiplications
to compute Ai…k to compute Ak+1…j to compute Ai…kAk…j

ALGORITHM DESIGN AND ANALYSIS 24


Recursive Solution
Example : Ai…j = Ai…k Ak+1…j

2 X 3 3 X 4

m[i, j] = m[i, k] + m[k+1, j] + pi-1pkpj


= 100 + 200 + 24
100 200 2X3X4

➢ Since we do not know the value of k, we will try for i ≤ k < j and the
minimum value of m is the solution.

ALGORITHM DESIGN AND ANALYSIS 25


Recursive Solution
The recursive definition for the minimum cost of parenthesizing the
product Ai Aj :

Example:

i j

ALGORITHM DESIGN AND ANALYSIS 26


Computing the optimal costs

➢ The running time of this algorithm is Ω(n3)


➢ The algorithm requires Θ(n2) space to store the m table.
ALGORITHM DESIGN AND ANALYSIS 27
Computing the optimal costs
Example: n = 4 and p =  3 7 6 2 9 

ALGORITHM DESIGN AND ANALYSIS 28


Computing the optimal costs
Example: n = 4 and p =  3 7 6 2 9 

,k=1

ALGORITHM DESIGN AND ANALYSIS 29


Computing the optimal costs
Example: n = 4 and p =  3 7 6 2 9 

,k=2

ALGORITHM DESIGN AND ANALYSIS 30


Computing the optimal costs
Example: n = 4 and p =  3 7 6 2 9 

,k=3

ALGORITHM DESIGN AND ANALYSIS 31


Computing the optimal costs
Example: n = 4 and p =  3 7 6 2 9 

k=1
126
126 k=2

ALGORITHM DESIGN AND ANALYSIS 32


Computing the optimal costs
Example: n = 4 and p =  3 7 6 2 9 

k=2

126 k=3

126 = 210

ALGORITHM DESIGN AND ANALYSIS 33


Computing the optimal costs
Example: n = 4 and p =  3 7 6 2 9 

k = 1:
k = 2:
126
k = 3: 126 + 0 + 3 . 2 . 9 = 180
180

ALGORITHM DESIGN AND ANALYSIS 34


Computing the optimal costs
For the sequence of dimensions  3 7 6 2 9 , the algorithm outputs the value of
the optimal solution: m[1, 4] = 180
The optimal parenthesization:
1. m[1,4] , the minimum value was obtained when k = 3
Put parenthesis between A3 and A4
(A1 × A2 × A3) × A4
2. m[1,3] , the minimum value was obtained when k = 1
Put parenthesis between A1 and A2
(A1 ×( A2 × A3)) × A4

ALGORITHM DESIGN AND ANALYSIS 35


Longest Common Subsequence
Given two sequences
X = x1, x2, …, xm
Y = y1, y2, …, yn
find a maximum length common subsequence (LCS) of X and Y
E.g.:
X = A, B, C, B, D, A, B
Subsequences of X:
◦ A subset of elements in the sequence taken in order
A, B, D, B, C, D, B, etc.

ALGORITHM DESIGN AND ANALYSIS 36


Longest Common Subsequence
X = A, B, C, B, D, A, B X = A, B, C, B, D, A, B X = A, B, C, B, D, A, B X = A, B, C, B, D, A, B

Y = B, D, C, A, B, A Y = B, D, C, A, B, A Y = B, D, C, A, B, A Y = B, D, C, A, B, A

B, C, A and B, D, A are common subsequences

But, B, C, B, A and B, D, A, B are longest common subsequences of X and Y (length = 4)

ALGORITHM DESIGN AND ANALYSIS 37


Longest Common Subsequence
Brute-force approach:
➢ Enumerate all subsequences of X.
➢ Check each subsequence to see whether it is also a subsequence of Y
➢ Keep track of the longest subsequence.
➢ Because X has 2m subsequences, this approach requires exponential time.

ALGORITHM DESIGN AND ANALYSIS 38


Optimal substructure
LCS problem has optimal substructure Solutions of subproblems are parts of the final solution.

➢Append xm = yn to the LCS of X and Y. X = A, B, D, E 


➢Must find a LCS of Xi-1 and Yj-1
Y = Z, B, E 
 Optimal solution to a problem includes optimal solutions to subproblems

ALGORITHM DESIGN AND ANALYSIS 39


Optimal substructure
LCS problem has optimal substructure Solutions of subproblems are parts of the final solution.

➢Find a LCS of Xi-1 and Yj: Xi-1 = A, B, D and Yj = Z, B, G


X = A, B, D, E
➢Find a LCS of Xi and Yj-1: Xi = A, B, D, E and Yj = Z, B Y = Z, B, G
 Optimal solution to a problem includes optimal solutions to subproblems

ALGORITHM DESIGN AND ANALYSIS 40


Recursive solution
Let’s define: c[i, j]: The length of a LCS of the sequences: Xi = x1, …, xi and Yj = y1, …, yj

Case 1: xi = yj Xi = A, B, D, E Yj = Z, B, E

c[i, j] = c[i - 1, j - 1] + 1

Case 2: xi  yj Xi = A, B, D, G Yj = Z, B, D


◦ Must solve two problems
◦ Find a LCS of Xi-1 and Yj: Xi-1 = A, B, D and Yj = Z, B, D
◦ Find a LCS of Xi and Yj-1: Xi = A, B, D, G and Yj = Z, B

c[i, j] = max { c[i - 1, j], c[i, j-1] }

ALGORITHM DESIGN AND ANALYSIS 41


Overlapping Subproblems
To find a LCS of X and Y, we may need to find the LCSs of:

▪ X and Yn-1 and

▪ Xm-1 and Y

➢Both the above subproblems has the subproblem of finding the LCS of Xm-1 and Yn-1

➢Many other subproblems share sub-sub-problems.

ALGORITHM DESIGN AND ANALYSIS 42


Computing the Length of the LCS

➢We start with i = 0 or j = 0 (empty substrings of x and y)

➢Since X0 and Y0 are empty strings, their LCS is always


empty (i.e. c[0,0] = 0)

➢LCS of empty string and any other string is empty, so for


every i and j: c[0, j] = c[i,0] = 0

ALGORITHM DESIGN AND ANALYSIS 43


Computing the Length of the LCS
Calculate:
c[1,j] for 1<=j<= n,
c[2,j] for 1<=j<= n,

▪If x[i]=y[j]: one more symbol in strings X and Y


matches,
➢ The length of LCS Xi and Yj equals to the length of
LCS of smaller strings Xi-1 and Yi-1 , plus 1
▪If x[i]  y[j]: symbols don’t match,
➢ The length of LCS(Xi , Yj) is the same as before (i.e.
maximum of LCS(Xi, Yj-1) and LCS(Xi-1,Yj)

ALGORITHM DESIGN AND ANALYSIS 44


Computing the Length of the LCS

▪ A matrix b[i, j]: For a subproblem [i, j] it tells us what choice


y4
was made to obtain the optimal value:
If xi = yj
b[i, j] = “ ”
x3
Else, if c[i - 1, j] ≥ c[i, j-1]
ifIfx[3
x[3] y[4]
] =y[4]
b[i, j] = “  ” C[3,4]C[3,4] = +1
= c[2,3]
max (c[3,3] , c[2,4] )
else b[i, j] = “  ”
ALGORITHM DESIGN AND ANALYSIS 45
Computing the Length of the LCS

The length of the LCS if one of the sequences


is empty is zero

Case 1: xi = yj

Running time: (mn)


Case 2: xi  yj

ALGORITHM DESIGN AND ANALYSIS 46


Example
Find the LCS between the following sequences: 0 1 2 3 4 5 6
yj B D C A B A
X = A, B, C, B, D, A
0 xi 0 0 0 0 0 0 0
Y = B, D, C, A, B, A   
1 A 0 0 0 0 1 1 1
if xi = yj 2 B 
0 1 1 1 1 2 2
b[i, j] = “ ” 3 C    
0 1 1 2 2 2 2
else if c[i - 1, j] ≥ c[i, j-1]   
4 B 0 1 1 2 2 3 3
b[i, j] = “  ”     
5 D 0 1 2 2 2 3 3
else    
6 A 0 1 2 2 3 3 4
b[i, j] = “  ”    
7 B 0 1 2 2 3 4 4
Example
0 1 2 3 4 5 6
➢Start at b[m, n] and follow the arrows.
yj B D C A B A
➢When we encounter a “ “ in b[i, j] 0 xi 0 0 0 0 0 0 0
 xi = yj is an element of the LCS 1 A   
0 0 0 0 1 1 1
2 B 
0 1 1 1 1 2 2
3 C    
0 1 1 2 2 2 2
4 B   
0 1 1 2 2 3 3
5 D     
0 1 2 2 2 3 3
   
6 A 0 1 2 2 3 3 4
   
7 B 0 1 2 2 3 4 4
Questions

ALGORITHM DESIGN AND ANALYSIS 49

You might also like