public static int solveLCS(String x, String y) { int i, j; /* =============================================== Initialize the base cases =============================================== */ for (i = 0; i < x.length()+1; i++) L[i][0] = 0; // y = "" ===> LCS = 0 for (j = 0; j < y.length()+1; j++) L[0][j] = 0; // x = "" ===> LCS = 0 /* ===================================================== Bottom-up (smaller to larger) computation of L[][j] ===================================================== */ for (i = 1; i < x.length()+1; i++) { for (j = 1; j < y.length()+1; j++) { if ( x.charAt(i-1) == y.charAt(j-1) ) { L[i][j] = L[i-1][j-1] + 1; } else { L[i][j] = max( L[i-1][j] , L[i][j-1] ); } } } return L[x.length()][y.length()]; } |
The method will return the length of the LCS of the input strings x and y.
|
Example:
|
Facts:
|
public static int solveLCS(String x, String y) { int i, j; /* =============================================== Initialize the base cases =============================================== */ for (i = 0; i < x.length()+1; i++) L[i][0] = 0; // y = "" ===> LCS = 0 for (j = 0; j < y.length()+1; j++) L[0][j] = 0; // x = "" ===> LCS = 0 /* ===================================================== Bottom-up (smaller to larger) computation of L[][j] ===================================================== */ for (i = 1; i < x.length()+1; i++) // Row i { for (j = 1; j < y.length()+1; j++) { if ( x.charAt(i-1) == y.charAt(j-1) ) { L[i][j] = L[i-1][j-1] + 1; // Use values in row i-1 } else { L[i][j] = max( L[i-1][j] , L[i][j-1] ); // Use values in row i-1 } } } return L[x.length()][y.length()]; } |
|
|
public static int solveLCS(String x, String y) { int i, j; /* =============================================== Initialize the base cases =============================================== */ for (i = 0; i < x.length()+1; i++) // ***** Problem ! ***** L[i][0] = 0; // We cannot initialize ALL rows at once ! for (j = 0; j < y.length()+1; j++) L[0][j] = 0; // x = "" ===> LCS = 0 /* ===================================================== Bottom-up (smaller to larger) computation of L[][j] ===================================================== */ for (i = 1; i < x.length()+1; i++) { for (j = 1; j < y.length()+1; j++) { if ( x.charAt(i-1) == y.charAt(j-1) ) { L[i][j] = L[i-1][j-1] + 1; } else { L[i][j] = max( L[i-1][j] , L[i][j-1] ); } } } return L[x.length()][y.length()]; } |
public static int solveLCS(String x, String y) { int i, j; /* =============================================== Initialize the base cases =============================================== for (j = 0; j < y.length()+1; j++) L[0][j] = 0; // x = "" ===> LCS = 0 /* ===================================================== Bottom-up (smaller to larger) computation of L[][j] ===================================================== */ for (i = 1; i < x.length()+1; i++) { L[i][0] = 0; // <<<--- moved HERE first ****** // Because we can't initialize ALL row at once // The 0-th element in row i is now // initialized when row i is USED for (j = 1; j < y.length()+1; j++) { if ( x.charAt(i-1) == y.charAt(j-1) ) { L[i][j] = L[i-1][j-1] + 1; } else { L[i][j] = max( L[i-1][j] , L[i][j-1] ); } } } return L[x.length()][y.length()]; } |
L[ i-1 ] [j] -----> K[ 0 ] [j] L[ i ] [j] -----> K[ 1 ] [j] L[ i ] [j-1] -----> K[ 1 ] [j-1] |
in the above modified algorithm, we will obtain a linear space algorithm for the LCS problem:
public static int solveLCS(String x, String y) { int i, j; /* =============================================== Initialize the base cases =============================================== for (j = 0; j < y.length()+1; j++) K[0][j] = 0; // x = "" ===> LCS = 0 for (i = 1; i < x.length()+1; i++) { K[1][0] = 0; // Piecemeal initialization of L[i][0] for (j = 1; j < y.length()+1; j++) { if ( x.charAt(i-1) == y.charAt(j-1) ) { K[1][j] = K[0][j-1] + 1; } else { K[1][j] = max( K[0][j] , K[1][j-1] ); } } /* ===================================================== Recycle phase: copy row K[1][...] to row K[0][...] ===================================================== */ for ( j = 0; j < y.length()+1; j++) K[0][j] = K[1][j]; } // The value of LCS is in K[1][y.length()] return K[1][y.length()]; } |
How to run the program:
|
|