"Algorithm" the longest common subsequence (NLOGN)

Source: Internet
Author: User

Reprint Annotated Source: http://blog.csdn.net/wdq347/article/details/9001005

The most common algorithm for the longest common subsequence (LCS) is the time Complexity O (n^2) dynamic Programming (DP) algorithm, but the paper "a Fast Algorithm for Computing" in James W. Hunt and Thomas G. Szymansky Longest Common subsequence ", an algorithm for the lower limit of O (Nlogn) is given.

Theorem: Set the sequence A length of n,{a (i)}, sequence B length is m,{b (i)}, consider all elements of a in B ordinal, that a element in B is the ordinal of {PK1,PK2,..}, the ordinal number in descending order, and then in a order to get a new sequence, The longest, strictly incrementing subsequence of this new sequence corresponds to the longest common subsequence of a and B.

For example, a={a,b,c,d,b},b={b,c,a,b}, a corresponding to the sequence number of B is 2,b corresponding to the ordinal of {3,0},c corresponding to the 1,d corresponding to the empty set, the resulting new sequence is {2, 3, 0, 1, 3, 0}, its longest strict increment subsequence is {0,1,3}, The corresponding common sub-sequence is {B, C, b}

The proof process of the original paper is more complicated, in fact, it can be proved simply by one by one correspondence. That is to prove a, b of a common sub-sequence and a new sequence of a strict increment sub-sequence one by one corresponds.

(1) A and B of a common sub-sequence corresponding to a new sequence of a strict increment subsequence

Assuming that a common subsequence length of A and B is k, its common subsequence can be written in A and B as

{AI1,AI2, ..., Aik}

{bj1,bj2, ..., BJK}

So there is Ai1 = Aj1,ai2 = Aj2, ...., Aik = AJK, consider the element Bj1 in B in the ordinal p (Bj1), then there is

P (Bj1) < P (Bj2) < ... < P (BJK)

Note that this strictly incrementing sub-sequence belongs to a subsequence of the new sequence, so the proof

(2) A strict increment subsequence of a new sequence corresponds to a common sub-sequence of A and b

Set a strict increment subsequence of the new sequence {p1,p2, ..., Pk}, any two identical p cannot belong to the same element in a, because the ordinal of an element in B is arranged in descending order, but this sequence is strictly ascending sequence, contradiction. So each p corresponds to a different position in a element, set to {Ai1, Ai2, ..., Aik}.

Because P is a strictly incrementing sequence, each p also corresponds to the only element in B, assuming {bj1,bj2, ..., BJK}, defined by the definition of P ai1= Bj1, Ai2 = Bj2, ...., Aik = BJK, so the certificate.

The implementation is more complex, there are several steps:

(1) Ordering of sequence B

(2) calculates the ordinal number of each element in a in B and forms a new sequence

(3) Calculation of the longest strictly ascending subsequence using Lis method

(4) to get the longest common sub-sequence


Performance Analysis:

(1) Sorting complexity of Nlogn

(2) Gets the complexity of the ordinal of an element in B, the minimum is Logn, the maximum is N, and gets the complexity of all elements nlogn = = = N*n

(3) LIS complexity of Nlogn

So the overall complexity between Nlogn to N*n Logn, but if the (2) step in a of the elements in B in the ordinal logarithm of a few, the performance is quite superior, in the actual test, string is lowercase letters, the length of 10000 cases, this method than ordinary LCS faster than If the character in string is expanded to char, or 0-255, this method is at least an order of magnitude faster than the normal LCS.

"Algorithm" the longest common subsequence (NLOGN)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.