Skip to content

Commit e6fb81d

Browse files
authored
Add knapsack problem (TheAlgorithms#2330)
1 parent cccb7be commit e6fb81d

3 files changed

Lines changed: 141 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package DynamicProgramming;
2+
3+
/* A Naive recursive implementation
4+
of 0-1 Knapsack problem */
5+
public class BruteForceKnapsack {
6+
7+
// A utility function that returns
8+
// maximum of two integers
9+
static int max(int a, int b) {
10+
return (a > b) ? a : b;
11+
}
12+
13+
// Returns the maximum value that
14+
// can be put in a knapsack of
15+
// capacity W
16+
static int knapSack(int W, int wt[], int val[], int n) {
17+
// Base Case
18+
if (n == 0 || W == 0)
19+
return 0;
20+
21+
// If weight of the nth item is
22+
// more than Knapsack capacity W,
23+
// then this item cannot be included
24+
// in the optimal solution
25+
if (wt[n - 1] > W)
26+
return knapSack(W, wt, val, n - 1);
27+
28+
// Return the maximum of two cases:
29+
// (1) nth item included
30+
// (2) not included
31+
else
32+
return max(val[n - 1] + knapSack(W - wt[n - 1], wt, val, n - 1), knapSack(W, wt, val, n - 1));
33+
}
34+
35+
// Driver code
36+
public static void main(String args[]) {
37+
int val[] = new int[] { 60, 100, 120 };
38+
int wt[] = new int[] { 10, 20, 30 };
39+
int W = 50;
40+
int n = val.length;
41+
System.out.println(knapSack(W, wt, val, n));
42+
}
43+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package DynamicProgramming;
2+
3+
// A Dynamic Programming based solution
4+
// for 0-1 Knapsack problem
5+
public class DyanamicProgrammingKnapsack {
6+
static int max(int a, int b) {
7+
return (a > b) ? a : b;
8+
}
9+
10+
// Returns the maximum value that can
11+
// be put in a knapsack of capacity W
12+
static int knapSack(int W, int wt[], int val[], int n) {
13+
int i, w;
14+
int K[][] = new int[n + 1][W + 1];
15+
16+
// Build table K[][] in bottom up manner
17+
for (i = 0; i <= n; i++) {
18+
for (w = 0; w <= W; w++) {
19+
if (i == 0 || w == 0)
20+
K[i][w] = 0;
21+
else if (wt[i - 1] <= w)
22+
K[i][w] = max(val[i - 1] + K[i - 1][w - wt[i - 1]], K[i - 1][w]);
23+
else
24+
K[i][w] = K[i - 1][w];
25+
}
26+
}
27+
28+
return K[n][W];
29+
}
30+
31+
// Driver code
32+
public static void main(String args[]) {
33+
int val[] = new int[] { 60, 100, 120 };
34+
int wt[] = new int[] { 10, 20, 30 };
35+
int W = 50;
36+
int n = val.length;
37+
System.out.println(knapSack(W, wt, val, n));
38+
}
39+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package DynamicProgramming;
2+
// Here is the top-down approach of
3+
// dynamic programming
4+
public class MemoizationTechniqueKnapsack {
5+
6+
//A utility function that returns
7+
//maximum of two integers
8+
static int max(int a, int b) {
9+
return (a > b) ? a : b;
10+
}
11+
12+
//Returns the value of maximum profit
13+
static int knapSackRec(int W, int wt[], int val[], int n, int[][] dp) {
14+
15+
// Base condition
16+
if (n == 0 || W == 0)
17+
return 0;
18+
19+
if (dp[n][W] != -1)
20+
return dp[n][W];
21+
22+
if (wt[n - 1] > W)
23+
24+
// Store the value of function call
25+
// stack in table before return
26+
return dp[n][W] = knapSackRec(W, wt, val, n - 1, dp);
27+
28+
else
29+
30+
// Return value of table after storing
31+
return dp[n][W] = max((val[n - 1] + knapSackRec(W - wt[n - 1], wt, val, n - 1, dp)),
32+
knapSackRec(W, wt, val, n - 1, dp));
33+
}
34+
35+
static int knapSack(int W, int wt[], int val[], int N) {
36+
37+
// Declare the table dynamically
38+
int dp[][] = new int[N + 1][W + 1];
39+
40+
// Loop to initially filled the
41+
// table with -1
42+
for (int i = 0; i < N + 1; i++)
43+
for (int j = 0; j < W + 1; j++)
44+
dp[i][j] = -1;
45+
46+
return knapSackRec(W, wt, val, N, dp);
47+
}
48+
49+
//Driver Code
50+
public static void main(String[] args) {
51+
int val[] = { 60, 100, 120 };
52+
int wt[] = { 10, 20, 30 };
53+
54+
int W = 50;
55+
int N = val.length;
56+
57+
System.out.println(knapSack(W, wt, val, N));
58+
}
59+
}

0 commit comments

Comments
 (0)