01.04.2015 Views

Sequence Comparison.pdf

Sequence Comparison.pdf

Sequence Comparison.pdf

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.

2.4 Dynamic Programming 27<br />

Let prefix sum P[i]=∑ i j=1 a j be the sum of the first i elements. It can be easily<br />

seen that ∑ j k=i a k = P[ j] − P[i − 1]. Therefore, if we wish to compute for a given<br />

position the maximum-sum segment ending at it, we could just look for a minimum<br />

prefix sum ahead of this position. This yields another linear-time algorithm for the<br />

maximum-sum segment problem.<br />

2.4.3 Longest Increasing Subsequences<br />

Given a sequence of numbers A = 〈a 1 ,a 2 ,...,a n 〉, the longest increasing subsequence<br />

problem is to find an increasing subsequence in A whose length is maximum.<br />

Without loss of generality, we assume that these numbers are distinct. Formally<br />

speaking, given a sequence of distinct real numbers A = 〈a 1 ,a 2 ,...,a n 〉, sequence<br />

B = 〈b 1 ,b 2 ,...,b k 〉 is said to be a subsequence of A if there exists a strictly increasing<br />

sequence 〈i 1 ,i 2 ,...,i k 〉 of indices of A such that for all j = 1,2,...,k, wehave<br />

a i j<br />

= b j . In other words, B is obtained by deleting zero or more elements from A.<br />

We say that the subsequence B is increasing if b 1 < b 2 < ... < b k . The longest increasing<br />

subsequence problem is to find a maximum-length increasing subsequence<br />

of A.<br />

For example, suppose A = 〈4, 8, 2, 7, 3, 6, 9, 1, 10, 5〉, both 〈2,3,6〉 and<br />

〈2,7,9,10〉 are increasing subsequences of A, whereas 〈8,7,9〉 (not increasing) and<br />

〈2,3,5,7〉 (not a subsequence) are not.<br />

Note that we may have more than one longest increasing subsequence, so we use<br />

“a longest increasing subsequence” instead of “the longest increasing subsequence.”<br />

Let L[i] be the length of a longest increasing subsequence ending at position i.They<br />

can be computed by the following recurrence:<br />

{ 1 + max<br />

L[i]=<br />

j=0,...,i−1 {L[ j] | a j < a i } if i > 0,<br />

0 if i = 0.<br />

Here we assume that a 0 is a dummy element and smaller than any element in A,<br />

and L[0] is equal to 0. By tabular computation for every i from 1 to n, each L[i] can be<br />

computed in O(i) steps. Therefore, they require in total ∑ n i=1 O(i)=O(n2 ) steps. For<br />

each position i, we use an array P to record the index of the best previous element<br />

for the current element to concatenate with. By tracing back from the element with<br />

the largest L value, we derive a longest increasing subsequence.<br />

Figure 2.9 illustrates the process of finding a longest increasing subsequence of<br />

A = 〈4, 8, 2, 7, 3, 6, 9, 1, 10, 5〉. Takei = 4 for instance, where a 4 = 7. Its previous<br />

smaller elements are a 1 and a 3 , both with L value equaling 1. Therefore, we have<br />

L[4] =L[1]+1 = 2, meaning that the length of a longest increasing subsequence<br />

ending at position 4 is of length 2. Indeed, both 〈a 1 ,a 4 〉 and 〈a 3 ,a 4 〉 are an increasing<br />

subsequence ending at position 4. In order to trace back the solution, we use array<br />

P to record which entry contributes the maximum to the current L value. Thus, P[4]<br />

can be 1 (standing for a 1 ) or 3 (standing for a 3 ). Once we have computed all L and

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

Saved successfully!

Ooh no, something went wrong!