CS146
Chris Pollett
Apr 28, 2014
LCS-LENGTH(X,Y) 01 m = X.length 02 n = Y.length 03 let b[1..m, 1 ..n] and c[0..m, 0..n] be new tables 04 for i = 1 to m //initialize first column 05 c[i, 0] = 0 06 for j = 0 to n //initialize first row 07 c[0, j] = 0 08 for i = 1 to n 09 for j = 1 to m 10 if X[i] == Y[j] 11 c[i,j] = c[i-1, j -1] + 1 12 b[i,j] = "diagonal" 13 elseif c[i-1,j] ≥ c[i, j - 1] 14 c[i,j] = c[i-1,j] 15 b[i,j] = "up" 16 else c[i,j] = c[i, j-1] 17 b[i,j] = "left" 18 return c and b
This procedure will be `Theta(mn)` because of the doubly nested for loop each step of which takes `Theta(1)` time to compute.
PRINT-LCS(b, X, i, j) 1 if i == 0 or j == 0 2 return //empty sequence is an LCS 3 if b[i,j] == "diagonal" 4 PRINT-LCS(b, X, i - 1, j - 1)//print sub-case first so forward order 5 print x[i] 6 elseif b[i,j] == "up" 7 PRINT-LCS(b, X, i - 1, j) 8 else PRINT-LCS(b, X, i, j - 1)
Which of the following statements is true?
Which of the following statements is true?