This repository has been archived by the owner on Nov 21, 2020. It is now read-only.
forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Knapsack_DP.cpp
84 lines (65 loc) · 2.43 KB
/
Knapsack_DP.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*DESCRIPTION*/
/*Reusing precalculated results is nothing but Dynamic Programming*/
/*In 0-1 Knapsack problem, WE are given a Knapsack (a kind of bag) and several items with their weight and profit. The bag has a maximum capacity of weight it can hold.
Now your task is to maximise the profit by choosing several items. It is called 0-1 because of the decision values, either an item will be taken or it will be discarded.
The right approach to solve it is using DP (dynamic programming). Let the capacity of Knapsack be K, and there be N items, wt[] be the array of weights and
wt[i] be the weight of ith item; P[] be the array of profits and P[i] be the profit gained by ith item.
Now you need to maximise Sigma (P[i]), take the sum as SUM. and constraint is Sigma (wt[i]) We can solve it using DP.*/
/*SOURCE CODE*/
#include <iostream>
using namespace std;
int max(int a, int b) // A utility function that returns maximum of two integers
{
return (a > b) ? a : b;
}
int knapSack(int K, int wt[], int P[], int n) // Returns the maximum value that can be put in a knapsack of capacity W
{
int i, w;
int DP[n + 1][K + 1];
for(i = 0; i <= n; i++)
{
for(w = 0; w <= K; w++)
{
if(i == 0 || w == 0)
DP[i][w] = 0;
else if(wt[i - 1] <= w)
DP[i][w] = max(P[i - 1] + DP[i - 1][w - wt[i - 1]], DP[i - 1][w]);
else
DP[i][w] = DP[i - 1][w];
}
}
return DP[n][K];
}
int knapSack_large(int K, int wt[], int P[], int n)
{
int i, w;
int DP[2][K + 1];
for(i = 1; i <= K; i++)
DP[0][i] = 0;
DP[0][0] = 0;
DP[1][0] = 0;
for(i = 1; i <= n; i++)
{
for(w = 1; w <= K; w++)
{
if(wt[i - 1] <= w)
DP[i & 1][w] = max(P[i - 1] + DP[!(i & 1)][w - wt[i - 1]], DP[!(i & 1)][w]);
else
DP[i & 1][w] = DP[!(i & 1)][w];
}
}
return DP[n & 1][K];
}
int main()
{
int P[] = {11, 22, 33, 44, 55};
int wt[] = {111, 121, 131, 141, 151};
int K = 300;
int n = sizeof(wt) / sizeof(wt[0]);
cout << knapSack(K, wt, P, n);
cout << endl << "Memory Optimised KnapSack " << knapSack_large(K, wt, P, n);
return 0;
}
/* Output
99
*/