Skip to content

Commit c82934f

Browse files
authored
Merge pull request #199 from RAUNAK-PANDEY/Raunak
0/1 Knapsack using Tabulation method
2 parents bf61b35 + bb90295 commit c82934f

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
You are given weights and values of N items, put these items in a knapsack of capacity W to get the maximum total value in the knapsack. Note that we have only one quantity of each item.
3+
In other words, given two integer arrays val[0..N-1] and wt[0..N-1] which represent values and weights associated with N items respectively. Also given an integer W which represents knapsack capacity, find out the maximum value subset of val[] such that sum of the weights of this subset is smaller than or equal to W. You cannot break an item, either pick the complete item or don’t pick it (0-1 property).
4+
5+
Example 1:
6+
7+
Input:
8+
N = 3
9+
W = 4
10+
values[] = {1,2,3}
11+
weight[] = {4,5,1}
12+
Output: 3
13+
Example 2:
14+
15+
Input:
16+
N = 3
17+
W = 3
18+
values[] = {1,2,3}
19+
weight[] = {4,5,6}
20+
Output: 0
21+
*/
22+
23+
// Alternative Method for 0/1 Knapsack
24+
//Tabulation Method (Bottom-up Approach)
25+
#include<bits/stdc++.h>
26+
using namespace std;
27+
28+
29+
// } Driver Code Ends
30+
31+
32+
class Solution
33+
{
34+
public:
35+
//Function to return max value that can be put in knapsack of capacity W.
36+
int knapSack(int W, int wt[], int val[], int n)
37+
{
38+
// Your code here
39+
int dp[n+1][W+1];
40+
for(int i=0;i<n+1;i++)
41+
{
42+
for(int j=0;j<W+1;j++)
43+
if(i==0 || j==0)
44+
dp[i][j]=0;
45+
else if(wt[i-1]<=j)
46+
dp[i][j]= max(val[i-1] + dp[i-1][j-wt[i-1]] , dp[i-1][j]);
47+
else
48+
dp[i][j]= dp[i-1][j];
49+
}
50+
return dp[n][W];
51+
}
52+
};
53+
54+
// { Driver Code Starts.
55+
56+
int main()
57+
{
58+
//taking total testcases
59+
int t;
60+
cin>>t;
61+
while(t--)
62+
{
63+
//reading number of elements and weight
64+
int n, w;
65+
cin>>n>>w;
66+
67+
int val[n];
68+
int wt[n];
69+
70+
//inserting the values
71+
for(int i=0;i<n;i++)
72+
cin>>val[i];
73+
74+
//inserting the weights
75+
for(int i=0;i<n;i++)
76+
cin>>wt[i];
77+
Solution ob;
78+
//calling method knapSack()
79+
cout<<ob.knapSack(w, wt, val, n)<<endl;
80+
81+
}
82+
return 0;
83+
} // } Driver Code Ends
84+

Dynamic Programming/knapsack_01.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,28 @@ class Solution
8585
}
8686
};
8787

88+
89+
// Alternative Method for 0/1 Knapsack
90+
//Tabulation Method (Bottom-up Approach)
91+
//Function to return max value that can be put in knapsack of capacity W.
92+
int knapSack(int W, int wt[], int val[], int n)
93+
{
94+
// Your code here
95+
int dp[n+1][W+1];
96+
for(int i=0;i<n+1;i++)
97+
{
98+
for(int j=0;j<W+1;j++)
99+
if(i==0 || j==0)
100+
dp[i][j]=0;
101+
else if(wt[i-1]<=j)
102+
dp[i][j]= max(val[i-1] + dp[i-1][j-wt[i-1]] , dp[i-1][j]);
103+
else
104+
dp[i][j]= dp[i-1][j];
105+
}
106+
return dp[n][W];
107+
}
108+
109+
88110
// { Driver Code Starts.
89111

90112
int main()

0 commit comments

Comments
 (0)