You are on page 1of 3

Course Name: DAA Lab Course Code: 21ITH-311/21CSH-311

EXPERIMENT NO.-2.3

Student Name: GAJENDER UID: 21BCS4627


Branch: CSE Section/Group: 21BCS_IOT-629-A
Semester: 5th Date of Performance:19-10-2023
Subject Name: DAA Subject Code:21CSH-311

1. Aim of the Practical:


Develop a program and analyze complexity to implement 0-1 Knapsack using Dynamic
Programming.

2. Objectives: To implement 0-1 knapsack problem using Dynamic Programming.

3. Algorithm:

a. Create a 2D DP table dp of size (n+1) x (capacity+1).


b. Initialize the first row and the first column of dp to 0, since there's no value associated with an
empty knapsack or with no items.
c. Iterate through the items from 1 to n and the knapsack capacities from 1 to capacity.
d. For each cell (i, j) in the DP table:
e. If the weight of the current item, weights[i-1], is greater than j, set dp[i][j] to be the same as dp[i-
1][j]. This means you cannot include the item in the knapsack.
f. If the weight of the current item is less than or equal to j, set dp[i][j] to be the maximum of:
g. The value of not including the current item: dp[i-1][j].
h. The value of including the current item: values[i-1] + dp[i-1][j - weights[i-1]].
i. The value in the bottom-right cell of the DP table, dp[n][capacity], will represent the maximum
value that can be obtained using the given items and knapsack capacity.
j. To reconstruct the selected items, start from i = n and j = capacity and trace back through the DP
table to find which items were included.
k. The selected items are the ones for which dp[i][j] differs from dp[i-1][j]. Store these items in the
knapsack to maximize the value.

4. Program:

CODE:
#include <iostream>
#include <vector>
using namespace std;
Course Name: DAA Lab Course Code: 21ITH-311/21CSH-311

struct Item {
int weight;
int value;
};

int knapsack(int capacity, const vector<Item>& items, vector<vector<int>>& dp) {


int n = items.size();
vector<vector<bool>> selected(n + 1, vector<bool>(capacity + 1, false));
for (int i = 1; i <= n; i++) {
for (int w = 0; w <= capacity; w++) {
if (items[i - 1].weight <= w) {
int including = dp[i - 1][w - items[i - 1].weight] + items[i - 1].value;
int excluding = dp[i - 1][w];
if (including >= excluding) {
dp[i][w] = including;
selected[i][w] = true;
} else {
dp[i][w] = excluding;
selected[i][w] = false;
}
} else {
dp[i][w] = dp[i - 1][w];
}
}
}
vector<int> selected_items;
int i = n, w = capacity;
while (i > 0 && w > 0) {
if (selected[i][w]) {
selected_items.push_back(i - 1);
w -= items[i - 1].weight;
}
i--;
}
cout << "Items included in the knapsack: ";
for (int i = selected_items.size() - 1; i >= 0; i--) {
cout << selected_items[i]+1 << " ";
}
cout << endl;
return dp[n][capacity];
}
int main() {
int n, capacity;
cout<<"NAME : GAJENDER"<<endl;
Course Name: DAA Lab Course Code: 21ITH-311/21CSH-311

cout<<"UID : 21BCS4627"<<endl;
cout << "Enter the number of items: ";
cin >> n;
vector<Item> items(n);
cout << "Enter the weights and values of the items:" << endl;
for (int i = 0; i < n; i++) {
cin >> items[i].weight >> items[i].value;
}
cout<<"Items |Weight | Profit"<<endl;
for (int i = 0; i < n; i++) {
cout<< "Item:"<<i + 1<<" | "<<items[i].weight<<" | " << items[i].value<<endl;
}
cout << "Enter the capacity of the knapsack: ";
cin >> capacity;
vector<vector<int>> dp(n + 1, vector<int>(capacity + 1, 0));
int maxValue = knapsack(capacity, items, dp);
cout << "The maximum value that can be obtained is: " << maxValue << endl;
return 0;
}
OUTPUT:

TIME COMPLEXITY:
Time Complexity: O(N * W). where ‘N’ is the number of elements and ‘W’ is capacity.

You might also like