Upload
gore
View
17
Download
0
Tags:
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
Design and Analysis of AlgorithmsIntro to dynamic programming; matrix chain
multiplication; longest common subsequence
Haidong XueSummer 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?
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
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
What is dynamic programming?
β’ Subproblems overlapβ Subproblems share sub-subproblems
Sub1 Sub2
Sub3 Sub4 Sub5 Sub4 Sub5 Sub6
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.
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?
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
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:
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
)
Matrix chain multiplication
β’
β Cost =
β Cost =
β’ With different parenthesizations, costs are different
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 β¦ π΄π
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
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?
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
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!
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
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
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
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
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
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
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
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
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
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
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
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
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) =
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 β¦
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
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 πΆπ΅π΄β¦
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 π΄
Longest common subsequence
β’ Longest common subsequence (LCS): longest common subsequencesE.g. Given A: π΅D πΆπ΄ π΄B
LCS:
B: π·B πΆπ΅ π΄A π΅
πΆπ΅B π΄
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
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)
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β¦ π΅π
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β¦
π΄π π΅ π
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:
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: