04.09.2013 Views

Algorithm Design

Algorithm Design

Algorithm Design

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

310<br />

Chapter 6 Dynamic Programming<br />

Solution This problem is vaguely reminiscent of Segmented Least Squares:<br />

we have a long sequence of "data" (the string A) that we want to "fit" with<br />

shorter segments (the strings in L).<br />

If we wanted to pursue this analogy, we could search for a solution as<br />

follows. Let B = B1B2 ¯ ¯ ¯ Be denote a concatenation over L that aligns as well<br />

as possible with the given string A. (That is, B is an optimal solution to the<br />

input instance.) Consider an optimal alignment M of A with B, let t be the first<br />

position in A that is matched with some symbol in Be, and let A e denote the<br />

substring of A from position t to the end. (See Figure 6.27 for an illustration<br />

of this with g = 3.) Now, the point is that in this optimal alignment M, the<br />

substring Ae is optimally aligned with B6 indeed, if there were a way to better<br />

align A e with Be, we could substitute it for the portion of M that aligns A e with<br />

B e and obtain a better overall alignment of A with B.<br />

This tells us that we can look at the optimal solution as follows. There’s<br />

some final piece of Aa that is aligned with one of the strings in L, and for_this<br />

piece all we’re doing is finding the string in L that aligns with it as well as<br />

possible. Having found this optimal alignment for Aa, we can break it bff al~d<br />

continue to find the optimal solution for the remainder of A.<br />

Thinking about the problem this way doesn’t tell us exactly how to<br />

proceed--we don’t know how long A~ is supposed to be, or which ~tring in<br />

L it should be aligned with. But this is the kind of thing we can search over<br />

in a dynamic programming algorithm. Essentially, we’re in about the same<br />

spot we were in with the Segmented Least Squares Problem: there we knew<br />

that we had to break off some final subsequence of the input points, fit them<br />

as well as possible with one line, and then iterate on the remaining input<br />

points.<br />

So let’s set up things to make the search for A~ possible. First, let A[x : y]<br />

denote the substring of A consisting of its symbols from position x to position<br />

y, inclusive. Let c(x, y) denote the cost of the optimal alignment of A[x : y] with<br />

any string in L. (That is, we search over each string in L and find the one that<br />

Figure 6.27 In the optimal concatentation of strings to align with A, there is a final<br />

string (B 3 in the figure) that a~gns with a substring of A (A3 In the figure) that e~xtends<br />

from some position t to the end.<br />

t<br />

Solved Exercises<br />

aligns best with A[x :y].) Let OPT(J) denote the alignment cost of the optimal<br />

solution on the string All :j].<br />

The argument above says that an optimal solution on A[1 :j] consists of<br />

identifying a final "segment boundary" t < j, finding the optimal alignment<br />

of A[t :j] with a single string in L, and iterating on All : t - !]. The cost of<br />

this alignment of A[t :j] is just c(t,j), and the cost of aligning with what’s left<br />

is just OPT(t -- 1). This suggests that our subproblems fit together very nicely,<br />

and it justifies the. following recurrence.<br />

(6.37) OPT(j) -- mint< ] c(t,j) + OPT(t -- 1) forj > !, and OPT(0) ----- 0.<br />

The fl~ algorithm consists of first computing the quantities c(t,j), for t < j,<br />

and then building up the values OPT(j) in order of increasing j. We hold these<br />

values in an array M.<br />

Set M[0] = o<br />

For all pairs l_

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!