

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!


ECS122A Homework Assignmet #3 Solutions
a =
, t =
and e =
, x =
Determine which stations to choose from line 1 and to choose from line 2 in order to minimize the total time through the factory. Solution: By straightforward calculation following the example given in section 15.1 of CLRS.
f =
, l =
In the array l, starred items indicate ties, in which case we deferred arbitrarily to line 1 although line 2 could have just as easily been chosen. f ∗^ = 31 and l∗^ = 1, which means that the optimal path through the line is to enter on line 1, transfer to 2, stay on line 2 through station 4, transfer back to 1, and then stay on line 1 and exit or 1,2,2,2,1,1 exit.
∑n k=0 ar
k (^) = a 1 −rn+ 1 −r. Then the total number of reference is ∑^2
i=
∑^ n
j=
ri(j) =
i=
∑^ n
j=
2 n−j^ =
i=
(2n−^1 + 2n−^2 + · · · + 2^0 ) = 2 ·
2 n^ − 1 2 − 1
= 2n+1^ − 2.
c, b =
yj A B A C B 0 xj 0 0 0 0 0 0 1 B 0 ↑ 0 տ 1 ← 1 ← 1 տ 1 2 C 0 ↑ 0 ↑ 1 ↑ 1 տ 2 ← 2 3 A 0 տ 1 ← 1 տ 2 ← 2 ← 2 4 A 0 տ 1 ← 1 տ 2 ← 2 ← 2 5 B 0 ↑ 1 տ 2 ← 2 ← 2 տ 3 6 A 0 տ 1 ↑ 2 տ 3 ← 3 ← 3
By the table, we conclude that the length of the LCS is 3. BAB is an LCS as is ABA.
(a) Give a dynamic programming algorithm to find the maximum common substring length for X and Y. (b) Analyze the worst-case running time and space requirements of your algorithm as functions of n and m. (c) Demonstrate your dynamic programming algorithm for finding the maximum common substring length of character strings (c.1) photograph and tomography (c.2) cabccb and babcba. by construct a dynamic programming table similar to Figure 15.5 of CLRS on page 354.
Solution:
(a),(b) We use a table D[0..m, 0 ..n], where d[i, j] is the length of an MCS ending at xi and yj. The first row and column are initialized to be zero. Then the remaining entries (by the optimal substructure of MCS) are computed by the following recursive formula
d[i, j] =
d[i − 1 , j − 1] + 1 if xi = yj 0 otherwise
The table entries can be computed row-by-row. The answer to the problem is the largest entry of D. Based on the above formula, we can easily write down a pseudocode for computing the MCS (omit). The running time of this procedure is Θ(mn), since each table entry takes O(1) to compute.
An alternative solution uses a table M [1..m, 1 ..n] where
m[i, j] =
1 if xi = yj 0 otherwise
After filling the table, the algorithm scans all diagonals for the longest string of contiguous 1’s. Each such string corresponds to a common substring. This method also takes Θ(mn). For the first solution, only two rows need actually be stored. It is easy to keep track of the largest value computed so far while filling the table. The second solution uses more space, it requires to store the the whole m × n table. (c.1) The length of MCS of character strings algorithm and logarithm is 5. The MCS is rithm. (c.2) The length of MCS of the character strings cabccb and babcba is 3. The MCS is abc. (tables are omitted).
Given an integer A, what is the minimum number of coins needed to make A cents in change?
(a) Under what condition(s) on the denominations di are you able to make change for any amount A?