Daa 7
Daa 7
Experiment 7
3. Algorithm:
4. Implementation/Code:
#include <iostream>
#include <vector>
using namespace std;
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";
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.