0% found this document useful (0 votes)
4 views

Daa 7

Uploaded by

Yash
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)
4 views

Daa 7

Uploaded by

Yash
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/ 4

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 7

Student Name: Yash pandey UID:22BCS15020


Branch: BE-CSE Section/Group:619(A)
Semester:th5 Date of Performance:03/10/24
Subject Name: DAA Lab Subject Code: 22CHS-311

1. Aim: Develop a program and analyze complexity to implement 0-1 Kn


using Dynamic Programming

2. Objective: To implementthe 0-1 Knapsack problem using dynamic


programming by constructing a solution that maximizes the total valu
selecteditemswhile ensuringthe total weightdoes not exceedthe given
capacity. Analyze the time and space complexity of the solution.

3. Algorithm:

Step 1:- Create a DP Table:

 Let dp[i][w] be a 2D array where i represents the first i items and w


represents the weight capacity.
 dp[i][w] will store the maximum value that can be obtained with
capacity w using the first i items.

Step 2:- Initialization:

 For i = 0 (no items), dp[0][w] = 0 for all w because no value can


be obtained with no items.
 For w = 0, dp[i][0] = 0 for all i because no value can be obtained
with zero capacity.

Step 3:- Filling the DP Table:

 For each item i from 1 to n:


o For each capacity w from 1 to W:
 If the weight of the current item wt[i-1] is less than or equal to w, we
can either:
1. Include the item: val[i-1] + dp[i-1][w - wt[i-1]]
2. Exclude the item: dp[i-1][w]
 Take the maximum of both choices:
dp[i][w]=max(dp[i−1][w],val[i−1]+dp[i−1][w−wt[i−1]])dp[i][w] =
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
\max(dp[i-1][w], val[i-1] + dp[i-1][w - wt[i-
1]])dp[i][w]=max(dp[i−1][w],val[i−1]+dp[i−1][w−wt[i−1]])
 If the weight of the current item is greater than w, exclude the ite
dp[i][w]=dp[i−1][w]dp[i][w] = dp[i-1][w]dp[i][w]=dp[i−1][w]

Step 4:- Return the Result:

 The maximum value that can be put in the knapsack is found in


dp[n][W].

4. Implementation/Code:
#include <iostream>
#include <vector>
using namespace std;

// Function to return the maximum value that can be put in a knapsack o


capacity W
int knapsack(int W, vector<int>& wt, vector<int>& val, int n) {
vector<vector<int>> dp(n+1, vector<int>(W+1, 0));

// Build table dp[][] in bottom-up manner


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

// The maximum value in the knapsack with capacity W


return dp[n][W];
}

int main() {
// Input values
vector<int> val = {60, 100, 120};
vector<int> wt = {10, 20, 30};
int W = 50;
int n = val.size();

// Descriptive Output
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
cout << "---------------------------\n";
cout << " 0-1 KNAPSACK PROBLEM \n";
cout << "---------------------------\n\n";

cout << "Item Details:\n";


cout << "--------------------------------\n";
cout << "Item | Weight | Value\n";
cout << "--------------------------------\n";
for (int i = 0; i < n; i++) {
cout << " " << i+1 << " | " << wt[i] << " | " << val[i] << "
}

cout << "\nMaximum Capacity of Knapsack: " << W << "\n";


cout << "--------------------------------\n\n";

int result = knapsack(W, wt, val, n);

cout << "--------------------------------\n";


cout << "Maximum Value that can be placed in the knapsack = " << r
<< "\n";
cout << "--------------------------------\n";

return 0;
}

5. Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

6. Time Complexity: O(n * W), where n is the number of items and W is the
capacity of the knapsack. This is because we fill the DP table of size n × W.
Space Complexity: O(n * W) for the DP table.

7. Learning Outcomes:
 Dynamic Programming Concept: Learned how to optimize the 0-1
Knapsack problem using dynamic programming to reduce time complexity.
 Table-based Approach: Understood the use of a 2D table to store
intermediate results and build the solution bottom-up.
 Code Presentation: Gained insights into making program output more
readable and professional through structured formatting.

You might also like