0% found this document useful (0 votes)
1 views2 pages

DAA EXP 2-3 - Prashant

The document outlines an experiment in a Computer Science and Engineering course focused on implementing the 0-1 Knapsack problem using Dynamic Programming. It includes a program code that calculates the maximum value obtainable given weights and profits, along with an analysis of the time complexity, which is O(N * W). The student conducting the experiment is Prashant Sinha, and the details of the course and submission date are provided.

Uploaded by

Prashant Sinha
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)
1 views2 pages

DAA EXP 2-3 - Prashant

The document outlines an experiment in a Computer Science and Engineering course focused on implementing the 0-1 Knapsack problem using Dynamic Programming. It includes a program code that calculates the maximum value obtainable given weights and profits, along with an analysis of the time complexity, which is O(N * W). The student conducting the experiment is Prashant Sinha, and the details of the course and submission date are provided.

Uploaded by

Prashant Sinha
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/ 2

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment: 2.3
Student Name: Prashant Sinha UID: 22BCS80181
Branch: BE-CSE Section/Group: 607-B
Semester: 5th Date: 22/09/2023
Subject Name: DAA Lab Subject Code: 21CSH-311

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

Program code:
#include <bits/stdc++.h>
using namespace std;

int max(int a, int b) { return (a > b) ? a : b; }

int knapSack(int W, int wt[], int val[], int n)


{
int i, w;
vector<vector<int> > K(n + 1, vector<int>(W + 1));

for (i = 0; i <= n; i++) {


for (w = 0; w <= W; w++) {
if (i == 0 || w == 0)
K[i][w] = 0;
else if (wt[i - 1] <= w)
K[i][w] = max(val[i - 1]
+ K[i - 1][w - wt[i - 1]],
K[i - 1][w]);
else
K[i][w] = K[i - 1][w];
}
}
return K[n][W];
}

int main()
{
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
int profit[] = { 60, 100, 120 };
int weight[] = { 10, 20, 30 };
int W = 50;
int n = sizeof(profit) / sizeof(profit[0]);

cout <<"Maximum value that can be obtained:" << knapSack(W, weight, profit, n);

return 0;
}
Output:

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

You might also like