Question

Longest Common Subsequence using bottom up recursion(JAVA) static List<Character> bulcs (List<Character> cs1, List<Character> cs2) { //TODO...

Longest Common Subsequence using bottom up recursion(JAVA)

static List<Character> bulcs (List<Character> cs1, List<Character>
        cs2) {
    //TODO
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The above question can be coded as follows.

import java.util.*;
import java.lang.*;
import java.io.*;

public class BULCS {

   public static void main(String[] args) {
      
       List <Character> s1 = new ArrayList<Character>();
       List <Character> s2 = new ArrayList<Character>();

       //s1 = "aba"
       s1.add('a');
       s1.add('b');
       s1.add('a');

       //s2 = "cab"
       s2.add('c');
       s2.add('a');
       s2.add('b');

       List <Character> ans = bulcs(s1,s2);

       for(int i=0;i<ans.size();i++)
           System.out.print(ans.get(i));
       System.out.println("");
   }


   static List<Character> bulcs (List<Character> cs1, List<Character> cs2) {

       int n = cs1.size();
       int m = cs2.size();
       int[][] dp = new int[n][m];


       //dp[i][j] will store answer for string (0,i) and (0,j)
       int i,j;

       //from relation of lcs we know to calc dp[i][j]
       //we need dp[i-1][j] , dp[i][j-1] and dp[i-1][j-1]
       //so we fille matrix from top left to bottom right one row at a time
       //also we fill 0th row and 0th column as base cases

       //filling 0th row
       for(i=0;i<m;i++)
           if(cs1.get(0) == cs2.get(i))
               dp[0][i] = 1;
           else
               dp[0][i] = 0;


       //filling 0th column
       for(i=0;i<n;i++)
           if(cs1.get(i) == cs2.get(0))
               dp[i][0] = 1;
           else
               dp[i][0] = 0;


       //dp loop
       for(i=1;i<n;i++)
       {
           for(j=1;j<m;j++)
           {
               if(cs1.get(i) == cs2.get(j))
                   dp[i][j] = 1 + dp[i-1][j-1];
               else
                   dp[i][j] = Math.max(dp[i-1][j] , dp[i][j-1]);
           }
       }

       List <Character> ret = new ArrayList <Character> ();
       int ans = dp[n-1][m-1];
       int copy = ans;
       i = n-1;
       j = m-1;
       //regenerate the answer. the LCS
       while(copy > 0)
       {
           if(cs1.get(i) == cs2.get(j))
           {
               ret.add(cs1.get(i));
               copy--;
               i--;
               j--;
           }
           else
           {
               if(dp[i-1][j] > dp[i][j-1])
                   i--;
               else
                   j--;
           }
       }

       Collections.reverse(ret);
       return ret;
   }
}

In case of any doubt please comment. Happy coding :)

Add a comment
Know the answer?
Add Answer to:
Longest Common Subsequence using bottom up recursion(JAVA) static List<Character> bulcs (List<Character> cs1, List<Character> cs2) { //TODO...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT