0% found this document useful (0 votes)
8 views6 pages

Aoa Exp 7 40

The document outlines an experiment to implement the 0/1 knapsack problem using a dynamic programming approach. It includes the algorithm, a C++ program, and discusses the time and space complexity of the solution. The time complexity is O(n × M) and space complexity is O(n × M), with potential optimization to O(M).

Uploaded by

Advay Joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Aoa Exp 7 40

The document outlines an experiment to implement the 0/1 knapsack problem using a dynamic programming approach. It includes the algorithm, a C++ program, and discusses the time and space complexity of the solution. The time complexity is O(n × M) and space complexity is O(n × M), with potential optimization to O(M).

Uploaded by

Advay Joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Experiment No.

07

Name:Arjun Nair Rollno:SE2 40 Date:


Aim: To implement 0/1 knapsack using dynamic programming approach

Theory:

We are given N items where each item has some weight and profit associated with it.
We are also given a bag with capacity W, [i.e., the bag can hold at most W weight in it].
The target is to put the items into the bag such that the sum of profits associated with
them is the maximum possible. Initialize the solution matrix same as the input graph
matrix as a first step.

The constraint here is we can either put an item completely into the bag or cannot put it at
all [It is not possible to put a part of an item into the bag].

Algorithm:
Algorithm Knapsack_01(W, P, M, n):

Create a table V of size (n+1) × (M+1) initialized to 0

For i from 1 to n:

For j from 1 to M:

diff = j - W[i - 1]

If diff >= 0:

V[i][j] = max(V[i - 1][j], V[i - 1][diff] + P[i - 1])

Else:

V[i][j] = V[i - 1][j]

Print "Step", i and the table V

Initialize x as an array of size n with all values 0

j=M

For i from n down to 1:

If V[i][j] > V[i - 1][j]:

x[i - 1] = 1

j = j - W[i - 1]
Experiment No. 07

If V[n][M] == 0:

Print "Optimal solution does not exist."

Return None, None

Print "Optimal Solution:"

For i from 0 to n - 1:

If x[i] = 1:

Print "Item", i+1, "Weight =", W[i], "Profit =", P[i]

Return V[n][M], x

Begin:

Read n

Read W (weights)

Read P (profits)

Read M (maximum weight capacity)

max_profit, items_selected = Knapsack_01(W, P, M, n)

If max_profit is not None:

Print "Maximum Profit:", max_profit

Print "Items Selected:", items_selected


Experiment No. 07

Program:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

pair<int, vector<int>> knapsack_01(vector<int>& W, vector<int>& P, int M, int n)


{ vector<vector<int>> V(n + 1, vector<int>(M + 1, 0));

// Dynamic Programming Table Construction


for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= M; ++j)
{ int diff = j - W[i - 1];
if (diff >= 0) {
V[i][j] = max(V[i - 1][j], V[i - 1][diff] + P[i - 1]);
} else {
V[i][j] = V[i - 1][j];
}
}

// Printing the DP table at each step


cout << "Step " << i << ": \n";
for (const auto& row : V)
{ for (int val : row) {
cout << val << " ";
}
cout << endl;
}
cout << endl;
}

// Backtrack to find the items selected


vector<int> x(n, 0);
int j = M;
for (int i = n; i > 0; --i) {
if (V[i][j] > V[i - 1][j])
{ x[i - 1] = 1;
j -= W[i - 1];
}
}

// If no solution exists
if (V[n][M] == 0) {
cout << "Optimal solution does not exist." << endl;
return {0, {}};
Experiment No. 07

// Printing the optimal solution


cout << "Optimal Solution: \n";
for (int i = 0; i < n; ++i) {
if (x[i] == 1) {
cout << "Item " << i + 1 << ": Weight = " << W[i] << ", Profit = " << P[i] << endl;
}
}

return {V[n][M], x};


}

int main()
{ int n;
cout << "Enter number of items: ";
cin >> n;

vector<int> W(n), P(n);


cout << "Enter weights of items separated by space: ";
for (int i = 0; i < n; ++i) {
cin >> W[i];
}

cout << "Enter profits of items separated by space: ";


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

int M;
cout << "Enter maximum weight capacity of knapsack: ";
cin >> M;

auto [max_profit, items_selected] = knapsack_01(W, P, M, n);

if (max_profit > 0) {
cout << "Maximum Profit: " << max_profit << endl;
cout << "Items Selected (1 means included, 0 means not included): ";
for (int item : items_selected) {
cout << item << " ";
}
cout << endl;
}

return 0;
}
Experiment No. 07

Output:
Experiment No. 07

Conclusion: Describe the complexity of 0/1 knapsack problem

Time Complexity:

The 0/1 Knapsack problem using Dynamic Programming has a time complexity of O(n ×
M), where:

● n is the number of items.


● M is the maximum weight capacity of the knapsack.

We construct a DP table of size (n+1) × (M+1), and each entry in the table is computed in
constant time O(1). Thus, the overall complexity is O(n × M).

Space Complexity:

The space complexity is O(n × M) because we store a 2D DP table of size (n+1) × (M+1).
However, since the current state only depends on the previous row, the space can be
optimized to O(M) by using a 1D array instead of a full table.

You might also like