Professional Documents
Culture Documents
Experiment No.-2.3-3
Experiment No.-2.3-3
EXPERIMENT NO.-2.3
3. Algorithm:
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;
};
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.