Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Adapdf

Download as pdf or txt
Download as pdf or txt
You are on page 1of 43

NAME: Munavvar Popatiya

ENROLLMENT NO: 200130116003


BRANCH: INFORMATION TECHNOLOGY
BATCH: A1
INDEX

Sr no Practical Date Page Marks

1 Implementation and time analysis of sorting algorithm


.bubble sort, selection sort , insertion sort , merge and
Quick sort.
2 Implementation and time analysis of linear and binary
Search algorithm.

3 Implementation of max-heap sort algorithm.

4 Implementation and time analysis of factorial program


using iteractive and recursive method.

5 Implementation of knapsack problem using dynamic


Programming.

6 Implementation of chain matrix multiplication using


Dynamic programming .

7 Implementation of making change problem using


dynamic programming.

8 Implementation of a knapsack problem using greedy


Algorithm.

9 Implementation of graph and searching (DFS AND BFS)

10 Implementation of prim’s algorithm.


11 Implementation Kruskal’s algorithm.

12 Implementation of LCS Problem.

Certificate Of Completion

This is to certify that Munavvar Popatiya having


enrolment number 200130116003of batch _A1__ of
INFORMATION TECHNOLOGY branch at GEC

GANDHINAGAR has completed practicals in


ADA(3150703).

SIGN : DATE :
Practical:1
Aim: Sorting algorithms. Selection sort, Insertion sort, Bubble sort, Merge sort and Quick sor

1.Selection Sort

class SelectionSort
{
void sort(int arr[])
{
int n = arr.length;

for (int i = 0; i < n-1; i++)


{
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;

// Swap the found minimum element with the first


// element
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}

void printArray(int arr[])


{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}

public static void main(String args[])


{
SelectionSort ob = new SelectionSort();
int arr[] = {52,87,12,71,25};
ob.sort(arr);
System.out.println("Munavvar Popatiya");
System.out.println("Sorted array");
ob.printArray(arr);
}
}
Output:

2.Insertion Sort
public class InsertionSort {
void sort(int arr[])
{
int n = arr.length;
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
static void printArray(int arr[])
{
int n = arr.length;
System.out.println("munavvar popatiya");
System.out.println("sorted array ");
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");

System.out.println();
}

public static void main(String args[])


{
int arr[] = { 12, 11, 13, 5, 6 };

InsertionSort ob = new InsertionSort();


ob.sort(arr);

printArray(arr);
}
};
Output
3.Bubble Sort
class BubbleSort {
void bubbleSort(int arr[])
{
int n = arr.length;
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1]) {
// swap arr[j+1] and arr[j]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}

void printArray(int arr[])


{
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void main(String args[])
{
BubbleSort ob = new BubbleSort();
int arr[] = {21,18,2,32,1 };
ob.bubbleSort(arr);
System.out.println("Munavvar Popatiya");
System.out.println("Sorted array ");
ob.printArray(arr);
}
}
4.Merg Sort
class MergeSort {
void merge(int arr[], int l, int m, int r)
{
int n1 = m - l + 1;
int n2 = r - m;

int L[] = new int[n1];


int R[] = new int[n2];

for (int i = 0; i < n1; ++i)


L[i] = arr[l + i];
for (int j = 0; j < n2; ++j)
R[j] = arr[m + 1 + j];

int i = 0, j = 0;
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}

while (i < n1) {


arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
void sort(int arr[], int l, int r)
{
if (l < r) {
int m = l + (r - l) / 2;
sort(arr, l, m);
sort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
static void printArray(int arr[])
{
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void main(String args[])
{
int arr[] = { 21, 14, 51, 35, 76, 7 };

System.out.println("Munavvar Popatiya");
System.out.println("Given Array");
printArray(arr);

MergeSort ob = new MergeSort();


ob.sort(arr, 0, arr.length - 1);
System.out.println("\sorted array");
printArray(arr);
}
}
OUTPUT

5.Quick Sort
import java.io.*;
class Quicksort {

static void swap(int[] arr, int i, int j)


{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
static int partition(int[] arr, int low, int high)
{
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++) {

if (arr[j] < pivot) {


i++;
swap(arr, i, j);
}
}
swap(arr, i + 1, high);
return (i + 1);
}

static void quickSort(int[] arr, int low, int high)


{
if (low < high) {
int pi = partition(arr, low, high);

quickSort(arr, low, pi - 1);


quickSort(arr, pi + 1, high);
}
}
static void printArray(int[] arr, int size)
{
for (int i = 0; i < size; i++)
System.out.print(arr[i] + " ");

System.out.println();
}
public static void main(String[] args)
{
int[] arr = { 40, 27, 38, 79, 21, 51 };
int n = arr.length;

quickSort(arr, 0, n - 1);
System.out.println("Munavvar Popatiya");
System.out.println("Quick Sorted array: ");
printArray(arr, n);
}
}
OUTPUT:
Practical:2
Aim: Searching Algorithm: linear and binary search.
1.linera Search:
public class linear {
public static int search(int arr[], int x)
{
int N = arr.length;
for (int i = 0; i < N; i++) {
if (arr[i] == x)
return i;
}
return -1;
}
public static void main(String args[])
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
System.out.println("Munavvar Popatiya");
System.out.println("Searched Element " + x);
int result = search(arr, x);
if (result == -1)
System.out.print(
"Element is not present in array");
else
System.out.print("Element is present at index "
+ result);
}
}
OUTPUT:

2.Binary Search

class BinarySearch{
public static void binarySearch(int arr[], int first, int
last, int key){
System.out.println("Munavvar Popatiya");
System.out.println("Element Searched " +key);
int mid = (first + last)/2;
while( first <= last ){
if ( arr[mid] < key ){
first = mid + 1;
}else if ( arr[mid] == key ){
System.out.println("Element is found at index:
" + mid);
break;
}else{
last = mid - 1;
}
mid = (first + last)/2;
}
if ( first > last ){
System.out.println("Element is not found!");
}
}
public static void main(String args[]){
int arr[] = {22,13,47,51};
int key = 47;
int last=arr.length-1;
binarySearch(arr,0,last,key);
}
}

OUTPUT:
Practical 3: implemention of max-heap sort algorithm .

Code:
public class HeapSort {

public void sort(int arr[]) {


int n = arr.length;

for (int i = n / 2 - 1; i >= 0; i--) {


heapify(arr, n, i);
}

for (int i = n - 1; i >= 0; i--) {


int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;

heapify(arr, i, 0);
}
}

void heapify(int arr[], int n, int i) {


int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;

if (l < n && arr[l] > arr[largest])


largest = l;

if (r < n && arr[r] > arr[largest])


largest = r;

if (largest != i) {
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;

heapify(arr, n, largest);
}
}

static void printArray(int arr[]) {


int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void main(String args[]) {
int arr[] = { 41, 29, 22, 3, 62, 10
};

HeapSort hs = new HeapSort();


hs.sort(arr);

System.out.println("Sorted array is");


printArray(arr);}}
OUTPUT:
Practical:4
Aim: Find the Factorial of given number (iterative and recursive

1.Iterative
class Factorial
{
public static long factorial(int n)
{
long fact = 1;

for (int i = 1; i <= n; i++) {


fact = fact * i;
}

return fact;
}

public static void main(String[] args)


{
int n = 5;
System.out.println("Munavvar Popatiya");
System.out.println("The Factorial of " + n + " is " +
factorial(n));
}
}
OUTPUT:

2.recursive
class Recursive {
static int factorial(int n)
{
int res = 1, i;
for (i = 2; i <= n; i++)
res *= i;
return res;
}
public static void main(String[] args)
{
int num = 6;
System.out.println("Munavvar Popatiya");
System.out.println(
"Factorial of " + num
+ " is " + factorial(5));
}
}
OUTPUT
Practical:5
Aim: Knapsack problem using dynamic programming

class Knapsack {

static int max(int a, int b)


{
return (a > b) ? a : b;
}

static int knapSack(int W, int wt[], int val[], int n)


{
if (n == 0 || W == 0)
return 0;
if (wt[n - 1] > W)
return knapSack(W, wt, val, n - 1);
else
return max(val[n - 1]
+ knapSack(W - wt[n - 1], wt,
val, n - 1),
knapSack(W, wt, val, n - 1));
}
public static void main(String args[])
{
int val[] = new int[] { 60, 100, 120 };
int wt[] = new int[] { 10, 20, 30 };
int W = 50;
int n = val.length;
System.out.println("Munavvar Popatiya");
System.out.println(knapSack(W, wt, val, n));
}
}
OUTPUT:
Practical:6
Aim: Chain matrix multiplication using dynamic programming
class MatrixChainMultiplication {
static int MatrixChainOrder(int p[], int i, int j)
{
if (i == j)
return 0;

int min = Integer.MAX_VALUE;


for (int k = i; k < j; k++) {
int count = MatrixChainOrder(p, i, k)
+ MatrixChainOrder(p, k + 1, j)
+ p[i - 1] * p[k] * p[j];

if (count < min)


min = count;
}
return min;
}

public static void main(String args[])


{
int arr[] = new int[] { 4, 1, 3, 4, 3 };
int N = arr.length;
System.out.println("Munavvar Popatiya");
System.out.println(
"Minimum number of multiplications is "
+ MatrixChainOrder(arr, 1, N - 1));
}
}
OUTPUT:
Practical:7
Aim: Making a change problem using dynamic programming
import java.util.Arrays;

class CoinChange {
static long countWays(int coins[], int n, int sum)
{

long[] table = new long[sum + 1];

Arrays.fill(table, 0);

table[0] = 1;

for (int i = 0; i < n; i++)


for (int j = coins[i]; j <= sum; j++)
table[j] += table[j - coins[i]];

return table[sum];
}

public static void main(String args[])


{
int coins[] = { 4, 2, 3 };
int n = coins.length;
int sum = 4;
System.out.println("Munavvar Popatiya");
System.out.println(countWays(coins, n, sum));
}
}
OUTPUT:
Practical 8: Implementation of a knapsack problem using greedy
import java.io.*;
import java.util.Arrays;
import java.util.Comparator;

class FractionalKnapSack {
private static double getMaxValue(ItemValue[] arr,
int capacity)
{
Arrays.sort(arr, new Comparator<ItemValue>() {
@Override
public int compare(ItemValue item1,
ItemValue item2)
{
double cpr1
= new Double((double)item1.value
/ (double)item1.weight);
double cpr2
= new Double((double)item2.value
/ (double)item2.weight);

if (cpr1 < cpr2)


return 1;
else
return -1;
}
});

double totalValue = 0d;

for (ItemValue i : arr) {

int curWt = (int)i.weight;


int curVal = (int)i.value;

if (capacity - curWt >= 0) {

// this weight can be picked while


capacity = capacity - curWt;
totalValue += curVal;
}
else {
double fraction
= ((double)capacity / (double)curWt);
totalValue += (curVal * fraction);
capacity
= (int)(capacity - (curWt *
fraction));
break;
}
}

return totalValue;
}
static class ItemValue {

int value, weight;


public ItemValue(int val, int wt)
{
this.weight = wt;
this.value = val;
}
}
public static void main(String[] args)
{

ItemValue[] arr = { new ItemValue(60, 10),


new ItemValue(12, 20),
new ItemValue(10, 30) };

int capacity = 50;

double maxValue = getMaxValue(arr, capacity);

// Function call
System.out.println(maxValue);
}
}
OUTPUT:
Practical 9: Implementation of graph and searching (DFS AND BFS)
Code for BFS :
import java.util.*;
class Graph<T> {
private Map<T, List<T> > map = new HashMap<>();
// This function adds a new vertex to the graph
public void addVertex(T s)
{
map.put(s, new LinkedList<T>());
}

public void addEdge(T source,


T destination,
boolean bidirectional)
{
if (!map.containsKey(source))
addVertex(source);
if (!map.containsKey(destination))
addVertex(destination);
map.get(source).add(destination);
if (bidirectional == true) {
map.get(destination).add(source);
}
}
public void getVertexCount()
{
System.out.println("The graph has "
+ map.keySet().size()
+ " vertex");
}
public void getEdgesCount(boolean bidirection)
{
int count = 0;
for (T v : map.keySet()) {
count += map.get(v).size();
}
if (bidirection == true) {
count = count / 2;
}
System.out.println("The graph has "
+ count
+ " edges.");
}
public void hasVertex(T s)
{
if (map.containsKey(s)) {
System.out.println("The graph contains "
+ s + " as a vertex.");
}
else {
System.out.println("The graph does not contain "
+ s + " as a vertex.");
}
}
public void hasEdge(T s, T d)
{
if (map.get(s).contains(d)) {
System.out.println("The graph has an edge between "
+ s + " and " + d + ".");
}
else {
System.out.println("The graph has no edge between "
+ s + " and " + d + ".");
}
}
@Override
public String toString()
{
StringBuilder builder = new StringBuilder();
for (T v : map.keySet()) {
builder.append(v.toString() + ": ");
for (T w : map.get(v)) {
builder.append(w.toString() + " ");

}
builder.append("\n");
}
return (builder.toString());
}
}
class Graph1 {
public static void main(String args[])
{
System.out.println("Munavvar Popatiya");

// Object of graph is created.


Graph<Integer> g = new Graph<Integer>();
g.addEdge(0, 1, true);
g.addEdge(0, 4, true);
g.addEdge(1, 2, true);
g.addEdge(1, 3, true);
g.addEdge(1, 4, true);
g.addEdge(2, 3, true);
g.addEdge(3, 4, true);
// Printing the graph
System.out.println("Graph:\n"
+ g.toString());
// Gives the no of vertices in the graph.
g.getVertexCount();
// Gives the no of edges in the graph.
g.getEdgesCount(true);
// Tells whether the edge is present or not.
g.hasEdge(3, 4);
// Tells whether vertex is present or not
g.hasVertex(5);
}
}
OUTPUT:
2) Searching :

BFS (Breadth First Search)


import java.io.*;
import java.util.*;
class Graph
{
private int V; // No. of vertices
private LinkedList<Integer> adj[]; //Adjacency Lists
Graph(int v)
{
V = v;
adj = new LinkedList[v];
for (int i=0; i<v; ++i)
adj[i] = new LinkedList();
}
void addEdge(int v,int w)
{
adj[v].add(w);
}
void BFS(int s)
{
boolean visited[] = new boolean[V];
LinkedList<Integer> queue = new LinkedList<Integer>();
visited[s]=true;
queue.add(s);
while (queue.size() != 0)
{
s = queue.poll();
System.out.print(s+" ");
Iterator<Integer> i = adj[s].listIterator();
while (i.hasNext())
{
int n = i.next();
if (!visited[n])
{
visited[n] = true;
queue.add(n);
}
}
}
}
public static void main(String args[])
{
System.out.println("Munavvar Popatiya");
Graph g = new Graph(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
System.out.println("Following is Breadth First
Traversal "+
"(starting from vertex 2)");
g.BFS(2);
}
}

OUTPUT:
DFS (Depth First Search) :

CODE:

import java.util.*;
class graphDfs {
public static final graphDfs G = new graphDfs(4);
private int V;
private LinkedList<Integer> adj[];
// Constructor
@SuppressWarnings("unchecked")
graphDfs(int v)
{
V = v;
adj = new LinkedList[v];
for (int i = 0; i < v; ++i)
adj[i] = new LinkedList();
}
void addEdge(int v, int w)
{
adj[v].add(w); // Add w to v's list.
}
void DFSUtil(int v, boolean visited[])
{
visited[v] = true;
System.out.print(v + " ");
Iterator<Integer> i = adj[v].listIterator();
while (i.hasNext()) {
int n = i.next();
if (!visited[n])
DFSUtil(n, visited);
}
}
void DFS(int v)
{
boolean visited[] = new boolean[V];
DFSUtil(v, visited);
}
public static void main(String args[])
{
System.out.println("Munavvar Popatiya");
graphDfs G = new graphDfs(4);
G.addEdge(0, 1);
G.addEdge(0, 2);
G.addEdge(1, 2);
G.addEdge(2, 0);
G.addEdge(2, 3);
G.addEdge(3, 3);
System.out.println(
"Following is Depth First Traversal "
+ "(starting from vertex 1)");
G.DFS(1);}}
OUTPUT
Practical 10: Implementation of prim’s algorithm
CODE:
import java.io.*;
import java.lang.*;
import java.util.*;
class MST {
// Number of vertices in the graph
private static final int V = 5;
int minKey(int key[], Boolean mstSet[])
{
int min = Integer.MAX_VALUE, min_index = -1;
for (int v = 0; v < V; v++)
if (mstSet[v] == false && key[v] < min) {
min = key[v];
min_index = v;
}
return min_index;
}
void printMST(int parent[], int graph[][])
{
System.out.println("Edge \tWeight");
for (int i = 1; i < V; i++)
System.out.println(parent[i] + " - " + i + "\t"
+ graph[i][parent[i]]);
}
void primMST(int graph[][])
{

int parent[] = new int[V];


int key[] = new int[V];
// To represent set of vertices included in MST
Boolean mstSet[] = new Boolean[V];
for (int i = 0; i < V; i++) {
key[i] = Integer.MAX_VALUE;
mstSet[i] = false;
}
key[0] = 0;
parent[0] = -1;
for (int count = 0; count < V - 1; count++) {
int u = minKey(key, mstSet);
// Add the picked vertex to the MST Set
mstSet[u] = true
for (int v = 0; v < V; v++)

if (graph[u][v] != 0 && mstSet[v] == false


&& graph[u][v] < key[v]) {
parent[v] = u;
key[v] = graph[u][v];
}
}

printMST(parent, graph);
}

public static void main(String[] args)


{
System.out.println("Munavvar Popatiya");
MST t = new MST();
int graph[][] = new int[][] { { 0, 4, 0, 6, 0 },
{ 9, 0, 4, 8, 5 },
{ 0, 7, 0, 0, 7 },
{ 1, 8, 4, 0, 9 },
{ 0, 5, 5, 9, 0 } };
t.primMST(graph);
}
}
OUTPUT:
Practical 11: Implementation Kruskal’s algorithm
CODE:
import java.io.*;
import java.lang.*;
import java.util.*;
class KruskalAlgorithm {

class Edge implements Comparable<Edge> {


int src, dest, weight;

public int compareTo(Edge compareEdge)


{
return this.weight - compareEdge.weight;
}
};
class subset {
int parent, rank;
};
int V, E;
Edge edge[]; // collection of all edges
KruskalAlgorithm(int v, int e)
{
V = v;
E = e;
edge = new Edge[E];
for (int i = 0; i < e; ++i)
edge[i] = new Edge();
}
int find(subset subsets[], int i)
{
if (subsets[i].parent != i)

subsets[i].parent
= find(subsets, subsets[i].parent);
return subsets[i].parent;
}
void Union(subset subsets[], int x, int y)
{
int xroot = find(subsets, x);
int yroot = find(subsets, y);
if (subsets[xroot].rank < subsets[yroot].rank)
subsets[xroot].parent = yroot;
else if (subsets[xroot].rank > subsets[yroot].rank)
subsets[yroot].parent = xroot;
else {
subsets[yroot].parent = xroot;
subsets[xroot].rank++;
}
void KruskalMST()
{
Edge result[] = new Edge[V];
int e = 0;
// An index variable, used for sorted edges
int i = 0;
for (i = 0; i < V; ++i)
result[i] = new Edge();
Arrays.sort(edge);
// Allocate memory for creating V subsets
subset subsets[] = new subset[V];
for (i = 0; i < V; ++i)
subsets[i] = new subset();
// Create V subsets with single elements

for (int v = 0; v < V; ++v) {


subsets[v].parent = v;
subsets[v].rank = 0;
}
i = 0;
while (e < V - 1) {
Edge next_edge = edge[i++];
int x = find(subsets, next_edge.src);
int y = find(subsets, next_edge.dest);
if (x != y) {
result[e++] = next_edge;
Union(subsets, x, y);
}
}
System.out.println("Following are the edges in "
+ "the constructed MST");
int minimumCost = 0;
for (i = 0; i < e; ++i) {
System.out.println(result[i].src + " -- "
+ result[i].dest
+ " == " + result[i].weight);
minimumCost += result[i].weight;
}
System.out.println("Minimum Cost Spanning Tree "
+ minimumCost);
}
// Driver's Code
public static void main(String[] args)
{
System.out.println("Munavvar Popatiya");
int V = 4; // Number of vertices in graph
int E = 5; // Number of edges in graph
KruskalAlgorithm graph = new KruskalAlgorithm(V, E);
// add edge 0-1
graph.edge[0].src = 0;
graph.edge[0].dest = 1;
graph.edge[0].weight = 14;
// add edge 0-2
graph.edge[1].src = 0;
graph.edge[1].dest = 2;
graph.edge[1].weight = 6;
// add edge 0-3
graph.edge[2].src = 0;
graph.edge[2].dest = 3;
graph.edge[2].weight = 15;
// add edge 1-3
graph.edge[3].src = 1;
graph.edge[3].dest = 3;
graph.edge[3].weight = 15;
// add edge 2-3
graph.edge[4].src = 2;
graph.edge[4].dest = 3;
graph.edge[4].weight = 5;
graph.KruskalMST();
}
}
OUTPUT:
Practical 12 : Implementation of LCS Problem
Code:
import java.util.*;
import java.io.*;
import java.util.Scanner;

class LCSExample1 {
public static String findLengthOfLCS(String str1, String
str2, int p, int q) {
int[][] tableForLCS = new int[p + 1][q + 1];
for (int i = 0; i <= p; i++) {
for (int j = 0; j <= q; j++) {
if (i == 0 || j == 0)
tableForLCS[i][j] = 0;
else if (str1.charAt(i - 1) == str2.charAt(j -
1))
tableForLCS[i][j] = tableForLCS[i - 1][j -
1] + 1;
else
tableForLCS[i][j] = Math.max(tableForLCS[i
- 1][j], tableForLCS[i][j - 1]); }
}
int index = tableForLCS[p][q];
int temp = index;
char[] longestCommonSubsequence = new char[index + 1];
longestCommonSubsequence[index] = '\0';
int i = p, j = q;
String lcs ="";
while (i > 0 && j > 0) {
if (str1.charAt(i - 1) == str2.charAt(j - 1)) {

longestCommonSubsequence[index - 1] =
str1.charAt(i - 1);
i--;
j--;
index--;
}
else if (tableForLCS[i - 1][j] > tableForLCS[i][j
- 1])
i--;
else
j--;
}

for (int k = 0; k <= temp; k++)


lcs = lcs + longestCommonSubsequence[k];

return lcs;
}
public static void main(String[] args) {
String str1, str2, LCS;
str1 = "KLMNO";
str2 = "LMNOP";
int p = str1.length();
int q = str2.length();
LCS = findLengthOfLCS(str1, str2, p, q);
System.out.print("Sequence1: " + str1 + "\nSequence2:
" + str2);
System.out.println("\nLCS: "+LCS);

}
}

OUTPUT:

You might also like