CSE - 208 Lab 5
CSE - 208 Lab 5
Lab Report NO # 05
Course Title: Algorithms Lab
Course Code: CSE_208 Section: 231_D2
Student Details
Name ID
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 {
return dp[n][W];
}
System.out.println("Enter items:");
int n = scanner.nextInt();
System.out.println("Enter weights");
for (int i = 0; i < n; i++) {
wt[i] = scanner.nextInt();
}
scanner.close();
}
}
LCS:
package lcs;
import java.util.Scanner;
return lcs.reverse().toString();
}
scanner.close();
}
}
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.