A Linear Space Algorithm for Computing Maximal Common
















- Slides: 16
A Linear Space Algorithm for Computing Maximal Common Subsequences Author: D. S. Hirschberg Publisher: Communications of the ACM 1975 Presenter: Han-Chen Date: 2010/04/07 1
Outline n n Introduction Algorithm A Algorithm B Algorithm C 2
Introduction n n LCS (Longest Common Subsequence) of two strings has been solved in quadratic time and space. We present an algorithm which will solve this problem in quadratic time and in linear space. 3
Algorithm A Input string A 1 m and B 1 n output matrix L 4
Analysis of Algorithm A n n Time Complexity : execute m*n times → O(mn) Space Complexity : input arrays m + n output array (m+1)*(n+1) space require → O(mn) 5
Algorithm B (I) n n Space require : O(m+n) It can output the max common length but cannot record the max common subsequence. 6
Algorithm B (II) Input string A 1 m and B 1 n output matrix LL 7
Analysis of Algorithm B n n Time Complexity : execute m*n times → O(mn) Space Complexity : input arrays m + n output array n+1 space require → O(m+n) 8
Algorithm C n Divide and conquer 1 B 1 j String B n Find j String A 1 i ALG B i=m/2 ALG B Ai+1, m m Bj+1, n 9
Algorithm C n n n L(i, j) j=0 … n the maximum lengths of common subsequence A 1 i and B 1 j L*(i, j) j=0 … n the maximum lengths of common subsequence Am, i+1 and Bn, j+1 Define M(i) = max{ L(i, j) + L*(i, j) } 0 ≦ j ≦n Theorem M(i) = L(m, n) Proof: for all L(i, j) + L*(i, j) ≦ L(m, n) S(i, j) : any maximal common subsequence of A 1 i and B 1 j S*(i, j) : any maximal common subsequence of Ai+1, m and Bj+1, n Then C= S(i, j) || S*(i, j) is a common subsequence of A 1 m and B 1 n of length M(i). Thus L(m, n) ≧ L(i, j) + L*(i, j) 10
Algorithm C n exist some L(i, j) + L*(i, j) ≧ L(m, n) S(m, n) : any maximal common subsequence of A 1 m and B 1 n S(m, n) is a subsequence of A 1 m so S(m, n) = S 1 || S 2 that S 1 is a subsequence of A 1 i , S 2 is a subsequence of Ai+1, m Also S(m, n) is a subsequence of B 1 n so there exists j such that S 1 is a subsequence of B 1 j and S 2 is a subsequence of Bj+1, n By definition of L and L* , |S 1| ≦ L(i, j) and |S 2| ≦ L*(i, j) Thus L(m, n) = |S(m, n)| = |S 1| + |S 2| ≦ L(i, j) + L*(i, j) So M(i) = max{ L(i, j) + L*(i, j) } = L(m, n) 11
Algorithm C m, i+1 12
Algorithm C 13
Analysis of Algorithm C (I) n n Time analysis: O(mn) + O(1/2 mn) + O(1/4 mn) + … = O(mn(1+1/2+1/4+…)) = O(mn) 15
Analysis of Algorithm C (II) n Space analysis: we calls ALG B use temporary storage which is m and n. Exclusive of recursive calls to ALG C, ALG C uses a constant amount of memory space. There are 2 m-1 calls to ALG C, so ALG C require memory space O(m+n). 16
Proof 2 m-1 calls to ALG C n n Let m≦ 2 r m=1 there are 2*1 – 1 = 1 call to ALG C Assume m ≦ 2 r = M there are 2 m-1 calls to ALG C For m’ = 2 r+1 = 2 M. First call ALG C to partition 2 part, each calls call 2 m-1 times ALG C. So there are 1 + (2 m-1) = 4 m - 1 = 2 m’ – 1 calls. 17