You are on page 1of 2

Solutions (Instructor and TAs) Assignment 7 02-713, March 27, 2014

Solutions to these problems came from various approaches taken by the professor, TAs, students,
and the solution manual for AD. There may be other valid solutions as well.

1: String Alignment with Swap

We begin by extending the standard edit-distance formulation to include an additional case for
the swap operation. The new optimal recurrence is given by:

cost(ai , bj ) + OPT(i − 1, j − 1) if ai = bj



gap + OPT(i − 1, j) if ai not matched
OPT(i, j) = min


gap + OPT(i, j − 1) if bj not matched

swap + OPT(i − 2, j − 2) if ai = bj+1 and ai+1 = bj

After filling the grid, we update the traceback to translate a diagonal arrow of length 2 into a
swap operation. The other arrows retain the same meanings, namely that horizontal and vertical
arrows represent gaps and a diagonal arrow of length 1 represents a match.
Checking for the swap case in the optimal recurrence is done in constant time, so the final
running time is still O(nm), where n and m are the lengths of the input strings.

2: Bitonic Traveling Salesman

We first sort the cities according to x-coordinate, left to right, in O(n log(n)) time. We define Pij
as bitonic path from i to j where i ≤ j that includes points p1 , . . ., pj . Path Pij starts at city pi ,
and it goes strictly left to the leftmost city p1 , and then goes strictly right to pj . Going strictly
left means that each city in the path has a lower x-coordinate than the previous point (the indices
of the sorted points form a strictly decreasing sequence). Similarly, going strictly right means that
the indices of the sorted points form a strictly increasing sequence. Here, pj is the rightmost city
in Pij and is on the rightgoing subpath, and the leftgoing subpath may be degenerate, consisting
of just p1 .
Let d(i, j) be the euclidean distance between cities i and j, and b[i, j], for 1 ≤ i ≤ j ≤ n to be the
length of the shortest bitonic path Pij . Since the leftgoing subpath may be degenerate, we can
easily compute all values b[1, j]. The only value of b[i, i] that we will need is b[n, n], which is the
length of the shortest bitonic tour. Dynamic Programming Formulation will be:

d12
 if i = 1 and j = 2
b[i, j] = b[i, j − 1] + d(j − 1, j) if i < j − 1

min1≤k<j−1 (b[k, j − 1] + d(k, j)) if i = j − 1

First case is the base case; bitonic path ending at city p2 has p2 as its rightmost point, so it
consists only of p1 and p2 . Its length is d12 . Consider a shortest bitonic path Pij . If pj−1 is on its
rightgoing subpath, then it immediately preceeds pj . The subpath from p1 to pj−1 must be a
shortest subpath Pi,j−1 , since we otherwise could replace it to get a shorter bitonic path than Pij .
The length of Pij is therefore given by b[i, j − 1] + d(j − 1, j).
If pj−1 is on the leftgoing subpath, then it must be its rightmost point, so i = j − 1. Then pj has
an immediate predecessor pk , for k < j − 1, on the rightgoing subpath. Optimal substructure

1
Solutions (Instructor and TAs) Assignment 7 02-713, March 27, 2014

again applies: the subpath from pk to pj−1 must be a shortest bitonic path Pk,j−1 . The length of
Pij is therefore given by min1≤k<j−1 (b[k, j − 1] + d(k, j)).
In an optimal bitonic tour, one of the cities adjacent to pn must be pn−1 , so
b[n, n] = b[n − 1, n] + d(n − 1, n). The time complexity will be O(n2 ) since we iterate over i and j,
and it dominates the initial sorting time.

3: Context-free Grammar

This is also known as Cocke-Younger-Kasami (CYK) algorithm. Let the input be a string S
consisting of n characters: a1 , . . ., an , and grammar contain r nonterminal symbols R1 , . . ., Rr .
This grammar contains the subset Rs which is the set of start symbols. Pseudocode of the
algorithm is below.

CYK Algorithm
1: Let B[n, n, r] be an array of booleans. Initialize all elements of B to false.
2: for i = 1 to n do
3: for each unit production Rj → ai do
4: set B[i, 1, j] = true
5: end for
6: end for
7: for i = 2 to n do
8: for j = 1 to n − i + 1 do
9: for k = 1 to i − 1 do
10: for each production RA → RB RC do
11: if B[j, k, B] and B[j + k, i − k, C] then
12: B[j, i, A] = true
13: end if
14: end for
15: end for
16: end for
17: end for
18: if any of B[1, n, x] is true then
19: S is member of language
20: else
21: S is not member of language
22: end if

n2 (n−1)
Running time will be ni=2 n−i+1
Pi−1
|G| = O(n3 · |G|) = O(n3 ) where n is the
P P
j=1 k=1 |G| = 6
length of the parsed string and |G| is the size of the CNF grammar G.

You might also like