




















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





















































I (^) Given a sequence of integers, A = a 1 , a 2 ,... , aN I (^) Determine a longest, increasing, subsequence of A
Example: 5 2 8 6 3 6 9 7
Increasing subsequences include: 8 8 9 2 6 9 2 3 6 9
We want a longest increasing subsequence
I (^) Success hinges on identifying subproblems
I (^) Tabulate all solutions to the subproblems — typically bottom-up: from smallest to largest
I (^) Identify one or more rules which solve a problem given solutions to subproblems
prefix α[·] L[·]
5 α[1] = 5 L[1] = 1 5 2 α[2] = 2 L[2] = 1 5 2 8 α[3] = 5 8 L[3] = 2 5 2 8 6 α[4] = 2 6 L[4] = 2 5 2 8 6 a 5 α[5] =? L[5] =?
I (^) α[5] ends with a 5 I (^) α[5] potentially extends one of the subsequences α[1], α[2], α[3], or α[4] I (^) α[5] can extend α[j] provided a 5 > aj I (^) choose the largest extension possible for α[5]
The problem I (^) Given prefix a 1 a 2... ak− 1 I (^) α[1], α[2],... , α[k − 1] are all known I (^) L[1], L[2],... , L[k − 1] are all known I (^) Consider the prefix a 1 a 2... ak− 1 ak I (^) We want to know α[k] and L[k]
A plan of attack I (^) C = { 1 ≤ j ≤ k − 1 : ak > aj } I (^) C = {} ⇒ α[k] = ak C 6 = {} ⇒ α[k] = longest from {α[j] ⊕ ak : j ∈ C } I (^) L[k] = length(α[k])
I (^) Running time: O(N^2 )
I (^) Storage requirements can be reduced — in place of complete path information in α[k], a single predecessor could be stored I (^) Can be viewed as a graph problem on a DAG: I (^) introduce a vertex for each element Ai I (^) whenever Ai < Aj , include the edge Ai → Aj I (^) longest increasing subsequence corresponds to a longest path
q
p p q
r
r
A x B = C
I (^) Dimensions: (p × q) and (q × r ) yields (p × r ) I (^) Usual matrix multiplication algorithm takes O(pqr ) time I (^) Given N matrices, M 1 , M 2 ,... , Mn, find optimal multiplication order for the product M 1 M 2 · · · MN
I (^) An ordering corresponds to a fully-parenthesized expression I (^) Let P(n) be the number of ways to parenthesize n matrices
P(n) =
n∑− 1
i=
P(i)P(n − i), n > 1
I (^) P(n) is Ω(2n), so brute force enumeration is not practical I (^) Cultural note: P(n) are known as Catalan numbers
We will focus on one slice of the product:
M 1 × · · · × Mi− 1 × Mi × · · · × Mj × Mj+1 × · · · × Mn
I (^) Let Ci, j be the optimal cost to compute Mi × · · · × Mj I (^) Tabulate Ci,j for all possible values of i and j I (^) Bottom-up tabulation: begin with simplest choices for i and j I (^) j − i is a good measure of simplicity I (^) Rule for finding Ci, j? Assume all simpler values of C are available
I (^) We want to form the product Mi × · · · × Mj I (^) What is the last multiplication performed?
(Mi × · · · × Mk )×(Mk+1 × · · · × Mj )
I (^) Perform (Mi × · · · × Mk ) in the best way Perform (Mk+1 × · · · × Mj ) in the best way Do the final multiplication I (^) This costs Ci,k + Ck, j + cost of final multiplication I (^) Consider all possible k values
Given n matrices and their dimensions:
M 1 M 2 M 3... Mn r 0 × r 1 r 1 × r 2 r 2 × r 3 rn− 1 × rn
Cij =
0 if i = j mini≤k<j (Ci,k + Ck+1, j + ri− 1 rk rj ) if j > i
matrix dimensions M 1 10 × 20 M 2 20 × 50 M 3 50 × 1 M 4 1 × 100
matrix dimensions M 1 10 × 20 M 2 20 × 50 M 3 50 × 1 M 4 1 × 100
0
0
0
0
10,000 1,200 2,
5,
1,000 3,
I (^) So far, only the optimal cost is known I (^) Recording the best split points leads to a best parenthesization I (^) Si,j = k provided a best order splits the product at k:
opt(Mi · · · Mj ) = opt(Mi · · · Mk ) × opt(Mk+1 · · · Mj )
We intend to find the product M 1 M 2 M 3 M 4 M 5 M 6 , given these dimensions: matrix dimensions M 1 5 × 10 M 2 10 × 3 M 3 3 × 12 M 4 12 × 5 M 5 5 × 50 M 6 50 × 6
I (^) Modify OptimalMatrixChainCost to yield C 1 ,n and S I (^) Design an algorithm which will output a lowest-cost parenthesization, given S I (^) Determine the table C of costs I (^) Determine the table S of best splits I (^) Find a best parenthesization