Question
After filling the dp table using the standard dynamic
programming approach, we can reconstruct one of the Longest Common Subsequences by backtracking. Consider text1 = "ABCBDAB" and text2 = "BDCABA". The dp table is filled as follows: Â Â Â Â ""Â BÂ Â DÂ Â CÂ Â AÂ Â BÂ Â A Â Â ""Â 0Â Â 0Â Â 0Â Â 0Â Â 0Â Â 0Â Â 0 Â Â AÂ Â 0Â Â 0Â Â 0Â Â 0Â Â 1Â Â 1Â Â 1 Â Â BÂ Â 0Â Â 1Â Â 1Â Â 1Â Â 1Â Â 2Â Â 2 Â Â CÂ Â 0Â Â 1Â Â 1Â Â 2Â Â 2Â Â 2Â Â 2 Â Â BÂ Â 0Â Â 1Â Â 1Â Â 2Â Â 2Â Â 3Â Â 3 Â Â DÂ Â 0Â Â 1Â Â 2Â Â 2Â Â 2Â Â 3Â Â 3 Â Â AÂ Â 0Â Â 1Â Â 2Â Â 2Â Â 3Â Â 3Â Â 4 Â Â BÂ Â 0Â Â 1Â Â 2Â Â 2Â Â 3Â Â 4Â Â 4 Using the following Python code snippet for backtracking, what LCS string will be reconstructed? def reconstruct_lcs(text1, text2, dp): Â Â lcs_str = [] Â Â i = len(text1) Â Â j = len(text2) Â Â while i > 0 and j > 0: Â Â Â Â if text1[i - 1] == text2[j - 1]: Â Â Â Â Â Â lcs_str.append(text1[i - 1]) Â Â Â Â Â Â i -= 1 Â Â Â Â Â Â j -= 1 Â Â Â Â elif dp[i - 1][j] > dp[i][j - 1]: Â Â Â Â Â Â i -= 1 Â Â Â Â else: Â Â Â Â Â Â j -= 1 Â Â return "".join(lcs_str[::-1]) # Reverse to get correct orderSolution
Detailed Answer and Dry Run: The reconstruct_lcs function starts from the bottom-right corner of the dp table (dp[m][n]) and moves upwards and leftwards. If text1[i-1] == text2[j-1], it means these characters form part of the LCS. We append text1[i-1] to our lcs_str and move diagonally up-left (i-1, j-1). If the characters don't match, we look at dp[i-1][j] (value from above) and dp[i][j-1] (value from left). We move to the cell that contributed the maximum value to dp[i][j]. If dp[i-1][j] > dp[i][j-1], we move up (i-1, j). Otherwise, we move left (i, j-1). Let's dry run with text1 = "ABCBDAB" (m=7) and text2 = "BDCABA" (n=6). Initial: i = 7, j = 6, lcs_str = [] 1. i=7, j=6 (dp = 4)   text1[6] = 'B', text2[5] = 'A'. Mismatch.   dp[6][6] (value above) = 3   dp[7][5] (value left) = 4   Since dp[7][5] (4) is not greater than dp[6][6] (3), we move left. (Note: if they are equal, moving left or up is arbitrary, here else moves left).   i = 7, j = 5 2. i=7, j=5 (dp = 4)   text1[6] = 'B', text2[4] = 'B'. Match!   Append 'B' to lcs_str. lcs_str = ['B']   Move diagonally up-left.   i = 6, j = 4 3. i=6, j=4 (dp = 3)   text1[5] = 'A', text2[3] = 'A'. Match!   Append 'A' to lcs_str. lcs_str = ['B', 'A']   Move diagonally up-left.   i = 5, j = 3 4. i=5, j=3 (dp = 2)   text1[4] = 'D', text2[2] = 'C'. Mismatch.   dp[4][3] (value above) = 2   dp[5][2] (value left) = 2   Since dp[4][3] (2) is not greater than dp[5][2] (2), we move left.   i = 5, j = 2 5. i=5, j=2 (dp = 2)   text1[4] = 'D', text2[1] = 'D'. Match!   Append 'D' to lcs_str. lcs_str = ['B', 'A', 'D']   Move diagonally up-left.   i = 4, j = 1 6. i=4, j=1 (dp = 1)   text1[3] = 'B', text2[0] = 'B'. Match!   Append 'B' to lcs_str. lcs_str = ['B', 'A', 'D', 'B']   Move diagonally up-left.   i = 3, j = 0 7. i=3, j=0   j is now 0. The while loop condition j > 0 is false. Loop terminates. Finally, lcs_str = ['B', 'A', 'D', 'B']. We reverse it to get the correct order: ["B", "D", "A", "B"]. The reconstructed LCS is "BDAB".
Which Indian port has been designated by the Ministry of Ports, Shipping, and Waterways to function as India's first transshipment port?
Which country will be the first to introduce a tax on livestock carbon dioxide emissions from 2030?
Which government initiative is aimed at promoting financial literacy among school students in India?
 At the Senior World Wrestling Championship, which Indian had won the bronze medal ?
When is Anti-Terrorism Day celebrated annually?
Which state government has recently launched the ‘Swayam scheme’ to provide interest-free loans to empower its youth?
When was the International Agricultural Development Fund funded IFAD Integrated Livelihood Cooperation Project launched?
According to S&P Global Ratings, what is India’s updated insolvency regime group ranking? Â
Which of the following statements is/are not correct with reference to Mont Blanc:
-
It is the highest peak...
-
In Which district of Uttar Pradesh 'Temple Museum' will be established?