40
Design and Analysis of Algorithms Intro to dynamic programming; matrix chain multiplication; longest common subsequence Haidong Xue Summer 2012, at GSU

Haidong Xue Summer 2012, at GSU

  • Upload
    gore

  • View
    17

  • Download
    0

Embed Size (px)

DESCRIPTION

Design and Analysis of Algorithms Intro to dynamic programming; matrix chain multiplication; longest common subsequence. Haidong Xue Summer 2012, at GSU. When is divide-and-conquer inefficient?. In divide-and-conquer, a solution is combined from solutions of subproblems Mergesort - PowerPoint PPT Presentation

Citation preview

Page 1: Haidong Xue Summer 2012, at GSU

Design and Analysis of AlgorithmsIntro to dynamic programming; matrix chain

multiplication; longest common subsequence

Haidong XueSummer 2012, at GSU

Page 2: Haidong Xue Summer 2012, at GSU

When is divide-and-conquer inefficient?

β€’ In divide-and-conquer, a solution is combined from solutions of subproblems– Mergesort– Quicksort– Maximum-subarray

β€’ What if the subproblems are largely overlapped?

Page 3: Haidong Xue Summer 2012, at GSU

When is divide-and-conquer inefficient?

β€’ E.g. find out who is the tallest person in this classroom

β€’ It may be solved by: – Find out the tallest male– Find out the tallest student– Choose the taller one from these two

β€’ What is the problem here?– All the male students are measured twice

Page 4: Haidong Xue Summer 2012, at GSU

What is dynamic programming?

β€’ Similar to divide-and-conquer– (When applied to optimization problems, it is

called optimal substructure: optimal solution is constructed from optimal solutions to its subproblems)

β€’ Subproblems overlap– Subproblems share sub-subproblems– Overlapped subproblems grows quickly with

problem size

Page 5: Haidong Xue Summer 2012, at GSU

What is dynamic programming?

β€’ Subproblems overlap– Subproblems share sub-subproblems

Sub1 Sub2

Sub3 Sub4 Sub5 Sub4 Sub5 Sub6

Page 6: Haidong Xue Summer 2012, at GSU

What is dynamic programming?

β€’ Dynamic programming algorithms– Solve each subproblem only once– If solve problems in a top-down manner, record sub

problem solutions in a table (named: top-down with memoization)

– If solve the problems in a bottom-up manner, solve the smaller problem first (named: bottom-up method)

In computing, memoization is an optimization technique used primarily to speed up computer programs by having function calls avoid repeating the calculation of results for previously processed inputs.

Page 7: Haidong Xue Summer 2012, at GSU

What is dynamic programming?β€’ Find out who is the tallest person in this classroom– Find out the tallest male– Find out the tallest student– Find the taller one from this two

β€’ Assuming there are only these persons here:– Instructor, Haydon, male– Student, Brad, male– Student, Rebecca, female– Student, Eric, male

β€’ The problems β€œmeasure the height of a person” are highly overlapped

β€’ What are the top-down method and bottom-up method of this algorithm?

Page 8: Haidong Xue Summer 2012, at GSU

Top-downMemo

Name Height

Haydon

Brad

Rebecca

Eric

Find out the tallest male:Check memo for Haydon’s height

Measure Haydon’s height and record it. Max=(Haydon, 1.75)

1.75mCheck memo for Brad’s height

Measure Brad’s height and record it. Max=(Brad, 1.8) 1.80mCheck memo for Eric’s height

Measure Eric’s height and record it. Max=(Brad, 1.8) 1.78m

Return (Brad, 1.8)

Find out the tallest student:Check memo for Brad’s height. Max=(Brad, 1.8)

Check memo for Rebecca’s height.

Measure Rebecca’s height and record it. Max=(Brad, 1.8)

1.73m

Check memo for Eric’s height. Max=(Brad, 1.8)

Choose the taller one from tallest male and tallest student

Return (Brad, 1.8)

Return (Brad, 1.8)

How many times we measure the height?

4

Page 9: Haidong Xue Summer 2012, at GSU

Bottom-upMemo

Name Height

Haydon

Brad

Rebecca

Eric

Solve all the subproblems from smaller ones to bigger ones

Measure Haydon’s height and record it.

1.75m

Measure Brad’s height and record it.

1.80mMeasure Eric’s height and record it.

1.78mReturn (Brad, 1.8)

Find out the tallest student:

Measure Rebecca’s height and record it.

1.73m

Choose the taller one from tallest male and tallest studentReturn (Brad, 1.8)

Return (Brad, 1.8)How many times we measure the height?

4

Find out the tallest male:

Page 10: Haidong Xue Summer 2012, at GSU

Matrix chain multiplication

β€’ What is the number of real number multiplication needed for multiplying 2 matrices?

– pqrβ€’ For a matrix chain, do you think the order to

multiplication matters?– The order does not affect the result– But affects the cost (the number of real number

multiplications)

𝐴3( )𝐴2( 𝐴1

𝐴3( )𝐴2( )𝐴1

)

Page 11: Haidong Xue Summer 2012, at GSU

Matrix chain multiplication

β€’

– Cost =

– Cost =

β€’ With different parenthesizations, costs are different

Page 12: Haidong Xue Summer 2012, at GSU

Matrix chain multiplication

β€’ Matrix chain multiplication problem: given a matrix chain, find out a parenthesization with the lowest cost

β€’ How to solve it by brute force?𝐴1𝐴2 𝐴3 … 𝐴𝑛

Page 13: Haidong Xue Summer 2012, at GSU

Matrix chain multiplicationβ€’ How to solve it buy brute force?

𝐴3𝐴4( )𝐴2( )𝐴1( )

𝐴3 𝐴4( )𝐴2( )𝐴1( )

𝐴3𝐴4( )𝐴2( )𝐴1( )

𝐴3 𝐴4( )𝐴2( )𝐴1( )

𝐴3 𝐴4( )𝐴2( )𝐴1( )

How to divide and conquer?

Last multiplication is between A1 and A2

Last multiplication is between A2 and A3

Last multiplication is between A3 and A4

The number of alternative parenthesizations P(n)=Calculate the cost of a parenthesization needs

Page 14: Haidong Xue Summer 2012, at GSU

Matrix chain multiplicationβ€’ How to solve it buy divide-and-conquer?

𝐴1𝐴2 𝐴3𝐴4

𝐴1 𝐴2 𝐴3𝐴4optimal cost of + optimal cost of + cost of 𝐴1𝐴24

𝐴1 𝐴3𝐴4optimal cost of + optimal cost of + cost of 𝐴12𝐴34

𝐴1𝐴2 𝐴3 𝐴4optimal cost of + optimal cost of + cost of 𝐴13𝐴4

𝐴2

Choose the smallest one from them

Many subproblems are overlapped

Weakness?

Page 15: Haidong Xue Summer 2012, at GSU

Matrix chain multiplication𝐴1𝐴2𝐴3 … 𝐴𝑛

There are n-1 ways to divide it into 2 smaller matrix chains, if divide it after matrix k, for each of them we need to calculate:

𝐴1 π΄π‘˜+1… 𝐴𝑛optimal cost of + optimal cost of + cost of 𝐴1π‘˜π΄π‘˜+1 ,𝑛… π΄π‘˜

Choose the smallest one from them

Many subproblems are overlapped

Page 16: Haidong Xue Summer 2012, at GSU

Matrix chain multiplication

β€’ How do it in a dynamic programming way?– Top-down, record the solutions to subproblem– Or, bottom-up, start from smallest problems (the

typical manner)β€’ Solve all the possible subproblems

Let’s try it!

Page 17: Haidong Xue Summer 2012, at GSU

Matrix chain multiplication30*35 35*15 15*5 5*10 10*20 20*25

1 2 3 4 5 6

1

2

3

4

5

6

Table records the minimal cost of 𝐴𝑖 … 𝐴 𝑗

𝐴1

𝐴2

𝐴3

𝐴4

𝐴5

𝐴6

𝐴1𝐴2

𝐴2𝐴3

𝐴3 𝐴4

𝐴4 𝐴5

𝐴5𝐴6

𝐴1… 𝐴3 𝐴1… 𝐴4 𝐴1… 𝐴5 𝐴1… 𝐴6

𝐴2… 𝐴4 𝐴2… 𝐴5 𝐴2… 𝐴6

𝐴3… 𝐴5 𝐴3… 𝐴6

𝐴4… 𝐴6

Page 18: Haidong Xue Summer 2012, at GSU

Matrix chain multiplication30*35 35*15 15*5 5*10 10*20 20*25

1 2 3 4 5 6

1

2

3

4

5

6

Table records the minimal cost of 𝐴𝑖 … 𝐴 𝑗

𝐴1

𝐴2

𝐴3

𝐴4

𝐴5

𝐴6

𝐴1𝐴2

𝐴2𝐴3

𝐴3 𝐴4

𝐴4 𝐴5

𝐴5𝐴6

𝐴1… 𝐴3 𝐴1… 𝐴4 𝐴1… 𝐴5 𝐴1… 𝐴6

𝐴2… 𝐴4 𝐴2… 𝐴5 𝐴2… 𝐴6

𝐴3… 𝐴5 𝐴3… 𝐴6

𝐴4… 𝐴6

0

0

0

0

0

0

30*35*15=15750

35*15*5=2625

15*5*10=750

5*10*20=1000

10*20*25=5000

15750

2625

750

1000

5000

Page 19: Haidong Xue Summer 2012, at GSU

Matrix chain multiplication30*35 35*15 15*5 5*10 10*20 20*25

1 2 3 4 5 6

1

2

3

4

5

6

Table records the minimal cost of 𝐴𝑖 … 𝐴 𝑗

𝐴1… 𝐴3 𝐴1… 𝐴4 𝐴1… 𝐴5 𝐴1… 𝐴6

𝐴2… 𝐴4 𝐴2… 𝐴5 𝐴2… 𝐴6

𝐴3… 𝐴5 𝐴3… 𝐴6

𝐴4… 𝐴6

0

0

0

0

0

0

15750

2625

750

1000

5000

𝐴1 𝐴2𝐴3

0+2625+30*35*5=7875

𝐴1𝐴2 𝐴3

15750+0+30*15*5=30825

7875,1-2

Page 20: Haidong Xue Summer 2012, at GSU

Matrix chain multiplication30*35 35*15 15*5 5*10 10*20 20*25

1 2 3 4 5 6

1

2

3

4

5

6

Table records the minimal cost of 𝐴𝑖 … 𝐴 𝑗

𝐴1… 𝐴4 𝐴1… 𝐴5 𝐴1… 𝐴6

𝐴2… 𝐴4 𝐴2… 𝐴5 𝐴2… 𝐴6

𝐴3… 𝐴5 𝐴3… 𝐴6

𝐴4… 𝐴6

0

0

0

0

0

0

15750

2625

750

1000

5000

𝐴2 𝐴3𝐴4

0+750+35*15*10=6000

2625+0+35*5*10=4375

7875 ,1-2

𝐴2𝐴3 𝐴44375, 3-4

Page 21: Haidong Xue Summer 2012, at GSU

Matrix chain multiplication30*35 35*15 15*5 5*10 10*20 20*25

1 2 3 4 5 6

1

2

3

4

5

6

Table records the minimal cost of 𝐴𝑖 … 𝐴 𝑗

𝐴1… 𝐴4 𝐴1… 𝐴5 𝐴1… 𝐴6

𝐴2… 𝐴5 𝐴2… 𝐴6

𝐴3… 𝐴5 𝐴3… 𝐴6

𝐴4… 𝐴6

0

0

0

0

0

0

15750

2625

750

1000

5000

𝐴5𝐴3 𝐴4

0+1000+15*5*20=2500

750+0+15*10*20=3750

7875 ,1-2

4375, 3-4 𝐴5𝐴3𝐴42500, 3-4

Page 22: Haidong Xue Summer 2012, at GSU

Matrix chain multiplication30*35 35*15 15*5 5*10 10*20 20*25

1 2 3 4 5 6

1

2

3

4

5

6

Table records the minimal cost of 𝐴𝑖 … 𝐴 𝑗

𝐴1… 𝐴4 𝐴1… 𝐴5 𝐴1… 𝐴6

𝐴2… 𝐴5 𝐴2… 𝐴6

𝐴3… 𝐴6

𝐴4… 𝐴6

0

0

0

0

0

0

15750

2625

750

1000

5000

𝐴5𝐴6𝐴4

0+5000+5*10*25=6250

1000+0+5*20*25=3500

7875 ,1-2

4375, 3-4

2500, 3-4𝐴5 𝐴6𝐴4

3500, 5-6

Page 23: Haidong Xue Summer 2012, at GSU

Matrix chain multiplication30*35 35*15 15*5 5*10 10*20 20*25

1 2 3 4 5 6

1

2

3

4

5

6

Table records the minimal cost of 𝐴𝑖 … 𝐴 𝑗

𝐴1… 𝐴4 𝐴1… 𝐴5 𝐴1… 𝐴6

𝐴2… 𝐴5 𝐴2… 𝐴6

𝐴3… 𝐴6

0

0

0

0

0

0

15750

2625

750

1000

5000

7875 ,1-2

4375, 3-4

2500, 3-4

3500, 5-6

𝐴1 𝐴2𝐴3𝐴4

0+4375+30*35*10=14875

𝐴1𝐴2 𝐴3𝐴4

15750+750+30*15*10=21000

𝐴1𝐴2𝐴3 𝐴4

7875+0+30*5*10=9375

9375, 3-4

Page 24: Haidong Xue Summer 2012, at GSU

Matrix chain multiplication30*35 35*15 15*5 5*10 10*20 20*25

1 2 3 4 5 6

1

2

3

4

5

6

Table records the minimal cost of 𝐴𝑖 … 𝐴 𝑗

𝐴1… 𝐴5 𝐴1… 𝐴6

𝐴2… 𝐴5 𝐴2… 𝐴6

𝐴3… 𝐴6

0

0

0

0

0

0

15750

2625

750

1000

5000

7875 ,1-2

4375, 3-4

2500, 3-4

3500, 5-6

𝐴5𝐴2 𝐴3𝐴4

0+2500+35*15*20=13000

𝐴2𝐴3 𝐴4

2625+1000+35*5*20=7125

𝐴2𝐴3𝐴4

4375+0+35*10*20=11375

9375, 3-4

𝐴5

𝐴5

7125, 3-4

Page 25: Haidong Xue Summer 2012, at GSU

Matrix chain multiplication30*35 35*15 15*5 5*10 10*20 20*25

1 2 3 4 5 6

1

2

3

4

5

6

Table records the minimal cost of 𝐴𝑖 … 𝐴 𝑗

𝐴1… 𝐴5 𝐴1… 𝐴6

𝐴2… 𝐴6

𝐴3… 𝐴6

0

0

0

0

0

0

15750

2625

750

1000

5000

7875 ,1-2

4375, 3-4

2500, 3-4

3500, 5-6

𝐴5𝐴6𝐴3 𝐴4

0+3500+15*5*25=5375

750+5000+15*10*25=9500

2500+0+15*20*25=10000

9375, 3-4

7125, 3-4 𝐴5𝐴6𝐴3𝐴4

𝐴5 𝐴6𝐴3𝐴4

5375, 3-4

Page 26: Haidong Xue Summer 2012, at GSU

Matrix chain multiplication30*35 35*15 15*5 5*10 10*20 20*25

1 2 3 4 5 6

1

2

3

4

5

6

Table records the minimal cost of 𝐴𝑖 … 𝐴 𝑗

𝐴1… 𝐴5 𝐴1… 𝐴6

𝐴2… 𝐴6

0

0

0

0

0

0

15750

2625

750

1000

5000

7875 ,1-2

4375, 3-4

2500, 3-4

3500, 5-6

𝐴5𝐴2𝐴3𝐴40+7125+30*35*20=281259375, 3-4

7125, 3-4

5375, 3-4

𝐴1

𝐴5𝐴2 𝐴3𝐴4𝐴115750+2500+30*15*20=27250

𝐴5𝐴2𝐴3 𝐴4𝐴17875+1000+30*5*20=11875

𝐴5𝐴2𝐴3𝐴4𝐴19375+0+30*20*25=24375

11875, 3-4

Page 27: Haidong Xue Summer 2012, at GSU

Matrix chain multiplication30*35 35*15 15*5 5*10 10*20 20*25

1 2 3 4 5 6

1

2

3

4

5

6

Table records the minimal cost of 𝐴𝑖 … 𝐴 𝑗

𝐴1… 𝐴6

𝐴2… 𝐴6

0

0

0

0

0

0

15750

2625

750

1000

5000

7875 ,1-2

4375, 3-4

2500, 3-4

3500, 5-6

𝐴5𝐴2 𝐴3𝐴40+5375+35*15*25=185009375, 3-4

7125, 3-4

5375, 3-4

𝐴6

11875, 3-4

𝐴5𝐴2𝐴3 𝐴42625+3500+35*5*25=10500

𝐴6

𝐴5𝐴2𝐴3𝐴44375+5000+35*10*25=18125

𝐴6

𝐴5𝐴2𝐴3𝐴47125+0+35*20*25=24625

𝐴6

18125, 3-4

Page 28: Haidong Xue Summer 2012, at GSU

Matrix chain multiplication30*35 35*15 15*5 5*10 10*20 20*25

1 2 3 4 5 6

1

2

3

4

5

6

Table records the minimal cost of 𝐴𝑖 … 𝐴 𝑗

𝐴1… 𝐴60

0

0

0

0

0

15750

2625

750

1000

5000

7875 ,1-2

4375, 3-4

2500, 3-4

3500, 5-6

𝐴5𝐴2𝐴3𝐴40+18125+30*35*25=44375

9375, 3-4

7125, 3-4

5375, 3-4

𝐴6

11875, 3-4

18125, 3-4

𝐴1

𝐴5𝐴2 𝐴3𝐴415750+5375+30*15*25=32375

𝐴6𝐴1

𝐴5𝐴2𝐴3 𝐴47875+3500+30*5*25=15125

𝐴6𝐴1

𝐴5𝐴2𝐴3𝐴49375+5000+30*10*25=21875

𝐴6𝐴1

𝐴5𝐴2𝐴3𝐴411875+0+30*20*25=26875

𝐴6𝐴1

15125, 3-4

Page 29: Haidong Xue Summer 2012, at GSU

Matrix chain multiplication

1 2 3 4 5 6

1

2

3

4

5

6

𝐴5𝐴2 𝐴3 𝐴4 𝐴6𝐴1

0

0

0

0

0

0

15750

2625

750

1000

5000

7875, 1-2

4375, 3-4

2500, 3-4

3500, 5-6

9375, 3-4

7125, 3-4

5375, 3-4

11875, 3-4

18125, 3-4

15125, 3-4

( ) ( )( ) ( )

Time complexity? Number of subproblems =

When all sub solutions are known, time needed for a solution=O(n)

So T(n) =

Page 30: Haidong Xue Summer 2012, at GSU

Matrix chain multiplication3*2 2*5 5*10 10*6

1 2 3 4

1 0 30 160, 1-2 256, 1-2

2 0 100 220, 3-4

3 0 300

4 0

5 minutes, try it now …

Page 31: Haidong Xue Summer 2012, at GSU

How to design a dynamic programming algorithms?

β€’ Precondition: subproblems are largely overlapped

β€’ Define the optimal solution by optimal subsolutions

β€’ Compute the value of an optimal solutionβ€’ Construct an optimal solution from computed

information

Page 32: Haidong Xue Summer 2012, at GSU

Longest common subsequenceZ is a subsequence of A:β€’ All elements in Z are also in Aβ€’ The order of elements in Z is the same as the

corresponding ones in A

E.g. Given A: 𝐡D 𝐢𝐴 𝐴B

Subsequences: 𝐢𝐴B

D 𝐢𝐡𝐴…

Page 33: Haidong Xue Summer 2012, at GSU

Longest common subsequenceC is a common subsequence of A and B:β€’ C is a subsequence of A and C is a subsequence of B

E.g. Given A: 𝐡D 𝐢𝐴 𝐴B

Common subsequences: 𝐢𝐴B

𝐡𝐴

…

B: 𝐷B 𝐢𝐡 𝐴A 𝐡

𝐢𝐡B 𝐴

Page 34: Haidong Xue Summer 2012, at GSU

Longest common subsequence

β€’ Longest common subsequence (LCS): longest common subsequencesE.g. Given A: 𝐡D 𝐢𝐴 𝐴B

LCS:

B: 𝐷B 𝐢𝐡 𝐴A 𝐡

𝐢𝐡B 𝐴

Page 35: Haidong Xue Summer 2012, at GSU

Longest common subsequence

β€’ LCS problem: given 2 sequences (e.g. A and B), find a longest common subsequence

β€’ How to design this algorithm?– Brute-force– Divide-and-conquer– Dynamic-programming

Page 36: Haidong Xue Summer 2012, at GSU

Longest common subsequence

β€’ Brute-force– Find all the common sequencesβ€’ Find all the subsequence of Aβ€’ Test each of them if it also a subsequence of B

– MAXIMUM(all common subsequences)

Page 37: Haidong Xue Summer 2012, at GSU

Longest common subsequence

β€’ Divide-and-conquer

if(m==0 or n==0 ) return β€œβ€;if(= )

return LCS( , ) + ;return max( LCS( , ), LCS( , ) );𝐴1…

π΄π‘š

𝐡1… 𝐡𝑛

π΄π‘š

𝐴1…

𝐡1… π΅π‘›βˆ’ 1

π΄π‘šπ΄1… π΄π‘šβˆ’1 𝐡1… π΅π‘›βˆ’ 1

𝐴1… π΄π‘šβˆ’1 𝐡1… 𝐡𝑛

Page 38: Haidong Xue Summer 2012, at GSU

Longest common subsequence

β€’ Dynamic programming– Calculate LCS for ,– from i=0 to m, j=0 to n– the length of LCS is indicated by c[i,j]– Algorithmβ€’ if(i==0 or j==0) c[i, j]=0;β€’ else if ( == ) c[i,j]=c[i-1, j-1]+1;β€’ else c[i,j]=max(c[i-1, j], c[i, j-1])

𝐴𝑖 𝐡1… 𝐡 𝑗𝐴1…

π΄π‘š

𝐡1… 𝐡𝑛

𝐴1…

𝐴𝑖 𝐡 𝑗

Page 39: Haidong Xue Summer 2012, at GSU

Longest common subsequence

i 0 1 2 3 4

j B D C A

0

1 A

2 B

3 C

4 B

0

0

0

0

0

0 0 0 0

0 0 0 1

1 1 1 1

1 1 2 2

1 1 2 2

Compute the value of a optimal solution:

Page 40: Haidong Xue Summer 2012, at GSU

Longest common subsequence

i 0 1 2 3 4

j B D C A

0

1 A

2 B

3 C

4 B

0

0

0

0

0

0 0 0 0

0 0 0 1

1 1 1 1

1 1 2 2

1 1 2 2

Construct the optimal solution: