0% found this document useful (0 votes)
12 views7 pages

CSE - 208 Lab 5

Uploaded by

freefirevai676
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views7 pages

CSE - 208 Lab 5

Uploaded by

freefirevai676
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Green University of Bangladesh

Department of Computer Science and Engineering (CSE)


Faculty of Sciences and Engineering
Semester: (Fall, Year:2024), B.Sc. in CSE (Day)

Lab Report NO # 05
Course Title: Algorithms Lab
Course Code: CSE_208 Section: 231_D2

Lab Experiment Name: Implementation of 0-1 Knapsack and Longest


Common Subsequence (LCS).

Student Details

Name ID

1. Md Mahabub Hasan Mahin 231902056

Lab Date​ : 04 - 12 - 2024


Submission Date​ : 11 - 12 -2024
Course Teacher’s Name​ : Farjana Akter Jui

Lab Report Status


Marks: ………………………………… Signature:.....................
Comments:.............................................. Date:..............................
1. TITLE OF THE LAB REPORT EXPERIMENT
Implementation of 0-1 Knapsack and Longest Common Subsequence
(LCS).

2. OBJECTIVES
●​ Understand the basic of dynamic programming
●​ Apply dynamic programming to solve real-life optimal decision making
●​ To learn about Longest Common Sub-sequence (LCS) algorithm for determining the
length of common sub-sequences in strings.

3. PROCEDURE
Algorithm 2: LCS

4. IMPLEMENTATION

0/1 knapsack :

package knapsack;

import java.util.Scanner;
public 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) {
int[][] dp = new int[n + 1][W + 1];

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


for (int w = 0; w <= W; w++) {
if (i == 0 || w == 0)
dp[i][w] = 0;
else if (wt[i - 1] <= w)
dp[i][w] = max(val[i - 1] + dp[i - 1][w - wt[i - 1]], dp[i - 1][w]);
else
dp[i][w] = dp[i - 1][w];
}
}

return dp[n][W];
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.println("Enter items:");
int n = scanner.nextInt();

System.out.println("Enter Capacity ");


int W = scanner.nextInt();

int[] val = new int[n];


int[] wt = new int[n];

System.out.println("Enter items values");


for (int i = 0; i < n; i++) {
val[i] = scanner.nextInt();
}

System.out.println("Enter weights");
for (int i = 0; i < n; i++) {
wt[i] = scanner.nextInt();
}

System.out.println("Maximum total profit = " + knapSack(W, wt, val, n));

scanner.close();
}
}
LCS:

package lcs;

import java.util.Scanner;

public class LongestCommonSubsequence {

public static String findLCS(String s1, String s2) {


int m = s1.length();
int n = s2.length();

int[][] dp = new int[m + 1][n + 1];

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


for (int j = 1; j <= n; j++) {
if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}

StringBuilder lcs = new StringBuilder();


int i = m, j = n;
while (i > 0 && j > 0) {
if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
lcs.append(s1.charAt(i - 1));
i--;
j--;
} else if (dp[i - 1][j] > dp[i][j - 1]) {
i--;
} else {
j--;
}
}

return lcs.reverse().toString();
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter first string: ");


String s1 = scanner.nextLine();
System.out.print("Enter second string: ");
String s2 = scanner.nextLine();

String lcs = findLCS(s1, s2);


System.out.println("Longest Common Subsequence: " + lcs);

scanner.close();
}
}

5. TEST RESULT / OUTPUT

0/1 Knapsack:
LCS output:

6. DISCUSSION

The 0/1 Knapsack and Longest Common Subsequence (LCS) problems both use dynamic
programming to solve complex problems. In the knapsack problem, I’m try to get the most value
without going over a weight limit, while in LCS, you find the longest sequence that appears in
both strings. Both use a table to store results and take O(n * m) time. The main difference is that
knapsack focuses on value, while LCS looks for common sequences.

You might also like