

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!


Solutions HW Nina Amenta ECS 122A
Yes, we can use a similar dynamic programming algorithm to find the sequence of matrix multiplications which maximizes the number of scalar multiplys, rather than the sequence that minimizes it. Let A 1 ,... , An be the input sequence of matrices, with dimensions p 0 × p 1 ,... , pn− 1 × pn, and let c(i, j) be the maximum number of scalar multiplies needed to multiply together the subsequence Ai,... , Aj. Then we can express c(i, j) recursively: c(i, j) = maxk∈{i,...,j−i− 1 }c(i, k) + c(k + 1, j) + pipkpj
This recursive formulation leads to a similar pyramid of subproblems, where the solutions for the larger subproblems near the top of the pyramid can be calculated given the solutions for the smaller subproblems near the bottom. The running time is again O(n^3 ).
We want to show by substitution that the solution to the recurrence below is Ω(2n), with base case P (1) = 1.
P (n) =
n∑− 1
k=
P (k)P (n − k)
Like all proofs by substitution, this is a proof by induction. We’ll do the induction step first, since it will determine what n 0 we use for the base case.
Induction step: Assume inductively that P (i) ≥ c 2 i, for all 1 ≤ i < n. Then we can write P (n) as:
P (n) ≥
n∑− 1
k=
(c 2 k)(c 2 n−k) = (n − 1)c^22 n
We want to prove a lower bound on P (n), so we need to show that P (n) ≥ c 2 n, for some c. This will be true for c ≥ 1.
Base case: We now need to pick an n 0 so that P (n 0 ) ≥ c 2 n^0 , for any c ≥ 1. P (1) = 1, so this does not work. P (2) = 4, which does work, so we set n 0 = 2.
The idea of this problem is that we want to describe how different a string x is from a string y. We express the difference by counting the number of editing operations required to transform x into y. We consider moving a pointer i from left to right across string x, and performing operations to transform x into y. As we go along, we are producing a copy of y, also from right to left, with a pointer j pointing to the current character in y. If x(i) = y(j), we do not need to change the current character. We call this a COPY operation (although we could also have called it a NOP) and we give it cost 0. The other operations are INSERT, REPLACE and DELETE. We want to find the cost c(m, n) of the minimum cost sequence of operations, where m is the length of n and m is the length of y. To do this using dynamic programming, we consider subproblems of the form c(i, j), the minimum cost required to transform the string x(1),... , x(i) to y(1),... , y(j). We express this recursively by thinking about the last operation which will be performed in the sequence of operations. If x(i) = y(j), we could first transform x(1),... , x(i − 1) to y(1),... , y(j − 1) and then COPY x(i) to y(j). Other options would be to first transform x(1),... , x(i − 1) to y(1),... , y(j) and then DELETE x(i), or to first transform x(1),... , x(i) to y(1),... , y(j − 1) and then INSERT y(j). There would be no point in using a REPLACE operation. If x(i) 6 = y(j), then we might transform x(1),... , x(i − 1) to y(1),... , y(j − 1) and
then REPLACE x(i) with y(j). Or we might use a DELETE or INSERT, as above. A COPY would not be possible. So we get this recursive formulation for the cost:
c(i, j) =
min{c(i − 1 , cj − 1), 1 + c(i − 1 , j), 1 + c(i, j − 1)} if x(i) = y(j) min{1 + (c − 1 , j − 1), 1 + c(i − 1 , j), 1 + c(i, j − 1)} if x(i) 6 = y(j)
To compute c(i, j) using this formulation, we need to have already solved the smaller subproblems c(i − 1 , j − 1), c(i, j − 1), c(i − 1 , j). So we need to solve the subproblems in some order that will ensure this. We can consider the subproblems organized into a table as follows:
c(n, 0) c(n, 1) · · · c(n, m) · · · · · · c(1, 0) c(1, 2) · · · c(1, m) c(0, 0) c(0, 2) · · · c(0, m)
The only solutions for the bottom row require INSERTING j characters, so c(0, j) = j. Similarly the only solutions for the left column are to DELETE i characters, so c(0, i) = i. If we solve the next-lowest row first, from left to right, then the next-lowest row, and so on, we will always have the three subproblem solutions we need to compute c(i, j). We can compute c(i, j) from the subproblem solutions c(i − 1 , j − 1), c(i, j − 1), c(i − 1 , j) in constant time. The total number of subproblems is (n + 1)(m + 1), so the running time of the dynamic programming solution is O(nm).