hw09 Solution PDF
hw09 Solution PDF
CSCI-GA.1170-001/Summer 2016
Solution to Homework 9
Problem 1 (CLRS 15.1-3). (1 point) Consider a modification of the rod-cutting problem in
which, in addition to a price pi for each rod, each cut incurs a fixed cost of c. The revenue
associated with a solution is now the sum of the prices of the pieces minus the costs of making
the cuts. Give a dynamic-programming algorithm to solve this modified problem.
Solution: We can modify B OTTOM-UP-CUT-ROD algorithm from section 15.1 as follows:
B OTTOM-UP-CUT-ROD(p, n, c)
1 let r[0..n] be a new array
2 r[0] = 0
3 for j = 1 to n
4
q =
5
for i = 1 to j 1
6
q = max(q, p[i] + r[ j i] c)
7
r[ j] = max(q, p[ j])
8 return r[n]
We need to account for cost c on every iteration of the loop in lines 5-6 but the last one, when
i = j (no cuts). We make the loop run to j 1 instead of j, make sure c is subtracted from the
candidate revenue in line 6, then pick the greater of current best revenue q and p[ j] (no cuts)
in line 7.
Problem 2 (CLRS 15.4-5). (1 point) Give an O(n2 )-time algorithm to find the longest monotonically increasing subsequence of a sequence of n numbers.
Solution: We observe that the longest monotonically increasing subsequence (LIS) of sequence
S is a longest common subsequence (LCS) of S and sorted S. For a sequence of length n, sorting
can be done in O(n lg n) time, and finding the LCS in O(n2 ) time. Utilizing the LCS-LENGTH
and PRINT-LCS procedures from section 15.4, we can find the LIS as follows:
LIS(S)
1 S 0 = SORT(S)
2 c, b = LCS-LENGTH(S, S 0 )
3 PRINT-LCS(b, S, S.leng th, S.leng th)
Problem 3 (CLRS 15.4-6). (2 points) Give an O(n lg n)-time algorithm to find the longest
monotonically increasing subsequence of a sequence of n numbers. (Hint: Observe that the
last element of a candidate subsequence of length i is at least as large as the last element of
a candidate subsequence of length i 1. Maintain candidate subsequences by linking them
through the input sequence.)
1
PRINT-LIS(X , P, M , L)
1 let S[0..L] be a new array
2 k = M [L]
3 for i = L 1 to 0
4
S[i] = X [k]
5
k = P[k]
Problem 4 (CLRS 15-4). (3 points) Consider the problem of neatly printing a paragraph
with a monospaced font (all characters having the same width) on a printer. The input text
2
lc[i, j] =
if ex t r as[i, j] < 0,
if j = n and ex t r as[i, j] 0,
otherwise.
Negative ex t r as[i, j] indicates that words i through j dont fit; this should never occur
in a correct solution and so we assign this case an infinite cost.
We dont need to minimize ex t r as[i, j] in the last line, so any non-negative value is an
acceptable solution.
The remaining case specifies the cost function according to the problem statement.
We note that the problem exhibits optimal substructure: If an arrangement of words 1, ..., j
with the last line containing words i, ..., j is optimal, the preceding lines contain an optimal
arrangement of words 1, ..., i 1.
Let us define c[ j] to be the cost of an optimal arrangement of words 1, ..., j. Following the
optimal substructure argument above, c[ j] = c[i1]+lc[i, j]. Enumerating all possible choices
for i (the first word on the last line for a given subproblem) gives the following recursive
definition for c[ j]:
0
if j = 0,
c[ j] =
min1i j (c[i 1] + lc[i, j]) otherwise.
To be able to reconstruct the actual solution, we also record the arrangement in array p such
that p[ j] = k indicates that c[ j] ended up picking c[k 1] + lc[k, j] for the optimal solution.
This way, the last line of the final arrangement contains words p[n], ..., n, the line before last
words p[p[n]], ..., p[n] 1, and so on.
Implementing each of the steps above as a separate procedure gives:
COMPUTE-EXTRAS(l, n, M )
1 let e x t r as[1..n, 1..n] be a new array
2 for i = 1 to n
3
// One-word line, so just width minus word length.
4
e x t r as[i, i] = M l[i]
5
for j = i + 1 to n
6
// Previous minus new word length minus space between words.
7
ex t r as[i, j] = ex t r as[i, j 1] l[ j] 1
8 return ex t r as
COMPUTE-LINE-COST(ex t r as, n)
1 let l c[1..n, 1..n] be a new array
2 for i = 1 to n
3
for j = i to n
4
if ex t r as[i, j] < 0
5
// Words dont fit.
6
lc[i, j] =
7
elseif j = n and ex t r as[i, j] 0
8
// Last line and at least zero trailing spaces.
9
lc[i, j] = 0
10
else
11
// Normal cost function.
12
lc[i, j] = (ex t r as[i, j])3
13 return lc
COMPUTE-COST(lc, n)
1 let c[1..n] be a new array
2 c[0] = 0
3 for j = 1 to n
4
c[ j] =
5
for i = 1 to j
6
if c[i 1] + lc[i, j] < c[ j]
7
c[ j] = c[i 1] + lc[i, j]
8
p[ j] = i
9 return c, p
PRINT-LINES(p, j)
1
2
3
4
5
6
7
8
i = p[ j]
if i = 1
k=1
else
k = PRINT-LINES(p, i 1) + 1
// Print words i through j on line k.
PRINT(k, i, j)
return k
4
PRINT-PARAGRAPH(l, n, M )
1 e x t r as = COMPUTE-EXTRAS(l, n, m)
2 l c = COMPUTE-LINE-COST(ex t r as, n)
3 c, p = COMPUTE-COST(lc, n)
4 PRINT-LINES(p, n)
The algorithm runs in (n2 ) time and requires (n2 ) space. Both characteristics can be improved to (nM ) by observing that at most dM /2e words can fit on a line (each word being at
least one character long plus spaces between words), and only computing and storing ex t r as
and l c for j i + 1 dM /2e.
Problem 5 (CLRS 15-5). (4 points) See CLRS 15-5 for full problem statement.
(a) Given two sequences x[1..m] and y[1..n] and set of transformation-operation costs, the
edit distance from x to y is the cost of the least expensive operation sequence that transforms x to y. Describe a dynamic-programming algorithm that finds the edit distance
from x to y and prints an optimal operation sequence. Analyze the running time and
space requirements of your algorithm.
Solution:
Let us define X i = x[1..i] and Y j = y[1.. j] to be prefixes of sequences x and y, X i Y j
to be a problem of determining the cost of the least expensive operation sequence that
transforms X i to Y j , and c[i, j] to be that cost.
We observe that the problem exhibits optimal substructure: an optimal solution to X p
Yq includes optimal solutions to X i<p Y j<q .
We now consider different possibilities for the last operation in the optimal solution to
X i Yj :
Copy. Then x[i] = y[ j], the remaining subproblem is X i1 Y j1 and c[i, j] =
c[i 1, j 1] + cost(cop y).
Replace. Then x[i] 6= y[ j], the remaining subproblem is X i1 Y j1 and c[i, j] =
c[i 1, j 1] + cost(r eplace).
Delete. Then we have no restrictions on x[i] and y[ j], the remaining subproblem
is X i1 Y j and c[i, j] = c[i 1, j] + cost(delet e).
Insert. Then we have no restrictions on x[i] and y[ j], the remaining subproblem is
X i Y j1 and c[i, j] = c[i, j 1] + cost(inser t).
Twiddle. Then x[i] = y[ j 1] and x[i 1] = y[ j] for i, j 2, the remaining
subproblem is X i2 Y j2 and c[i, j] = c[i 2, j 2] + cost(t widdle).
Kill. This must be the final operation, so the current problem must be X m Yn .
We can kill the string starting from any 0 i < m and so c[i, j] = c[m, n] =
min0i<m (c[i, n]) + cost(kill).
We can now provide the following recursive definition for c[i, j]:
c[i 1, j 1] + cost(cop y)
if x[i] = y[ j],
c[i 1, j] + cost(delet e)
in all cases,
c[i, j] = min
c[i, j 1] + cost(inser t)
in all cases,
COMPUTE-EDIT-DISTANCE(x, y, m, n)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
The algorithm fills an m n table, spending constant time on each cell, so the running
time and space are both (mn).
7
We can reconstruct the actual sequence of operations using the following procedure:
PRINT-OPERATIONS(op, i, j)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
if i = 0 and j = 0
return
if op[i, j] = COPY or op[i, j] = REPLACE
i0 = i 1
j0 = j 1
elseif op[i, j] = DELETE
i0 = i 1
j0 = j
elseif op[i, j] = INSERT
i0 = i
j0 = j 1
elseif op[i, j] = TWIDDLE
i0 = i 2
j0 = j 2
else // KILL
i 0 = GET-KILL-INDEX(op[i, j])
j0 = j
PRINT-OPERATIONS(op, i 0 , j 0 )
PRINT(op[i, j])
(b) Explain how to cast the problem of finding an optimal DNA alignment as an edit distance
problem using a subset of the transformation operations copy, replace, delete, insert,
twiddle, and kill.
The DNA alignment problem can be reduced to the edit distance problem by taking the
following operation costs:
COST (COPY) = 1
COST (REPLACE) = +1
COST (DELETE) = +2
space case.
COST (KILL)