Linear space LCS algorithm Divide-and Conquer : Linear space LCS algorithm 2019/5/22 chapter25
The Complexity of the Normal Algorithm for LCS Time Complexity: O(nm), n and m are the lengths of sequences.. Space Complexity: O(nm). For some applications, the length of the sequence could be 10,000. The space required is 100,000,000, which might be too large for some computers. For most of computers, it is hard to handle sequences of length 100,000. 2019/5/22 chapter25
Is it possible to use O(n+m) space? Yes. Main Idea: As long as the cells are not useful for computing the values of other cells, release them. O(n) space is enough, where n is the length of the two sequences . 2019/5/22 chapter25
Original method for CS A B C A B C B A 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 A 0 1 1 1 2 2 2 2 2 C 0 1 1 2 2 2 3 3 3 B 0 1 2 2 2 3 3 4 4 A 0 1 2 2 3 3 3 4 5 2019/5/22 chapter25
Steps for computing c[n,m], the last cell in the matrix. A B C A B C B A 0 0 0 0 0 0 0 0 0 B 0 A 0 Initial values C 0 A 0 2019/5/22 chapter25
Steps for computing c[n,m] A B C A B C B A 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 A 0 C 0 B 0 2019/5/22 chapter25
Steps for computing c[n,m] A B C A B C B A 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 A 0 1 1 1 2 2 2 2 2 C 0 B 0 the 2nd row is no longer useful B 0 A 0 2019/5/22 chapter25
Steps for computing c[n,m] A B C A B C B A 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 A 0 1 1 1 2 2 2 2 2 C 0 B 0 the 2nd row is no longer useful B 0 A 0 2019/5/22 chapter25
Steps for computing c[n,m] A B C A B C B A 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 A 0 1 1 1 2 2 2 2 2 C 0 1 1 2 2 2 3 3 3 B 0 the 3rd row is no longer useful B 0 A 0 2019/5/22 chapter25
Steps for computing c[n,m] A B C A B C B A 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 A 0 1 1 1 2 2 2 2 2 C 0 1 1 2 2 2 3 3 3 B 0 A 0 2019/5/22 chapter25
Steps for computing c[n,m] A B C A B C B A 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 A 0 1 1 1 2 2 2 2 2 C 0 1 1 2 2 2 3 3 3 B 0 1 2 2 2 3 3 4 4 B 0 4th row is not useful A 0 2019/5/22 chapter25
Steps for computing c[n,m] A B C A B C B A 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 A 0 1 1 1 2 2 2 2 2 C 0 1 1 2 2 2 3 3 3 B 0 1 2 2 2 3 3 4 4 B 0 4th row is not useful A 0 2019/5/22 chapter25
Steps for computing c[n,m] A B C A B C B A 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 A 0 1 1 1 2 2 2 2 2 C 0 1 1 2 2 2 3 3 3 B 0 1 2 2 2 3 3 4 4 B 0 1 2 2 2 3 3 4 4 A 0 2019/5/22 chapter25
Steps for computing c[n,m] A B C A B C B A 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 A 0 1 1 1 2 2 2 2 2 C 0 1 1 2 2 2 3 3 3 B 0 1 2 2 2 3 3 4 4 B 0 1 2 2 2 3 3 4 4 A 0 2019/5/22 chapter25
Steps for computing c[n,m] A B C A B C B A 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 A 0 1 1 1 2 2 2 2 2 C 0 1 1 2 2 2 3 3 3 B 0 1 2 2 2 3 3 4 4 A 0 1 2 2 3 3 3 4 5 2019/5/22 chapter25
Linear Space Algorithm for LCS At any time, the space required is at most one column + 2 rows,i.e.O(n+m). How to do backtracking for getting the LCS? 2019/5/22 chapter25
Backtracking using linear space Simple method: Go back by one step at a time. Compute the sub-matrix again. Repeat the process until back to the 1st row or the 1st column. Time required O(n3) (n=m). 2019/5/22 chapter25
Backtracking A B C A B C B A 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 A 0 1 1 1 2 2 2 2 2 C 0 1 1 2 2 2 3 3 3 B 0 1 2 2 2 3 3 4 4 A 0 1 2 2 3 3 3 4 5 2019/5/22 chapter25
Backtracking A B C A B C B A 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 A 0 1 1 1 2 2 2 2 2 C 0 1 1 2 2 2 3 3 3 B 0 1 2 2 2 3 3 4 4 B 0 1 2 2 2 3 3 4 4 Time O(nm) A 0 1 2 2 3 3 3 4 5 Re-compute the cells in the blue box. 2019/5/22 chapter25
Backtracking A B C A B C B A 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 A 0 1 1 1 2 2 2 2 2 C 0 1 1 2 2 2 3 3 3 B 0 1 2 2 2 3 3 4 4 B 0 1 2 2 2 3 3 4 4 Time O(nm) A 0 1 2 2 3 3 3 4 5 Go back by one step. 2019/5/22 chapter25
Backtracking A B C A B C B A 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 A 0 1 1 1 2 2 2 2 2 C 0 1 1 2 2 2 3 3 3 B 0 1 2 2 2 3 3 4 4 B 0 1 2 2 2 3 3 4 4 Time O(nm) A 0 1 2 2 3 3 3 4 5 Re-compute the values in the blue box. 2019/5/22 chapter25
Backtracking A B C A B C B A 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 A 0 1 1 1 2 2 2 2 2 C 0 1 1 2 2 2 3 3 3 B 0 1 2 2 2 3 3 4 4 B 0 1 2 2 2 3 3 4 4 Time O(nm) A 0 1 2 2 3 3 3 4 5 Go back by one step 2019/5/22 chapter25
Backtracking A B C A B C B A 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 A 0 1 1 1 2 2 2 2 2 C 0 1 1 2 2 2 3 3 3 B 0 1 2 2 2 3 3 4 4 B 0 1 2 2 2 3 3 4 4 Time O(nm) A 0 1 2 2 3 3 3 4 5 Re-compute the cells in he blue box. 2019/5/22 chapter25
Backtracking A B C A B C B A 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 A 0 1 1 1 2 2 2 2 2 C 0 1 1 2 2 2 3 3 3 B 0 1 2 2 2 3 3 4 4 B 0 1 2 2 2 3 3 4 4 Time O(nm) A 0 1 2 2 3 3 3 4 5 Go back by one step. 2019/5/22 chapter25
Backtracking A B C A B C B A 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 A 0 1 1 1 2 2 2 2 2 C 0 1 1 2 2 2 3 3 3 B 0 1 2 2 2 3 3 4 4 B 0 1 2 2 2 3 3 4 4 Time O(nm) A 0 1 2 2 3 3 3 4 5 Re-compute the cells in the blue box. 2019/5/22 chapter25
Backtracking A B C A B C B A 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 A 0 1 1 1 2 2 2 2 2 C 0 1 1 2 2 2 3 3 3 B 0 1 2 2 2 3 3 4 4 B 0 1 2 2 2 3 3 4 4 Time O(nm) A 0 1 2 2 3 3 3 4 5 Go back by two steps. 2019/5/22 chapter25
Backtracking A B C A B C B A 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 A 0 1 1 1 2 2 2 2 2 C 0 1 1 2 2 2 3 3 3 B 0 1 2 2 2 3 3 4 4 B 0 1 2 2 2 3 3 4 4 Time O(n2) A 0 1 2 2 3 3 3 4 5 Re-compute the cells in the blue box. 2019/5/22 chapter25
Backtracking A B C A B C B A 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 A 0 1 1 1 2 2 2 2 2 C 0 1 1 2 2 2 3 3 3 B 0 1 2 2 2 3 3 4 4 A 0 1 2 2 3 3 3 4 5 Go back by two steps. 2019/5/22 chapter25
An Algorithm with O(nm) time and O(n+m) space. Divide and conquer. Main ideas Let n be the length of both sequences. K=n/2. After we get c[n,n] (the last cell), we know how to decompose the two sequences s1 and s2 and work for the LCS for s1[1…K] and s2[1…J], and s1[K+1…n] and s2[J+1…n]. The key point is to know J when c[n,m] is computed. 2019/5/22 chapter25
Example for Decomposition A B C A B C B A 0 0 0 0 0 0 0 0 0 B 0 0 1 1 1 1 1 1 1 A 0 1 1 1 2 2 2 2 2 C 0 1 1 2 2 2 3 3 3 B 0 1 2 2 2 3 3 4 4 B 0 1 2 2 2 3 3 4 4 Time O(n2) A 0 1 2 2 3 3 3 4 5 The red rectangle will be re-computed. 2019/5/22 chapter25
Re-computation of two LCS A B C A B C B A 0 0 0 0 0 B 0 0 1 1 1 A 0 1 1 1 2 0 0 0 0 0 C 0 1 1 2 2 C 0 0 1 1 1 B 0 1 2 2 2 B 0 1 1 2 2 A 0 1 2 2 3 A 0 1 1 2 3 LCS=BA+CBA. 2019/5/22 chapter25
How to know J when c[n,m] is computed? Main ideas when computing cell c[i,j], where i>=K, we have to know the configuration of s1[K], i.e., the cell c[K, J] that passes its value (may via a long way) to c[i, j]. We use an array d[i,j] to store J. The value of J will be passed to the cells while computing c[i, j]. 2019/5/22 chapter25
The array d[i, j] that stores the J values A B C A B C B A x x x x 0 0 0 0 0 B x x x x 1 0 0 0 0 A x x x x 2 2 2 2 0 C x x x x 3 2 2 2 2 B x x x x 4 3 2 2 2 B x x x x 5 3 2 2 2 A x x x x 6 6 3 2 2 c[6,8]=2 indicates where to decompose. 2019/5/22 chapter25
The divide and conquer algorithm 1. Compute c[n,n] and d[n,n] (assume |s1|=|s2|) 2. Cut s1 into s1[1…K] and s1[K+1…n] Cut s2 into s2[1…J] and s2[J+1…n]. 3. Compute the LCS L1 for s1[1…K] and s1[K+1…n] and compute the LSC L2 for s2[1…J] and s2[J+1…n]. (Recursive step) 4. Combine L1 and L2 to get LCS L1L2. Space: O(n+m). Time: O(nm). (twice of the original alg.) 2019/5/22 chapter25
Example: Time : nm(1+ i=1 log n 0.5i)2 nm S1=ABCABCBA & s2=BACBBA nm ABCA & BA; BCBA & CBBA 0.5n m AB & B; CA & A; BC & CB; BA & BA 0.25nm A & ; B & B; C & ; A & A,B & ; C & CB; B & B; A & A 0.125n m 2019/5/22 chapter25
Time complexity Level 1: compute nm matrices c[i, j] and d[i, j]. Level 2:n/2m1 matrices and n/2m2 matrices m1+m2=m. Subtotal n/2 m Level 3: n/4m1 , n/4m2 n/4m3 n/4m4 m1+m2+m3+m4=m. Subtotal n/4 m …. Level log n: 1 m1 +1 m2 +…+1 mq m1 +m2 +…+mq =m. Subtotal m. Total: nm(1+ i=1 log n 0.5i)2 nm . 2019/5/22 chapter25
More on dynamic programming algorithms 2019/5/22 chapter25
2019/5/22 chapter25
2019/5/22 chapter25
2019/5/22 chapter25
2019/5/22 chapter25
2019/5/22 chapter25
Backtracking is used to get the schedule. Time complexity O(n) if the jobs are sorted. Total time: O(n log n) including sorting. 2019/5/22 chapter25
2019/5/22 chapter25
2019/5/22 chapter25
2019/5/22 chapter25
2019/5/22 chapter25
2019/5/22 chapter25
2019/5/22 chapter25