VINAYAK GARG
2200290110196
CSIT-5C2
DAA EXP-10
Write a program to implement 0/1 Knapsack problem
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Function to solve the 0/1 Knapsack problem using Dynamic
Programming
int knapsack(int W, vector<int>& weights, vector<int>& values, int n)
{
// Create a 2D DP table to store the maximum value that can be
obtained for each weight
vector<vector<int>> dp(n+1, vector<int>(W+1, 0));
// Build the dp table
for (int i = 1; i <= n; i++) {
for (int w = 1; w <= W; w++) {
// If the current item can be included (i.e., its weight is less
than or equal to the capacity w)
if (weights[i-1] <= w) {
// Either take the item or skip it, whichever gives a higher
value
dp[i][w] = max(dp[i-1][w], dp[i-1][w-weights[i-1]] + values[i-
1]);
} else {
// If the current item cannot be included, carry forward the
value from the previous item
dp[i][w] = dp[i-1][w];
}
}
}
// The value at dp[n][W] will be the maximum value that can be
obtained
return dp[n][W];
}
int main() {
int n, W;
// Number of items
cout << "Enter the number of items: ";
cin >> n;
// Capacity of knapsack
cout << "Enter the capacity of the knapsack: ";
cin >> W;
vector<int> values(n), weights(n);
// Enter the values of the items
cout << "Enter the values of the items: ";
for (int i = 0; i < n; i++) {
cin >> values[i];
}
// Enter the weights of the items
cout << "Enter the weights of the items: ";
for (int i = 0; i < n; i++) {
cin >> weights[i];
}
// Solve the 0/1 Knapsack problem
int result = knapsack(W, weights, values, n);
cout << "Maximum value that can be obtained: " << result << endl;
return 0;
}
OUTPUT:-
Write a program to implement Longest common subsequence
problem.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Function to find the length of the Longest Common Subsequence
int lcs(string X, string Y) {
int m = X.length();
int n = Y.length();
// Create a 2D table to store the length of LCS at different positions
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
// Build the dp table
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
// If characters match, increment the length of the LCS
if (X[i - 1] == Y[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
// Otherwise, take the maximum length from the previous
subsequences
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
// The length of the LCS will be in dp[m][n]
return dp[m][n];
}
// Function to print the Longest Common Subsequence
void printLCS(string X, string Y, vector<vector<int>>& dp) {
int i = X.length();
int j = Y.length();
string lcs = "";
// Trace back through the dp table to construct the LCS
while (i > 0 && j > 0) {
if (X[i - 1] == Y[j - 1]) {
lcs = X[i - 1] + lcs;
i--;
j--;
} else if (dp[i - 1][j] >= dp[i][j - 1]) {
i--;
} else {
j--;
}
}
cout << "Longest Common Subsequence: " << lcs << endl;
}
int main() {
string X, Y;
// Input two strings
cout << "Enter the first string: ";
cin >> X;
cout << "Enter the second string: ";
cin >> Y;
int m = X.length();
int n = Y.length();
// Create a 2D DP table to store the LCS lengths
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
// Call LCS function to fill the dp table
lcs(X, Y);
// Output the length of the LCS
cout << "Length of Longest Common Subsequence: " << dp[m][n]
<< endl;
// Output the LCS itself
printLCS(X, Y, dp);
return 0;
}