Dynamic Programming.

Slides:



Advertisements
Similar presentations
Lecture 8: Dynamic Programming Shang-Hua Teng. Longest Common Subsequence Biologists need to measure how similar strands of DNA are to determine how closely.
Advertisements

CPSC 335 Dynamic Programming Dr. Marina Gavrilova Computer Science University of Calgary Canada.
Overview What is Dynamic Programming? A Sequence of 4 Steps
Algorithms Dynamic programming Longest Common Subsequence.
COMP8620 Lecture 8 Dynamic Programming.
RAIK 283: Data Structures & Algorithms
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Dynamic Programming1 Modified by: Daniel Gomez-Prado, University of Massachusetts Amherst.
Dynamic Programming Reading Material: Chapter 7 Sections and 6.
Dynamic Programming 0-1 Knapsack These notes are taken from the notes by Dr. Steve Goddard at
Dynamic Programming A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River,
1 Dynamic Programming Jose Rolim University of Geneva.
Lecture 7 Topics Dynamic Programming
Longest Common Subsequence
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Dynamic Programming. Well known algorithm design techniques:. –Divide-and-conquer algorithms Another strategy for designing algorithms is dynamic programming.
ADA: 7. Dynamic Prog.1 Objective o introduce DP, its two hallmarks, and two major programming techniques o look at two examples: the fibonacci.
Dynamic Programming Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by or formulated.
CS 5243: Algorithms Dynamic Programming Dynamic Programming is applicable when sub-problems are dependent! In the case of Divide and Conquer they are.
1 0-1 Knapsack problem Dr. Ying Lu RAIK 283 Data Structures & Algorithms.
CSC401: Analysis of Algorithms CSC401 – Analysis of Algorithms Chapter Dynamic Programming Objectives: Present the Dynamic Programming paradigm.
Greedy Methods and Backtracking Dr. Marina Gavrilova Computer Science University of Calgary Canada.
6/4/ ITCS 6114 Dynamic programming Longest Common Subsequence.
1 Dynamic Programming Topic 07 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
Dynamic Programming.  Decomposes a problem into a series of sub- problems  Builds up correct solutions to larger and larger sub- problems  Examples.
2/19/ ITCS 6114 Dynamic programming 0-1 Knapsack problem.
Dynamic Programming Typically applied to optimization problems
Merge Sort 5/28/2018 9:55 AM Dynamic Programming Dynamic Programming.
Dynamic Programming Sequence of decisions. Problem state.
CS 3343: Analysis of Algorithms
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Chapter 8 Dynamic Programming
Chapter 8 Dynamic Programming.
CSCE 411 Design and Analysis of Algorithms
CS Algorithms Dynamic programming 0-1 Knapsack problem 12/5/2018.
Dynamic Programming Dr. Yingwu Zhu Chapter 15.
Chapter 8 Dynamic Programming
CS 3343: Analysis of Algorithms
Dynamic Programming 1/15/2019 8:22 PM Dynamic Programming.
Merge Sort Dynamic Programming
Dynamic Programming Dynamic Programming 1/15/ :41 PM
Dynamic Programming.
CS6045: Advanced Algorithms
Dynamic Programming.
Dynamic Programming Dynamic Programming 1/18/ :45 AM
Merge Sort 1/18/ :45 AM Dynamic Programming Dynamic Programming.
Dynamic Programming Merge Sort 1/18/ :45 AM Spring 2007
Longest Common Subsequence
Merge Sort 2/22/ :33 AM Dynamic Programming Dynamic Programming.
Lecture 8. Paradigm #6 Dynamic Programming
Dynamic Programming.
DYNAMIC PROGRAMMING.
CSC 413/513- Intro to Algorithms
Algorithm Design Techniques Greedy Approach vs Dynamic Programming
Longest Common Subsequence
Dynamic Programming II DP over Intervals
Merge Sort 4/28/ :13 AM Dynamic Programming Dynamic Programming.
Longest common subsequence (LCS)
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Analysis of Algorithms CS 477/677
0-1 Knapsack problem.
Longest Common Subsequence
Dynamic Programming Merge Sort 5/23/2019 6:18 PM Spring 2008
Dynamic Programming.
Algorithm Course Dr. Aref Rashad
Presentation transcript:

Dynamic Programming

Example 1: Fibonacci Numbers Computing the nth Fibonacci number recursively: F(n) = F(n-1) + F(n-2) F(0) = 0 F(1) = 1 Top-down approach Time complexity? int Fib(int n) {     if (n <= 1)         return 1;     else         return Fib(n - 1) + Fib(n - 2); } F(n) F(n-1) + F(n-2) F(n-2) + F(n-3) F(n-3) + F(n-4) n levels

Example 1: Fibonacci Numbers Computing the nth Fibonacci number using a bottom-up approach: F(0) = 0 F(1) = 1 F(2) = 1+0 = 1 … F(n-2) = F(n-1) = F(n) = F(n-1) + F(n-2) Complexity: Time – O(n) Space – O(n)

Example 1: Fibonacci Numbers The bottom-up approach is only O(n). Why is the top-down so inefficient? Re-computes many sub-problems. How many times is F(n-5) computed? F(n) F(n-1) + F(n-2) F(n-2) + F(n-3) F(n-3) + F(n-4) n levels

Example 1: Fibonacci Numbers + Fib(3) + Fib(4) + Fib(3) + Fib(2) + Fib(1) Fib(2) + Fib(2) + Fib(1) Fib(1) Fib(0) Fib(1) Fib(0) Fib(1) Fib(0)

Dynamic Programming Dynamic Programming is an algorithm design technique for optimization problems: often minimizing or maximizing. Like divide and conquer, DP solves problems by combining solutions to sub-problems. Unlike divide and conquer, sub-problems are not independent. Sub-problems overlap (share sub-sub-problems) Dynamic Programming = meta-technique (not a particular algorithm) Programming = “tableau method’’, not writing a code

Dynamic Programming In Dynamic Programming we usually reduce time by increasing the amount of space We solve the problem by solving sub-problems of increasing size and saving each optimal solution in a table. Time is saved since each sub-problem is solved only once. Main idea: set up a recurrence relating a solution to a larger instance to solutions of some smaller instances - solve smaller instances once record solutions in a table extract solution to the initial instance from that table A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping sub-problems “Programming” here means “tableau method” Main idea: set up a recurrence relating a solution to a larger instance to solutions of some smaller instances - solve smaller instances once record solutions in a table extract solution to the initial instance from that table A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

Example 2: Coin-row problem There is a row of n coins whose values are some positive integers c₁, c₂,...,cn, not necessarily distinct. The goal is to pick up the maximum amount of money subject to the constraint that no two coins adjacent in the initial row can be picked up. E.g.: 5, 1, 2, 10, 6, 2. What is the best selection? Answer: {c1, c4, c6} = {5, 10, 2} A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

DP solution to the coin-row problem Let F(n) be the maximum amount that can be picked up from the row of n coins. To derive a recurrence for F(n), we partition all the allowed coin selections into two groups: those without last coin – the max amount is ? those with the last coin -- the max amount is ? Thus we have the following recurrence F(n) = max{cn + F(n-2), F(n-1)} for n > 1, F(0) = 0, F(1)=c₁ A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

DP solution to the coin-row problem (cont.) F(n) = max{cn + F(n-2), F(n-1)} for n > 1, F(0) = 0, F(1)=c₁ index 1 2 3 4 5 6 coins -- 10 F( ) A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

DP solution to the coin-row problem (cont.) F(n) = max{cn + F(n-2), F(n-1)} for n > 1, F(0) = 0, F(1)=c₁ index 1 2 3 4 5 6 coins -- 10 F( ) F(2) = max{c2 + F(0), F(1)} = max{1 + 0, 5} = 5 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

DP solution to the coin-row problem (cont.) F(n) = max{cn + F(n-2), F(n-1)} for n > 1, F(0) = 0, F(1)=c₁ index 1 2 3 4 5 6 coins -- 10 F( ) 7 F(3) = max{c3 + F(1), F(2)} = max{2 + 5, 5} = 7 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

DP solution to the coin-row problem (cont.) F(n) = max{cn + F(n-2), F(n-1)} for n > 1, F(0) = 0, F(1)=c₁ index 1 2 3 4 5 6 coins -- 10 F( ) 7 15 F(4) = max{c4 + F(2), F(3)} = max{10 + 5, 7} = 15 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

DP solution to the coin-row problem (cont.) F(n) = max{cn + F(n-2), F(n-1)} for n > 1, F(0) = 0, F(1)=c₁ index 1 2 3 4 5 6 coins -- 10 F( ) 7 15 F(5) = max{c5 + F(3), F(4)} = max{6 + 7, 15} = 15 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

DP solution to the coin-row problem (cont.) F(n) = max{cn + F(n-2), F(n-1)} for n > 1, F(0) = 0, F(1)=c₁ index 1 2 3 4 5 6 coins -- 10 F( ) 7 15 17 F(6) = max{c6 + F(4), F(5)} = max{2 + 15, 15} = 17 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

DP solution to the coin-row problem (cont.) F(n) = max{cn + F(n-2), F(n-1)} for n > 1, F(0) = 0, F(1)=c₁ index 1 2 3 4 5 6 coins -- 10 F( ) 7 15 17 Max amount: 17 Coins of optimal solution: {c1, c4, c6} Time efficiency: O(n) Space efficiency: O(n) Note: All smaller instances were solved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

Example: Longest Common Subsequence Longest common subsequence (LCS) problem: Given two sequences X = <x1 x2 … xm> and Y = <y1 y2 … yn>, find the longest subsequence which occurs in both Ex: X = {A B C B D A B }, Y = {B D C A B A} What’s the LCS? X = A B C B D A B Y = B D C A B A Brute-force algorithm: For every subsequence of X, check if it’s a subsequence of Y How many subsequences of X are there? What will be the running time of the brute-force algorithm?

LCS Algorithm Brute-force algorithm: 2m subsequences of X to check against n elements of Y: O(n 2m) We can do better: for now, let’s only worry about the problem of finding the length of LCS Notice LCS problem has overlapped subporblems Subproblems: LCS of pairs of prefixes of X and Y

LCS Algorithm First we’ll find the length of LCS. Later we’ll modify the algorithm to find LCS itself. Define Xi, Yj to be the prefixes of X and Y of length i and j respectively (i ≤ m; j ≤ n) Xi = <x1 x2 … xi>; Yj = <y1 y2 … yj> Define c[i,j] to be the length of LCS of Xi and Yj Then the length of LCS of X and Y will be c[m,n] X = A B C B D A B Y = B D C A B A

LCS recursive solution We start with i = 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

LCS recursive solution When we calculate c[i,j], we consider two cases: First case: x[i]=y[j]: one more symbol in strings X and Y matches, so the length of LCS Xi and Yj equals to the length of LCS of smaller strings Xi-1 and Yj-1 , plus 1

LCS recursive solution Second case: x[i] != y[j] As symbols don’t match, our solution is not improved, and the length of LCS(Xi , Yj) is the same as before (i.e., maximum of LCS(Xi, Yj-1) and LCS(Xi-1,Yj) For example: AB | C; BC | A Why not just take the length of LCS(Xi-1, Yj-1) ?

LCS Length Algorithm LCS-Length(X, Y) 1. m = length(X) // get the # of symbols in X 2. n = length(Y) // get the # of symbols in Y 3. for i = 1 to m c[i,0] = 0 // special case: Y0 4. for j = 1 to n c[0,j] = 0 // special case: X0 5. for i = 1 to m // for all Xi 6. for j = 1 to n // for all Yj 7. if ( Xi == Yj ) 8. c[i,j] = c[i-1,j-1] + 1 9. else c[i,j] = max( c[i-1,j], c[i,j-1] ) 10. return c

LCS Example What is the Longest Common Subsequence of X and Y? We’ll see how LCS algorithm works on the following example: X = ABCB Y = BDCAB What is the Longest Common Subsequence of X and Y? LCS(X, Y) = BCB X = A B C B Y = B D C A B

LCS Example (0) ABCB BDCAB X = ABCB; m = |X| = 4 j 0 1 2 3 4 5 ABCB BDCAB i Yj B D C A B Xi A 1 B 2 3 C 4 B X = ABCB; m = |X| = 4 Y = BDCAB; n = |Y| = 5 Allocate array c[4,5]

LCS Example (1) ABCB BDCAB for i = 1 to m c[i,0] = 0 j 0 1 2 3 4 5 ABCB BDCAB i Yj B D C A B Xi A 1 B 2 3 C 4 B for i = 1 to m c[i,0] = 0 for j = 1 to n c[0,j] = 0

LCS Example (2) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 B 2 3 C A 1 B 2 3 C 4 B if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

LCS Example (3) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 B 2 3 C A 1 B 2 3 C 4 B if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

LCS Example (4) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 B 2 3 A 1 1 B 2 3 C 4 B if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

LCS Example (5) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2 A 1 1 1 B 2 3 C 4 B if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

LCS Example (6) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2 A 1 1 1 B 2 1 3 C 4 B if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

LCS Example (7) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2 A 1 1 1 B 2 1 1 1 1 3 C 4 B if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

LCS Example (8) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2 A 1 1 1 B 2 1 1 1 1 2 3 C 4 B if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

LCS Example (10) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 4 B if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

LCS Example (11) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 2 4 B if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

LCS Example (12) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 2 2 2 4 B if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

LCS Example (13) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 2 2 2 4 B 1 if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

LCS Example (14) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 2 2 2 4 B 1 1 2 2 if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

LCS Example (15) 3 ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 2 2 2 3 4 B 1 1 2 2 if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

LCS Algorithm Running Time LCS algorithm calculates the values of each entry of the array c[m,n] So what is the running time? O(mn) since each c[i,j] is calculated in constant time, and there are mn elements in the array

How to find actual LCS For example, here So far, we have just found the length of LCS, but not LCS itself. We want to modify this algorithm to make it output Longest Common Subsequence of X and Y Each c[i,j] depends on c[i-1,j] and c[i,j-1] or c[i-1, j-1] For each c[i,j] we can say how it was acquired: For example, here c[i,j] = c[i-1,j-1] +1 = 2+1=3 2 2 2 3

Finding LCS 3 ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2 1 A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 2 2 2 3 4 B 1 1 2 2

How to find actual LCS - continued Remember that So we can start from c[m,n] and go backwards Whenever c[i,j] = c[i-1, j-1]+1, remember x[i] (because x[i] is a part of LCS) When i=0 or j=0 (i.e., we reached the beginning), output remembered letters in reverse order

Finding LCS (2) 3 B C B B C B LCS (reversed order): j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 2 2 2 3 4 B 1 1 2 2 B C B LCS (reversed order): LCS (straight order): B C B

Properties of a problem that can be solved with dynamic programming Simple Sub-problems We should be able to break the original problem to smaller sub-problems that have the same structure Optimal Substructure of the problems The solution to the problem must be a composition of sub-problem solutions Sub-problem Overlap Optimal sub-problems to unrelated problems can contain sub-problems in common

Example 3: Change-making Problem Give change for amount n using the minimum number of coins of denominations d1 < d2 < … < dm. Example: n = 6, coin denominations 1, 3, and 4 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

DP solution to the change-making problem Let F(n) be the minimum number of coins whose value add up to n add one coin of denomination dj to the amount n – dj Thus we have the following recurrence F(n) = min{F(n - dj)} + 1 for n > 0, F(0) = 0   A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

DP solution to the change-making problem F(n) = min{F(n - dj)} + 1 for n > 0, F(0) = 0   n 1 2 3 4 5 6 F( ) F(0) = 0 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

DP solution to the change-making problem F(n) = min{F(n - dj)} + 1 for n > 0, F(0) = 0   n 1 2 3 4 5 6 F( ) F(1) = min{F[1 - 1]} + 1 = 0 + 1 = 1 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

DP solution to the change-making problem F(n) = min{F(n - dj)} + 1 for n > 0, F(0) = 0   n 1 2 3 4 5 6 F( ) F(2) = min{F[2 - 1]} + 1 = 1 + 1 = 2 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

DP solution to the change-making problem F(n) = min{F(n - dj)} + 1 for n > 0, F(0) = 0   n 1 2 3 4 5 6 F( ) F(3) = min{F[3 - 1], F[3 - 3]} + 1 = min{2, 0} + 1 = 1 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

DP solution to the change-making problem F(n) = min{F(n - dj)} + 1 for n > 0, F(0) = 0   n 1 2 3 4 5 6 F( ) F(4) = min{F[4 - 1], F[4 - 3], F[4 - 4]} + 1 = min{1, 1, 0} + 1 = 1 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

DP solution to the change-making problem F(n) = min{F(n - dj)} + 1 for n > 0, F(0) = 0   n 1 2 3 4 5 6 F( ) F(5) = min{F[5 - 1], F[5 - 3], F[5 - 4]} + 1 = min{1, 2, 1} + 1 = 2 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

DP solution to the change-making problem F(n) = min{F(n - dj)} + 1 for n > 0, F(0) = 0   n 1 2 3 4 5 6 F( ) F(6) = min{F[6 - 1], F[6 - 3], F[6 - 4]} + 1 = min{2, 1, 2} + 1 = 2 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

DP solution to the change-making problem F(n) = min{F(n - dj)} + 1 for n > 0, F(0) = 0   n 1 2 3 4 5 6 F( ) Min coin #: 2 Coins of optimal solution: two 3’s Time efficiency: O(nm) Space efficiency: O(n) Note: All smaller instances were solved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

Example 4: Coin-collecting by robot Several coins are placed in cells of an n×m board. 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. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

Solution to the coin-collecting problem Let F(i,j) be the largest number of coins the robot can collect and bring to cell (i,j) in the ith row and jth column. The largest number of coins that can be brought to cell (i,j): from the left neighbor ? from the neighbor above? The recurrence: F(i, j) = max{F(i-1, j), F(i, j-1)} + cij for 1 ≤ i ≤ n, 1 ≤ j ≤ m where cij = 1 if there is a coin in cell (i,j), and cij = 0 otherwise F(0, j) = 0 for 1 ≤ j ≤ m and F(i, 0) = 0 for 1 ≤ i ≤ n. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

Solution to the coin-collecting problem (cont.) F(i, j) = max{F(i-1, j), F(i, j-1)} + cij for 1 ≤ i ≤ n, 1 ≤ j ≤ m where cij = 1 if there is a coin in cell (i,j), and cij = 0 otherwise F(0, j) = 0 for 1 ≤ j ≤ m and F(i, 0) = 0 for 1 ≤ i ≤ n. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

Solution to the coin-collecting problem (cont.) F(i, j) = max{F(i-1, j), F(i, j-1)} + cij for 1 ≤ i ≤ n, 1 ≤ j ≤ m where cij = 1 if there is a coin in cell (i,j), and cij = 0 otherwise F(0, j) = 0 for 1 ≤ j ≤ m and F(i, 0) = 0 for 1 ≤ i ≤ n. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

0-1 Knapsack problem Given a knapsack with maximum capacity W, and a set S consisting of n items Each item i has some weight wi and benefit value bi (all wi , bi and W are integer values) Problem: How to pack the knapsack to achieve maximum total value of packed items?

0-1 Knapsack problem wi bi Weight Benefit value Items 2 3 This is a knapsack Max weight: W = 20 4 5 5 8 W = 20 3 4 9 10

0-1 Knapsack problem Problem, in other words, is to find The problem is called a “0-1” problem, because each item must be entirely accepted or rejected. Just another version of this problem is the “Fractional Knapsack Problem”, where we can take fractions of items.

0-1 Knapsack problem: brute-force approach Let’s first solve this problem with a straightforward algorithm Since there are n items, there are 2n possible combinations of items. We go through all combinations and find the one with the most total value and with total weight less or equal to W Running time will be O(2n)

0-1 Knapsack problem: brute-force approach Can we do better? Yes, with an algorithm based on dynamic programming We need to carefully identify the sub-problems Let’s try this: If items are labeled 1..n, then a sub-problem would be to find an optimal solution for Sk = {items labeled 1, 2, .. k}, where k ≤ n

Defining a Subproblem If items are labeled 1..n, then a sub-problem would be to find an optimal solution for Sk = {items labeled 1, 2, .. k} This is a valid sub-problem definition. The question is: can we describe the final solution (Sn ) in terms of sub-problems (Sk)? Unfortunately, we can’t do that. Explanation follows….

? Defining a Subproblem wi bi Weight Benefit wi bi Item # 1 2 3 Max weight: W = 20 For S4: Total weight: 14; total benefit: 20 S4 2 4 5 Optimal Solution ? 3 5 8 4 3 4 5 9 10 w1 =2 b1 =3 w2=4 b2 =5 w3 =5 b3 =8 w5 =9 b5 =10 Solution for S4 is not part of the solution for the problem!!! Optimal Solution: Total weight: 20 total benefit: 26

Defining a Subproblem (continued) As we have seen, the solution for S4 is not part of the solution for the problem So our definition of a sub-problem is flawed and we need another one! Let’s add another parameter: w, which will represent the remaining weight of the knapsack The sub-problem then will be to compute B[k,w]: maximum value in a knapsack with k item and remaining weight w

Recursive Formula for subproblems It means, that the best subset of Sk that has total weight w is one of the two: 1) the best subset of Sk-1 that has total weight w, or 2) the best subset of Sk-1 that has total weight w-wk plus the value of item k

Recursive Formula The best subset of Sk that has the total weight w, either contains item k or not. First case: wk>w. Item k can’t be part of the solution, since if it was, the total weight would be > w, which is unacceptable Second case: wk <=w. Then the item k can be in the solution, and we choose the case with greater value

0-1 Knapsack Algorithm for w = 0 to W B[0,w] = 0 for i = 0 to n B[i,0] = 0 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w

Remember that the brute-force algorithm takes O(2n) Running time What is the running time of this algorithm? O(n*W) Remember that the brute-force algorithm takes O(2n)

Example (1) Let’s run our algorithm on the following data: n = 4 (# of elements) W = 5 (max weight) Elements (weight, benefit): (2,3), (3,4), (4,5), (5,6)

Example (2) i 1 2 3 4 W 1 2 3 4 5 for w = 0 to W B[0,w] = 0

Example (3) i 1 2 3 4 W 1 2 3 4 5 for i = 0 to n B[i,0] = 0

Example (4) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=1 bi=3 wi=2 1 2 3 4 W i=1 bi=3 wi=2 w=1 w-wi =-1 1 2 3 4 5 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w

Example (5) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=1 bi=3 wi=2 1 2 3 4 W i=1 bi=3 wi=2 w=2 w-wi =0 1 2 3 3 4 5 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w

Example (6) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=1 bi=3 wi=2 1 2 3 4 W i=1 bi=3 wi=2 w=3 w-wi=1 1 2 3 3 3 4 5 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w

Example (7) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=1 bi=3 wi=2 1 2 3 4 W i=1 bi=3 wi=2 w=4 w-wi=2 1 2 3 3 3 4 3 5 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w

Example (8) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=1 bi=3 wi=2 1 2 3 4 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) W i=1 bi=3 wi=2 w=5 w-wi=2 1 2 3 3 3 4 3 5 3 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w

Example (9) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=2 bi=4 wi=3 1 2 3 4 W i=2 bi=4 wi=3 w=1 w-wi=-2 1 2 3 3 3 4 3 5 3 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w

Example (10) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=2 bi=4 wi=3 1 2 3 4 W i=2 bi=4 wi=3 w=2 w-wi=-1 1 2 3 3 3 3 4 3 5 3 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w

Example (11) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=2 bi=4 wi=3 1 2 3 4 W i=2 bi=4 wi=3 w=3 w-wi=0 1 2 3 3 3 3 4 4 3 5 3 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w

Example (12) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=2 bi=4 wi=3 1 2 3 4 W i=2 bi=4 wi=3 w=4 w-wi=1 1 2 3 3 3 3 4 4 3 4 5 3 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w

Example (13) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=2 bi=4 wi=3 1 2 3 4 W i=2 bi=4 wi=3 w=5 w-wi=2 1 2 3 3 3 3 4 4 3 4 5 3 7 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w

Example (14) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=3 bi=5 wi=4 1 2 3 4 W i=3 bi=5 wi=4 w=1..3 1 2 3 3 3 3 3 4 4 4 3 4 5 3 7 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w

Example (15) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=3 bi=5 wi=4 1 2 3 4 W i=3 bi=5 wi=4 w=4 w- wi=0 1 2 3 3 3 3 3 4 4 4 3 4 5 5 3 7 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w

Example (15) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=3 bi=5 wi=4 1 2 3 4 W i=3 bi=5 wi=4 w=5 w- wi=1 1 2 3 3 3 3 3 4 4 4 3 4 5 5 3 7 7 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w

Example (16) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=3 bi=5 wi=4 1 2 3 4 W i=3 bi=5 wi=4 w=1..4 1 2 3 3 3 3 3 3 4 4 4 4 3 4 5 5 5 3 7 7 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w

Example (17) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=3 bi=5 wi=4 1 2 3 4 W i=3 bi=5 wi=4 w=5 1 2 3 3 3 3 3 3 4 4 4 4 3 4 5 5 The greedy approach is not giving us the optimal value. 5 3 7 7 7 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w

Comments This algorithm only finds the max possible value that can be carried in the knapsack To know the items that make this maximum value, an addition to this algorithm is necessary Please see LCS algorithm from the previous lecture for the example how to extract this data from the table we built

Find Items Item (reversed order): i = 2, i = 1 1 2 3 4 W 1 2 3 3 3 3 3 3 4 4 4 4 3 4 5 5 5 3 7 7 7 Item (reversed order): i = 2, i = 1 Item (straight order): i=1, i = 2

Optimal Binary Search Trees Example: What is an optimal BST for keys A, B, C, and D with search probabilities 0.1, 0.2, 0.4, and 0.3, respectively? Average number of comparison: a) 0.1 * 1 + 0.2 * 2 + 0.4 * 3 + 0.3 * 4 = 2.9 b) 0.1 * 2 + 0.2 * 1 + 0.4 * 2 + 0.3 * 3 = 2.1 c) 0.1 *3 + 0.2 * 2 + 0.4 * 1 + 0. 3 * 2 = 1.7 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

Optimal Binary Search Trees Problem: Given n keys a1 < …< an and probabilities p1 ≤ … ≤ pn searching for them, find a BST with a minimum average number of comparisons in successful search. Since total number of BSTs with n nodes is given by the nth Catalan number C(2n,n)/(n+1), which grows exponentially, brute force is hopeless. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

DP for Optimal BST Problem Let C[i,j] be minimum average number of comparisons made in T[i,j], optimal BST for keys ai < …< aj , where 1 ≤ i ≤ j ≤ n. Consider optimal BST among all BSTs with some ak (i ≤ k ≤ j ) as their root; T[i,j] is the best among them. C[i,j] = min {pk · 1 + ∑ ps (level as in T[i,k-1] +1) + ∑ ps (level as in T[k+1,j] +1)} i ≤ k ≤ j k-1 s = i j s =k+1 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

DP for Optimal BST Problem (cont.) After simplifications, we obtain the recurrence for C[i,j]: C[i,j] = min {C[i,k-1] + C[k+1,j]} + ∑ ps for 1 ≤ i ≤ j ≤ n C[i,i] = pi for 1 ≤ i ≤ j ≤ n s = i j i ≤ k ≤ j A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

Example: key A B C D probability 0.1 0.2 0.4 0.3 The tables below are filled diagonal by diagonal: the left one is filled using the recurrence C[i,j] = min {C[i,k-1] + C[k+1,j]} + ∑ ps , C[i,i] = pi ; The initial tables look like this: j i ≤ k ≤ j s = i

Example: key A B C D probability 0.1 0.2 0.4 0.3 j C[i,j] = min {C[i,k-1] + C[k+1,j]} + ∑ ps , C[i,i] = pi ; i ≤ k ≤ j s = i i j 1 2 3 4 .1 ? .2 .4 .3 5 i j 1 2 3 4 ? 5 optimal BST

Example: key A B C D probability 0.1 0.2 0.4 0.3 j C[i,j] = min {C[i,k-1] + C[k+1,j]} + ∑ ps , C[i,i] = pi ; i ≤ k ≤ j s = i i j 1 2 3 4 .1 .4 .2 ? .3 5 i j 1 2 3 4 ? 5 optimal BST

Example: key A B C D probability 0.1 0.2 0.4 0.3 j   i ≤ k ≤ j s = i i j 1 2 3 4 .1 .4 .2 .8 ? .3 5 i j 1 2 3 4 ? 5 optimal BST

Example: key A B C D probability 0.1 0.2 0.4 0.3 j   i ≤ k ≤ j s = i i j 1 2 3 4 .1 .4 ? .2 .8 1.0 .3 5 i j 1 2 3 4 ? 5 optimal BST

Example: key A B C D probability 0.1 0.2 0.4 0.3 j   i ≤ k ≤ j s = i i j 1 2 3 4 .1 .4 1.1 .2 .8 ? 1.0 .3 5 i j 1 2 3 4 ? 5 optimal BST

Example: key A B C D probability 0.1 0.2 0.4 0.3 j   i ≤ k ≤ j s = i i j 1 2 3 4 .1 .4 1.1 ? .2 .8 1.4 1.0 .3 5 i j 1 2 3 4 ? 5 optimal BST

Example: key A B C D probability 0.1 0.2 0.4 0.3 j   i ≤ k ≤ j s = i i j 1 2 3 4 .1 .4 1.1 1.7 .2 .8 1.4 1.0 .3 5 i j 1 2 3 4 5 optimal BST

Example: key A B C D probability 0.1 0.2 0.4 0.3 The tables below are filled diagonal by diagonal: the left one is filled using the recurrence C[i,j] = min {C[i,k-1] + C[k+1,j]} + ∑ ps , C[i,i] = pi ; main table root table j i ≤ k ≤ j s = i i j 1 2 3 4 .1 .4 1.1 1.7 .2 .8 1.4 1.0 .3 5 i j 1 2 3 4 5 B A C D optimal BST

DP for Optimal BST Problem (cont.) j C[i,j] = min {C[i,k-1] + C[k+1,j]} + ∑ ps for 1 ≤ i ≤ j ≤ n C[i,i] = pi for 1 ≤ i ≤ j ≤ n i ≤ k ≤ j s = i A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

Optimal Binary Search Trees A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

Analysis DP for Optimal BST Problem Time efficiency: Θ(n3) Space efficiency: Θ(n2) A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.