Question

In: Computer Science

Write the pseudo code for longest common subsequence algorithm in Java. Alg lCS( X , n,...

Write the pseudo code for longest common subsequence algorithm in Java.

Alg lCS( X , n, Y, m)

Input: String X of length n, String Y of length m

Output: return the length of the longest common subsequence between X and Y

Solutions

Expert Solution

Answer:

class Main {

/*return the length of the longest common subsequence between X and Y */

int lcs(char[] X, int n, char[] Y, int m)

{

if (m == 0 || n == 0)

return 0;

if (X[n - 1] == Y[m - 1])

return 1 + lcs(X, n-1, Y, m - 1);

else

return max(lcs(X, n, Y, m - 1), lcs(X, n - 1, Y, m));

}

/* Utility function to get max of 2 integers */

int max(int a, int b)

{

return (a > b) ? a : b;

}

public static void main(String[] args)

{

Main lcs = new Main();

String s1 = "AGGTAB";

String s2 = "GXTXAYB";

char[] X = s1.toCharArray();

char[] Y = s2.toCharArray();

int n = X.length;

int m = Y.length;

System.out.println("Length of LCS is"+ " " + lcs.lcs(X, n, Y, m));

}

}

Output:

Please give thumbsup, if you like it. Thanks.


Related Solutions

Prove Longest common subsequence algorithm class finds the optimal solution
Prove Longest common subsequence algorithm class finds the optimal solution
Develop an algorithm for INSERTION SORT. Give the pseudo-code version. Convert your pseudo-code into a Java...
Develop an algorithm for INSERTION SORT. Give the pseudo-code version. Convert your pseudo-code into a Java program.
Write a pseudo code for an O (n7log3n) algorithm. Please write in C++.
Write a pseudo code for an O (n7log3n) algorithm. Please write in C++.
1. Write an algorithm to calculate the Matrix multiplication (or write with pseudo code) 2. Write...
1. Write an algorithm to calculate the Matrix multiplication (or write with pseudo code) 2. Write an algorithm to calculate the recursive Matrix multiplication (or write with pseudo code) 3. Find the time complexity of your pseudo code and analyze the differences
Write an algorithm in pseudo code to find one element and delete it in a doubly...
Write an algorithm in pseudo code to find one element and delete it in a doubly linked list. Your algorithm will print the original list, request the user to put in an element to be deleted, then print the final list after the deletion is done. If the element doesn’t exist in the list, print "XXX is not in the list" where "XXX" should be the one you received from the user. Use the following as your test cases to...
Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of...
Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of integers. Assume that the List type has members: int List.length returns the length of the list. void List.push(T n) pushes an element n to the front of the list T List.pop() pops an element from the front of the list. List$$ List$$.concat(List$$ other) returns the concatenation of this list with other. Explain in plain English the reasoning behind your algorithm. Power Lists should be...
Dynamic Programming Question. Make the table for finding a LCS (Longest Common Subsequence) of the strings...
Dynamic Programming Question. Make the table for finding a LCS (Longest Common Subsequence) of the strings SLWOVNNDK and ALWGQVNBKB. You need to Show the traceback for the LCS.
In pseudo-code, design an algorithm for finding baking a cake.
In pseudo-code, design an algorithm for finding baking a cake.
Write pseudo code for the following parts of class BinarySearchTree: a) find(x) b) contains(x) c) add(x)...
Write pseudo code for the following parts of class BinarySearchTree: a) find(x) b) contains(x) c) add(x) d) remove(x) e) splice(x) f) min() g) max() h) pred() i) succ() j) floor() k) ceil()
Scheme Programming - Racket R5RS Longest Non-Decreasing Subsequence You will write two Scheme functions that compute...
Scheme Programming - Racket R5RS Longest Non-Decreasing Subsequence You will write two Scheme functions that compute a longest non-decreasing subsequence from a list of numbers. For example, if you type > (lis '(1 2 3 2 4 1 2)) you might get (1 2 3 4) Note that there may be more than one longest non-decreasing subsequence. In the above example, your program might also find (1 2 2 4) or (1 2 2 2). You should concentrate first on...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT