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

Lab10 193

DAA Lab

Uploaded by

vikaspant990
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)
9 views7 pages

Lab10 193

DAA Lab

Uploaded by

vikaspant990
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

LAB 10

Name: Vikas Pant

Roll number: 2200290110193

Sem-Sec-Group: 5 C 2

AIM: Write a program to implement 0/1 Knapsack Problem.

0/1 Knapsack Problem :

Source Code :

#include <iostream>
#include <vector>
using namespace std;

// Function to solve the 0/1 Knapsack Problem


int knapsack(int W, vector<int> &weights, vector<int> &values, int n) {
// Create a 2D DP table
vector<vector<int>> dp(n + 1, vector<int>(W + 1, 0));

// Build the table bottom-up


for (int i = 1; i <= n; ++i) {
for (int w = 1; w <= W; ++w) {
if (weights[i - 1] <= w) {
dp[i][w] = max(dp[i - 1][w], values[i - 1] + dp[i - 1][w - weights[i - 1]]);
} else {
dp[i][w] = dp[i - 1][w];
}
}
}

return dp[n][W];
}
int main() {
int n, W;

cout << "Enter the number of items: ";


cin >> n;

cout << "Enter the capacity of the knapsack: ";


cin >> W;

vector<int> weights(n), values(n);

cout << "Enter the weights of the items: ";


for (int i = 0; i < n; ++i) {
cin >> weights[i];
}

cout << "Enter the values of the items: ";


for (int i = 0; i < n; ++i) {
cin >> values[i];
}

int maxValue = knapsack(W, weights, values, n);

cout << "Maximum value that can be obtained: " << maxValue << endl;

return 0;
}
Output:
AIM: Write a program to implement Longest Common Subsequence Problem.

Longest Common Subsequence Problem :

Source Code :
#include <iostream>
#include <vector>
#include <string>
using namespace std;

// Function to find the length of the Longest Common Subsequence


int longestCommonSubsequence(const string &X, const string &Y) {
int m = X.length();
int n = Y.length();

// Create a 2D DP table
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));

// Build the DP table bottom-up


for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
if (X[i - 1] == Y[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1; // Match case
} else {
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); // No match case
}
}
}

return dp[m][n];
}

// Function to reconstruct the LCS


string getLCS(const string &X, const string &Y, const vector<vector<int>> &dp) {
string lcs = "";
int i = X.length();
int j = Y.length();

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;
}
}

return lcs;
}

int main() {
string X, Y;

cout << "Enter the first string: ";


cin >> X;

cout << "Enter the second string: ";


cin >> Y;

int m = X.length();
int n = Y.length();

// Create a DP table
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));

// Fill the DP table and find the LCS length


int lcsLength = longestCommonSubsequence(X, Y);

// Build the DP table for reconstruction


for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
if (X[i - 1] == Y[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}

// Reconstruct the LCS


string lcs = getLCS(X, Y, dp);

cout << "Length of Longest Common Subsequence: " << lcsLength << endl;
cout << "Longest Common Subsequence: " << lcs << endl;

return 0;
}
Output:

You might also like