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".
Muta marriage is recognised by
As per the Companies Act the Tribunal may at any time after passing of a winding up order, pass an order requiring any contributory for the time being...
Banking Ombudsman is a senior official appointed by the RBI to redress customer complaint specified under which clause of the Banking Ombudsman Scheme 2...
Which of the following is not mentioned in Directive Principles of State Policy under The Constitution of India?Â
Which provision of Consumer protection act 2019 talks about rights of consumers?
Which of the folbwing section of Cr. P.C is related with the withdrawal of prosecution ?
Which of the following is not correct? As per At. 51 of the Indian Constitution, State shall endeavour to –
What is the term of office for the President of India?
What provision must the system provider make regarding dispute resolution between system participants, as per the Payment and Settlement Systems Act?
Which of the following criteria defines a "defunct co-operative bank" according to the Deposit Insurance and Credit Guarantee Corporation Act?