Docsity

Homework 3 with Solutions - Algorithm Design | ECS 122A, Assignments of Computer Science

Toggle sidebar
bg1
ECS122A Homework Assignmet #3 Solutions
1. In the assembly-line scheduling problem, let
a=·4 6 5 8 2 4
8 3 4 6 4 7 ¸, t =·3 2 1 2 1
1 3 2 3 1 ¸
and
e=£4 2 ¤, x =£1 3 ¤.
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=·8 14 19 25 26 30
10 11 15 21 27 34 ¸, l =·1 1∗2 2 1
1 2 2 2 1∗¸
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.
2. Exercise 15.1-2
Solution: We can prove the statement by induction. The base case is when j=n, and we have
r1(n) = 2n−n= 20= 1
r2(n) = 2n−n= 20= 1.
For the induction step, our inductive hypothesis is that
r1(j) = 2n−j
r2(j) = 2n−j.
Then
r1(j−1) = r1(j) + r2(j) = 2n−j+ 2n−j= 2 ·2n−j= 2n−j+1 = 2n−(j−1) .
and
r2(j−1) = r1(j) + r2(j) = 2n−j+ 2n−j= 2 ·2n−j= 2n−j+1 = 2n−(j−1) ,
which completes the inductive proof.
3. Exercise 15.1-3
Solution: First, recall that Pn
k=0 ark=a1−rn+1
1−r. Then the total number of reference is
2
X
i=1
n
X
j=1
ri(j) =
2
X
i=1
n
X
j=1
2n−j=
2
X
i=1
(2n−1+ 2n−2+···+ 20) = 2 ·µ2n−1
2−1= 2n+1 −2.
4. For the sequences X=hB, C, A, A, B, AiY=hA, B, A, C, B ifill in the dynamic programming table
including the arrows (as in Figure 15.5 of CLRS on page 354) for the longest common subsequence
(LCS) of the two sequences. List the final LCS.
Solution: First, we build the following c−btable is
c, b =
0 1 2 3 4 5
yjA B A C B
0xj0 0 0 0 0 0
1B0↑0տ1←1←1տ1
2C0↑0↑1↑1տ2←2
3A0տ1←1տ2←2←2
4A0տ1←1տ2←2←2
5B0↑1տ2←2←2տ3
6A0տ1↑2տ3←3←3
1
pf3

Partial preview of the text

Download Homework 3 with Solutions - Algorithm Design | ECS 122A and more Assignments Computer Science in PDF only on Docsity!

ECS122A Homework Assignmet #3 Solutions

  1. In the assembly-line scheduling problem, let

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 =

[

1 1 ∗^2 2

]

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.

  1. Exercise 15.1- Solution: We can prove the statement by induction. The base case is when j = n, and we have r 1 (n) = 2 n−n^ = 2^0 = 1 r 2 (n) = 2 n−n^ = 2^0 = 1. For the induction step, our inductive hypothesis is that r 1 (j) = 2 n−j r 2 (j) = 2 n−j^. Then r 1 (j − 1) = r 1 (j) + r 2 (j) = 2n−j^ + 2n−j^ = 2 · 2 n−j^ = 2n−j+1^ = 2n−(j−1). and r 2 (j − 1) = r 1 (j) + r 2 (j) = 2n−j^ + 2n−j^ = 2 · 2 n−j^ = 2n−j+1^ = 2n−(j−1), which completes the inductive proof.
  2. Exercise 15.1- Solution: First, recall that

∑n k=0 ar

k (^) = a 1 −rn+ 1 −r. Then the total number of reference is ∑^2

i=

∑^ n

j=

ri(j) =

∑^2

i=

∑^ n

j=

2 n−j^ =

∑^2

i=

(2n−^1 + 2n−^2 + · · · + 2^0 ) = 2 ·

2 n^ − 1 2 − 1

= 2n+1^ − 2.

  1. For the sequences X = 〈B, C, A, A, B, A〉 Y = 〈A, B, A, C, B〉 fill in the dynamic programming table including the arrows (as in Figure 15.5 of CLRS on page 354) for the longest common subsequence (LCS) of the two sequences. List the final LCS. Solution: First, we build the following c − b table is

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.

  1. Two character strings may have many common substrings. Substrings are required to be contiguous in the original string. For example, photograph and tomography have several common substrings of length one (i.e., single letters), and common substrings ph, to, and ograph (as well as all the substrings of ograph). The maximum common substring length is 6. Let X = x 1 x 2 · · · xm and Y = y 1 y 2 · · · yn be two character strings.

(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).

  1. Suppose that the denominations of the coins in a country are d 1 < d 2 < · · · < dn (e.g., 1, 5, 10, 25 for the United States). The problem to consider is:

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?