0% found this document useful (0 votes)
26 views3 pages

Daa Mad Worksheet7 CPP NEMESIS

The document outlines an experiment for implementing the 0-1 Knapsack problem using Dynamic Programming in a Computer Science & Engineering lab. It includes the aim, objectives, algorithm, implementation code, and complexity analysis, stating that both time and space complexity are O(N*W). The learning outcomes emphasize understanding dynamic programming, efficient problem-solving, and insights into optimization trade-offs.

Uploaded by

vishalbhateria
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)
26 views3 pages

Daa Mad Worksheet7 CPP NEMESIS

The document outlines an experiment for implementing the 0-1 Knapsack problem using Dynamic Programming in a Computer Science & Engineering lab. It includes the aim, objectives, algorithm, implementation code, and complexity analysis, stating that both time and space complexity are O(N*W). The learning outcomes emphasize understanding dynamic programming, efficient problem-solving, and insights into optimization trade-offs.

Uploaded by

vishalbhateria
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/ 3

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 7

Student Name: Ayush Singh UID: 22BCS14510


Branch: CSE Section/Group:635/B
Semester: 5 Date of Performance:23/09/24
Subject Name: DAA Lab Subject Code: 22CSH-311

1. Aim:
Develop a program and analyze complexity to implement 0-1 Knapsack
using Dynamic Programming.
2. Objective
To implement 0-1 Knapsack using Dynamic Programming.
3. Algorithm
o Initialize a 2D table for storing results.
o For each item, update the table with the maximum profit for each
weight.
o Consider both including and excluding the item.
o Repeat for all items and capacities.
o The final cell in the table contains the maximum profit.

4. Implemetation/Code

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000;
int knapsack(int W, int wt[], int profit[], int n) {
int dp[MAXN + 1][MAXN + 1] = {0};
for (int i = 1; i <= n; ++i) {
for (int w = 1; w <= W; ++w) {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

if (wt[i - 1] <= w)
dp[i][w] = max(dp[i - 1][w], profit[i - 1] + dp[i - 1][w - wt[i - 1]]);
else
dp[i][w] = dp[i - 1][w];
}
}
return dp[n][W];
}
int main() {
int n, W;
cout << "ENTER NUMBER OF ITEMS AND CAPACITY: ";
cin >> n >> W;
if (n <= 0 || W <= 0) {
cout << "Number of items and capacity must be positive." << endl;
return 1;
}
if (n > MAXN || W > MAXN) {
cout << "Number of items or capacity exceeds maximum allowed size." << endl;
return 1;
}
int profit[MAXN], wt[MAXN];
cout << "ENTER PROFITS AND WEIGHTS: ";
for (int i = 0; i < n; ++i) {
cin >> profit[i] >> wt[i];
}
cout << "MAXIMUM KNAPSACK VALUE: " << knapsack(W, wt, profit, n) <<
endl;
return 0;
}
Output
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

5. Time Complexity : O( N*W)

6. Space Complexity : O(N*W)

7. Learning Outcomes:-

1. Learn dynamic programming for optimization problems.


2. Understand how to solve the knapsack problem efficiently.
3. Gain insight into space and time trade-offs.

You might also like