Download presentation
Presentation is loading. Please wait.
1
Linear space LCS algorithm
Divide-and Conquer : Linear space LCS algorithm 2019/5/22 chapter25
2
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
3
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
4
Original method for CS A B C A B C B A 0 0 0 0 0 0 0 0 0
B A C B A 2019/5/22 chapter25
5
Steps for computing c[n,m], the last cell in the matrix.
A B C A B C B A B 0 A 0 Initial values C 0 A 0 2019/5/22 chapter25
6
Steps for computing c[n,m]
A B C A B C B A B A 0 C 0 B 0 2019/5/22 chapter25
7
Steps for computing c[n,m]
A B C A B C B A B A C 0 B 0 the 2nd row is no longer useful B 0 A 0 2019/5/22 chapter25
8
Steps for computing c[n,m]
A B C A B C B A B A C 0 B 0 the 2nd row is no longer useful B 0 A 0 2019/5/22 chapter25
9
Steps for computing c[n,m]
A B C A B C B A B A C B 0 the 3rd row is no longer useful B 0 A 0 2019/5/22 chapter25
10
Steps for computing c[n,m]
A B C A B C B A B A C B 0 A 0 2019/5/22 chapter25
11
Steps for computing c[n,m]
A B C A B C B A B A C B B 0 4th row is not useful A 0 2019/5/22 chapter25
12
Steps for computing c[n,m]
A B C A B C B A B A C B B 0 4th row is not useful A 0 2019/5/22 chapter25
13
Steps for computing c[n,m]
A B C A B C B A B A C B B A 0 2019/5/22 chapter25
14
Steps for computing c[n,m]
A B C A B C B A B A C B B A 0 2019/5/22 chapter25
15
Steps for computing c[n,m]
A B C A B C B A B A C B A 2019/5/22 chapter25
16
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
17
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
18
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
B A C B A 2019/5/22 chapter25
19
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
B A C B B Time O(nm) A Re-compute the cells in the blue box. 2019/5/22 chapter25
20
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
B A C B B Time O(nm) A Go back by one step. 2019/5/22 chapter25
21
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
B A C B B Time O(nm) A Re-compute the values in the blue box. 2019/5/22 chapter25
22
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
B A C B B Time O(nm) A Go back by one step 2019/5/22 chapter25
23
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
B A C B B Time O(nm) A Re-compute the cells in he blue box. 2019/5/22 chapter25
24
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
B A C B B Time O(nm) A Go back by one step. 2019/5/22 chapter25
25
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
B A C B B Time O(nm) A Re-compute the cells in the blue box. 2019/5/22 chapter25
26
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
B A C B B Time O(nm) A Go back by two steps. 2019/5/22 chapter25
27
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
B A C B B Time O(n2) A Re-compute the cells in the blue box. 2019/5/22 chapter25
28
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
B A C B A Go back by two steps. 2019/5/22 chapter25
29
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
30
Example for Decomposition
A B C A B C B A B A C B B Time O(n2) A The red rectangle will be re-computed. 2019/5/22 chapter25
31
Re-computation of two LCS
A B C A B C B A B A C C B B A A LCS=BA+CBA. 2019/5/22 chapter25
32
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
33
The array d[i, j] that stores the J values
A B C A B C B A x x x x B x x x x A x x x x C x x x x B x x x x B x x x x A x x x x c[6,8]=2 indicates where to decompose. 2019/5/22 chapter25
34
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
35
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
36
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
37
More on dynamic programming algorithms
2019/5/22 chapter25
38
2019/5/22 chapter25
39
2019/5/22 chapter25
40
2019/5/22 chapter25
41
2019/5/22 chapter25
42
2019/5/22 chapter25
43
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
44
2019/5/22 chapter25
45
2019/5/22 chapter25
46
2019/5/22 chapter25
47
2019/5/22 chapter25
48
2019/5/22 chapter25
49
2019/5/22 chapter25
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.