CSC401: Analysis of Algorithms 5-3-1 CSC401 – Analysis of Algorithms Chapter 5 -- 3 Dynamic...

20
CSC401: Analysis of Algorithms CSC401: Analysis of Algorithms 5-3- 5-3-1 CSC401 – Analysis of Algorithms CSC401 – Analysis of Algorithms Chapter 5 -- 3 Chapter 5 -- 3 Dynamic Programming Dynamic Programming Objectives: Objectives: Present the Dynamic Programming Present the Dynamic Programming paradigm paradigm Review the Matrix Chain-Product Review the Matrix Chain-Product problem problem Discuss the General Technique Discuss the General Technique Solve the 0-1 Knapsack Problem Solve the 0-1 Knapsack Problem Introduce pseudo-polynomial-time Introduce pseudo-polynomial-time algorithms algorithms

Transcript of CSC401: Analysis of Algorithms 5-3-1 CSC401 – Analysis of Algorithms Chapter 5 -- 3 Dynamic...

Page 1: CSC401: Analysis of Algorithms 5-3-1 CSC401 – Analysis of Algorithms Chapter 5 -- 3 Dynamic Programming Objectives: Present the Dynamic Programming paradigm.

CSC401: Analysis of AlgorithmsCSC401: Analysis of Algorithms 5-3-5-3-11

CSC401 – Analysis of Algorithms CSC401 – Analysis of Algorithms Chapter 5 -- 3Chapter 5 -- 3

Dynamic ProgrammingDynamic Programming

Objectives:Objectives: Present the Dynamic Programming Present the Dynamic Programming

paradigmparadigm Review the Matrix Chain-Product problemReview the Matrix Chain-Product problem Discuss the General TechniqueDiscuss the General Technique Solve the 0-1 Knapsack ProblemSolve the 0-1 Knapsack Problem Introduce pseudo-polynomial-time Introduce pseudo-polynomial-time

algorithmsalgorithms

Page 2: CSC401: Analysis of Algorithms 5-3-1 CSC401 – Analysis of Algorithms Chapter 5 -- 3 Dynamic Programming Objectives: Present the Dynamic Programming paradigm.

CSC401: Analysis of AlgorithmsCSC401: Analysis of Algorithms<footer><footer> 5-3-5-3-22

Dynamic ProgrammingDynamic Programming

Dynamic Programming is a general algorithm design techniqueInvented by American mathematician Richard Bellman in the 1950s to solve optimization problems“Programming” here means “planning”Main idea:

solve several smaller (overlapping) subproblemssolve several smaller (overlapping) subproblems

record solutions in a table so that each subproblem is record solutions in a table so that each subproblem is only solved onceonly solved once

final state of the table will be (or contain) solutionfinal state of the table will be (or contain) solution

Page 3: CSC401: Analysis of Algorithms 5-3-1 CSC401 – Analysis of Algorithms Chapter 5 -- 3 Dynamic Programming Objectives: Present the Dynamic Programming paradigm.

CSC401: Analysis of AlgorithmsCSC401: Analysis of Algorithms<footer><footer> 5-3-5-3-33

Example: Fibonacci numbersExample: Fibonacci numbers

• Recall definition of Fibonacci numbers:f(0) = 0f(1) = 1f(n) = f(n-1) + f(n-2)

• Computing the nth Fibonacci number recursively (top-down): f(n)

f(n-1) + f(n-2)

f(n-2) + f(n-3) f(n-3) + f(n-4) ......

• Time efficiency: T(n) = T(n-1)+T(n-2)+1 O(2n)

Page 4: CSC401: Analysis of Algorithms 5-3-1 CSC401 – Analysis of Algorithms Chapter 5 -- 3 Dynamic Programming Objectives: Present the Dynamic Programming paradigm.

CSC401: Analysis of AlgorithmsCSC401: Analysis of Algorithms<footer><footer> 5-3-5-3-44

Example: Fibonacci numbersExample: Fibonacci numbers

• Computing the nth fibonacci number using bottom-up iteration:• f(0) = 0• f(1) = 1• f(2) = 0+1 = 1• f(3) = 1+1 = 2• f(4) = 1+2 = 3• f(5) = 2+3 = 5•

• f(n-2) = • f(n-1) = • f(n) = f(n-1) + f(n-2)

• Time efficiency: O(n)

Page 5: CSC401: Analysis of Algorithms 5-3-1 CSC401 – Analysis of Algorithms Chapter 5 -- 3 Dynamic Programming Objectives: Present the Dynamic Programming paradigm.

CSC401: Analysis of AlgorithmsCSC401: Analysis of Algorithms<footer><footer> 5-3-5-3-55

Examples of Dynamic Programming AlgorithmsExamples of Dynamic Programming Algorithms

• Computing binomial coefficients

• Optimal chain matrix multiplication

• Constructing an optimal binary search tree

• Warshall’s algorithm for transitive closure

• Floyd’s algorithms for all-pairs shortest paths

• Some instances of difficult discrete optimization problems:• travelling salesman• knapsack

Page 6: CSC401: Analysis of Algorithms 5-3-1 CSC401 – Analysis of Algorithms Chapter 5 -- 3 Dynamic Programming Objectives: Present the Dynamic Programming paradigm.

CSC401: Analysis of AlgorithmsCSC401: Analysis of Algorithms<footer><footer> 5-3-5-3-66

Optimal chain matrix Optimal chain matrix multiplicationmultiplication

A C

B

d d

f

e

f

e

i

j

i,j

1

0

],[],[],[e

k

jkBkiAjiC

Review: Matrix Multiplication.Review: Matrix Multiplication.– CC = = AA××BB– AA isis d d × × ee andand BB isis e e ×× f f

– OO((defdef )) timetime

Page 7: CSC401: Analysis of Algorithms 5-3-1 CSC401 – Analysis of Algorithms Chapter 5 -- 3 Dynamic Programming Objectives: Present the Dynamic Programming paradigm.

CSC401: Analysis of AlgorithmsCSC401: Analysis of Algorithms<footer><footer> 5-3-5-3-77

Matrix Chain-ProductsMatrix Chain-ProductsMatrix Chain-Product:Matrix Chain-Product:– Compute A=ACompute A=A00*A*A11*…*A*…*An-1n-1

– AAii is d is di i × d× di+1i+1

– Problem: How to parenthesize?Problem: How to parenthesize?

ExampleExample– B is 3 × 100B is 3 × 100– C is 100 × 5C is 100 × 5– D is 5 × 5D is 5 × 5– (B*C)*D takes 1500 + 75 = 1575 (B*C)*D takes 1500 + 75 = 1575

operationsoperations– B*(C*D) takes 1500 + 2500 = 4000 B*(C*D) takes 1500 + 2500 = 4000

operationsoperations

Page 8: CSC401: Analysis of Algorithms 5-3-1 CSC401 – Analysis of Algorithms Chapter 5 -- 3 Dynamic Programming Objectives: Present the Dynamic Programming paradigm.

CSC401: Analysis of AlgorithmsCSC401: Analysis of Algorithms<footer><footer> 5-3-5-3-88

A Brute-force ApproachA Brute-force ApproachMatrix Chain-Product Algorithm:Matrix Chain-Product Algorithm:– Try all possible ways to parenthesize Try all possible ways to parenthesize

A=AA=A00*A*A11*…*A*…*An-1n-1

– Calculate number of operations for each oneCalculate number of operations for each one– Pick the best onePick the best one

Running time:Running time:– The number of paranethesizations is equal The number of paranethesizations is equal

to the number of binary trees with n nodesto the number of binary trees with n nodes– This is This is exponentialexponential!!– It is called the Catalan number, and it is It is called the Catalan number, and it is

almost 4almost 4nn..– This is a terrible algorithm!This is a terrible algorithm!

Page 9: CSC401: Analysis of Algorithms 5-3-1 CSC401 – Analysis of Algorithms Chapter 5 -- 3 Dynamic Programming Objectives: Present the Dynamic Programming paradigm.

CSC401: Analysis of AlgorithmsCSC401: Analysis of Algorithms<footer><footer> 5-3-5-3-99

Greedy ApproachesGreedy ApproachesIdea #1Idea #1: repeatedly : repeatedly select the product select the product that uses (up) the that uses (up) the most operations.most operations.Counter-exampleCounter-example: : – A is 10 × 5A is 10 × 5– B is 5 × 10B is 5 × 10– C is 10 × 5C is 10 × 5– D is 5 × 10D is 5 × 10– Greedy idea #1 gives Greedy idea #1 gives

(A*B)*(C*D), which (A*B)*(C*D), which takes 500+1000+500 takes 500+1000+500 = 2000 operations= 2000 operations

– A*((B*C)*D) takes A*((B*C)*D) takes 500+250+250 = 500+250+250 = 1000 operations1000 operations

Idea #2Idea #2: repeatedly select : repeatedly select the product that uses the the product that uses the fewest operations.fewest operations.Counter-exampleCounter-example: : – A is 101 × 11A is 101 × 11– B is 11 × 9B is 11 × 9– C is 9 × 100C is 9 × 100– D is 100 × 99D is 100 × 99– Greedy idea #2 gives Greedy idea #2 gives

A*((B*C)*D)), which takes A*((B*C)*D)), which takes 109989+9900+108900=228109989+9900+108900=228789 operations789 operations

– (A*B)*(C*D) takes (A*B)*(C*D) takes 9999+89991+89100=189099999+89991+89100=189090 operations0 operations

The greedy approach is not The greedy approach is not giving us the optimal giving us the optimal value.value.

Page 10: CSC401: Analysis of Algorithms 5-3-1 CSC401 – Analysis of Algorithms Chapter 5 -- 3 Dynamic Programming Objectives: Present the Dynamic Programming paradigm.

CSC401: Analysis of AlgorithmsCSC401: Analysis of Algorithms<footer><footer> 5-3-5-3-1010

A “Recursive” ApproachA “Recursive” ApproachDefine Define subproblemssubproblems::– Find the best parenthesization of AFind the best parenthesization of A ii*A*Ai+1i+1*…*A*…*Ajj..– Let NLet Ni,ji,j denote the number of operations done by this denote the number of operations done by this

subproblem.subproblem.– The optimal solution for the whole problem is NThe optimal solution for the whole problem is N0,n-10,n-1..

Subproblem optimalitySubproblem optimality: The optimal solution can : The optimal solution can be defined in terms of optimal subproblemsbe defined in terms of optimal subproblems– There has to be a final multiplication (root of the There has to be a final multiplication (root of the

expression tree) for the optimal solution. expression tree) for the optimal solution. – Say, the final multiply is at index i: (ASay, the final multiply is at index i: (A00*…*A*…*Aii)*(A)*(Ai+1i+1*…*A*…*An-1n-1).).– Then the optimal solution NThen the optimal solution N0,n-10,n-1 is the sum of two optimal is the sum of two optimal

subproblems, Nsubproblems, N0,i0,i and N and Ni+1,n-1 i+1,n-1 plus the time for the last plus the time for the last multiply.multiply.

– If the global optimum did not have these optimal If the global optimum did not have these optimal subproblems, we could define an even better “optimal” subproblems, we could define an even better “optimal” solution.solution.

Page 11: CSC401: Analysis of Algorithms 5-3-1 CSC401 – Analysis of Algorithms Chapter 5 -- 3 Dynamic Programming Objectives: Present the Dynamic Programming paradigm.

CSC401: Analysis of AlgorithmsCSC401: Analysis of Algorithms<footer><footer> 5-3-5-3-1111

A Characterizing EquationA Characterizing EquationThe global optimal has to be defined in terms of The global optimal has to be defined in terms of optimal subproblems, depending on where the optimal subproblems, depending on where the final multiply is at.final multiply is at.Let us consider all possible places for that final Let us consider all possible places for that final multiply:multiply:– Recall that ARecall that Aii is a d is a dii × d × di+1i+1 dimensional matrix. dimensional matrix.– So, a characterizing equation for NSo, a characterizing equation for Ni,ji,j is the following: is the following:

Note that subproblems are not independent--the Note that subproblems are not independent--the subproblems overlapsubproblems overlap..

}{min 11,1,, jkijkki

jkiji dddNNN

Page 12: CSC401: Analysis of Algorithms 5-3-1 CSC401 – Analysis of Algorithms Chapter 5 -- 3 Dynamic Programming Objectives: Present the Dynamic Programming paradigm.

CSC401: Analysis of AlgorithmsCSC401: Analysis of Algorithms<footer><footer> 5-3-5-3-1212

A Dynamic Programming AlgorithmA Dynamic Programming AlgorithmSince Since subproblems subproblems overlapoverlap, we don’t , we don’t use recursion.use recursion.

Instead, we Instead, we construct optimal construct optimal subproblems subproblems “bottom-up.” “bottom-up.”

NNi,ii,i’s are easy, so ’s are easy, so start with themstart with them

Then do length 2,3,Then do length 2,3,… subproblems, … subproblems, and so on.and so on.

Running time: Running time: O(nO(n33))

Algorithm matrixChain(S):Input: sequence S of n matrices to be multipliedOutput: number of operations in an optimal

paranethization of Sfor i 1 to n-1 do

Ni,i 0 for b 1 to n-1 do

for i 0 to n-b-1 doj i+b

Ni,j +infinityfor k i to j-1 do

Ni,j min{Ni,j , Ni,k +Nk+1,j +di dk+1

dj+1}

Page 13: CSC401: Analysis of Algorithms 5-3-1 CSC401 – Analysis of Algorithms Chapter 5 -- 3 Dynamic Programming Objectives: Present the Dynamic Programming paradigm.

CSC401: Analysis of AlgorithmsCSC401: Analysis of Algorithms<footer><footer> 5-3-5-3-1313

A Dynamic Programming A Dynamic Programming AlgorithmAlgorithm

The bottom-up The bottom-up construction fills in the construction fills in the N array by diagonalsN array by diagonals

NNi,ji,j gets values from gets values from pervious entries in i-th pervious entries in i-th row and j-th column row and j-th column

Filling in each entry in Filling in each entry in the N table takes O(n) the N table takes O(n) time.time.

Total run time: O(nTotal run time: O(n33))

Getting actual Getting actual parenthesization can parenthesization can be done by be done by remembering “k” for remembering “k” for each N entryeach N entry

answer

N 0 1

0

1

2 …

n-1

n-1j

i

}{min 11,1,, jkijkki

jkiji dddNNN

Page 14: CSC401: Analysis of Algorithms 5-3-1 CSC401 – Analysis of Algorithms Chapter 5 -- 3 Dynamic Programming Objectives: Present the Dynamic Programming paradigm.

CSC401: Analysis of AlgorithmsCSC401: Analysis of Algorithms<footer><footer> 5-3-5-3-1414

The General Dynamic The General Dynamic Programming TechniqueProgramming Technique

Applies to a problem that at first seems to Applies to a problem that at first seems to require a lot of time (possibly require a lot of time (possibly exponential), provided we have:exponential), provided we have:– Simple subproblems:Simple subproblems: the subproblems can the subproblems can

be defined in terms of a few variables, such as be defined in terms of a few variables, such as j, k, l, m, and so on.j, k, l, m, and so on.

– Subproblem optimality:Subproblem optimality: the global the global optimum value can be defined in terms of optimum value can be defined in terms of optimal subproblemsoptimal subproblems

– Subproblem overlap:Subproblem overlap: the subproblems are the subproblems are not independent, but instead they overlap not independent, but instead they overlap (hence, should be constructed bottom-up).(hence, should be constructed bottom-up).

Page 15: CSC401: Analysis of Algorithms 5-3-1 CSC401 – Analysis of Algorithms Chapter 5 -- 3 Dynamic Programming Objectives: Present the Dynamic Programming paradigm.

CSC401: Analysis of AlgorithmsCSC401: Analysis of Algorithms<footer><footer> 5-3-5-3-1515

The 0/1 Knapsack ProblemThe 0/1 Knapsack ProblemGiven: A set S of n items, with each item i havingGiven: A set S of n items, with each item i having– bbii - a positive benefit - a positive benefit– wwii - a positive weight - a positive weight

Goal: Choose items with maximum total benefit Goal: Choose items with maximum total benefit but with weight at most W.but with weight at most W.If we are If we are notnot allowed to take fractional amounts, allowed to take fractional amounts, then this is the then this is the 0/1 knapsack problem0/1 knapsack problem..– In this case, we let TIn this case, we let T denote the set of items we takedenote the set of items we take

– Objective: maximizeObjective: maximize

– Constraint:Constraint:

Ti

ib

Ti

i Ww

Page 16: CSC401: Analysis of Algorithms 5-3-1 CSC401 – Analysis of Algorithms Chapter 5 -- 3 Dynamic Programming Objectives: Present the Dynamic Programming paradigm.

CSC401: Analysis of AlgorithmsCSC401: Analysis of Algorithms<footer><footer> 5-3-5-3-1616

Given: A set S of n items, with each item i havingGiven: A set S of n items, with each item i having– bbii - a positive benefit - a positive benefit– wwii - a positive weight - a positive weight

Goal: Choose items with maximum total benefit Goal: Choose items with maximum total benefit but with weight at most W.but with weight at most W.

ExampleExample

Weight:Benefit:

1 2 3 4 5

4 in 2 in 2 in 6 in 2 in

$20 $3 $6 $25 $80

Items:

9 in

Solution:• 5 (2 in)• 3 (2 in)• 1 (4 in)

“knapsack”

Page 17: CSC401: Analysis of Algorithms 5-3-1 CSC401 – Analysis of Algorithms Chapter 5 -- 3 Dynamic Programming Objectives: Present the Dynamic Programming paradigm.

CSC401: Analysis of AlgorithmsCSC401: Analysis of Algorithms<footer><footer> 5-3-5-3-1717

A 0/1 Knapsack Algorithm, A 0/1 Knapsack Algorithm, First AttemptFirst Attempt

SSkk: Set of items numbered 1 to k.: Set of items numbered 1 to k.

Define B[k] = best selection from SDefine B[k] = best selection from Skk..Problem: does not have subproblem optimality:Problem: does not have subproblem optimality:– Consider S={(3,2),(5,4),(8,5),(4,3),10,9)} weight-benefit Consider S={(3,2),(5,4),(8,5),(4,3),10,9)} weight-benefit

pairspairs

Best for S4:

Best for S5:

Page 18: CSC401: Analysis of Algorithms 5-3-1 CSC401 – Analysis of Algorithms Chapter 5 -- 3 Dynamic Programming Objectives: Present the Dynamic Programming paradigm.

CSC401: Analysis of AlgorithmsCSC401: Analysis of Algorithms<footer><footer> 5-3-5-3-1818

A 0/1 Knapsack Algorithm, A 0/1 Knapsack Algorithm, Second AttemptSecond Attempt

SSkk: Set of items numbered 1 to k.: Set of items numbered 1 to k.

Define B[k,w] = best selection from SDefine B[k,w] = best selection from Skk with with weight exactly equal to wweight exactly equal to wGood news: this does have subproblem Good news: this does have subproblem optimality:optimality:

I.e., best subset of SI.e., best subset of Skk with weight exactly w with weight exactly w is either the best subset of Sis either the best subset of Sk-1k-1 w/ weight w w/ weight w or the best subset of Sor the best subset of Sk-1k-1 w/ weight w-w w/ weight w-wkk plus item k.plus item k.

else}],1[],,1[max{

if],1[],[

kk

k

bwwkBwkB

wwwkBwkB

Page 19: CSC401: Analysis of Algorithms 5-3-1 CSC401 – Analysis of Algorithms Chapter 5 -- 3 Dynamic Programming Objectives: Present the Dynamic Programming paradigm.

CSC401: Analysis of AlgorithmsCSC401: Analysis of Algorithms<footer><footer> 5-3-5-3-1919

The 0/1 Knapsack AlgorithmThe 0/1 Knapsack AlgorithmRecall definition of Recall definition of B[k,w]:B[k,w]:

Since B[k,w] is Since B[k,w] is defined in terms of defined in terms of B[k-1,*], we can reuse B[k-1,*], we can reuse the same arraythe same arrayRunning time: O(nW).Running time: O(nW).Not a polynomial-time Not a polynomial-time algorithm if W is largealgorithm if W is largeThis is a This is a pseudo-pseudo-polynomialpolynomial time time algorithmalgorithm

Algorithm 01Knapsack(S, W):

Input: set S of items w/ benefit bi and weight wi; max. weight W

Output: benefit of best subset with weight at most W

for w 0 to W doB[w] 0

for k 1 to n do

for w W downto wk do

if B[w-wk]+bk > B[w] then

B[w] B[w-wk]+bk

else}],1[],,1[max{

if],1[],[

kk

k

bwwkBwkB

wwwkBwkB

Page 20: CSC401: Analysis of Algorithms 5-3-1 CSC401 – Analysis of Algorithms Chapter 5 -- 3 Dynamic Programming Objectives: Present the Dynamic Programming paradigm.

CSC401: Analysis of AlgorithmsCSC401: Analysis of Algorithms<footer><footer> 5-3-5-3-2020

Pseudo-Polynomial Time AlgorithmsPseudo-Polynomial Time Algorithms

An algorithm is a pseudo-polynomial time An algorithm is a pseudo-polynomial time algorithm, if the running time of the algorithm, if the running time of the algorithm depends on the magnitude of a algorithm depends on the magnitude of a number given in the input, not its number given in the input, not its encoding sizeencoding size

Example: The 0/1 Knapsack algorithm Example: The 0/1 Knapsack algorithm depends on Wdepends on W

Not a true polynomial-time algorithmNot a true polynomial-time algorithm

But usually it’s better than the brute-force But usually it’s better than the brute-force algorithmalgorithm