Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f981a2b

Browse files
github-actionsgithub-actions
github-actions
authored and
github-actions
committed
Formatted with Google Java Formatter
1 parent e6fb81d commit f981a2b

File tree

3 files changed

+94
-102
lines changed

3 files changed

+94
-102
lines changed

‎DynamicProgramming/BruteForceKnapsack.java

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,38 @@
44
of 0-1 Knapsack problem */
55
public class BruteForceKnapsack {
66

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-
}
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+
}
1212

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;
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) return 0;
2019

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);
20+
// If weight of the nth item is
21+
// more than Knapsack capacity W,
22+
// then this item cannot be included
23+
// in the optimal solution
24+
if (wt[n - 1] > W) return knapSack(W, wt, val, n - 1);
2725

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-
}
26+
// Return the maximum of two cases:
27+
// (1) nth item included
28+
// (2) not included
29+
else
30+
return max(val[n - 1] + knapSack(W - wt[n - 1], wt, val, n - 1), knapSack(W, wt, val, n - 1));
31+
}
3432

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-
}
33+
// Driver code
34+
public static void main(String args[]) {
35+
int val[] = new int[] {60, 100, 120};
36+
int wt[] = new int[] {10, 20, 30};
37+
int W = 50;
38+
int n = val.length;
39+
System.out.println(knapSack(W, wt, val, n));
40+
}
4341
}

‎DynamicProgramming/DyanamicProgrammingKnapsack.java

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,34 @@
33
// A Dynamic Programming based solution
44
// for 0-1 Knapsack problem
55
public class DyanamicProgrammingKnapsack {
6-
static int max(int a, int b) {
7-
return (a > b) ? a : b;
8-
}
6+
static int max(int a, int b) {
7+
return (a > b) ? a : b;
8+
}
99

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];
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];
1515

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-
}
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) K[i][w] = 0;
20+
else if (wt[i - 1] <= w) K[i][w] = max(val[i - 1] + K[i - 1][w - wt[i - 1]], K[i - 1][w]);
21+
else K[i][w] = K[i - 1][w];
22+
}
23+
}
2724

28-
return K[n][W];
29-
}
25+
return K[n][W];
26+
}
3027

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-
}
28+
// Driver code
29+
public static void main(String args[]) {
30+
int val[] = new int[] {60, 100, 120};
31+
int wt[] = new int[] {10, 20, 30};
32+
int W = 50;
33+
int n = val.length;
34+
System.out.println(knapSack(W, wt, val, n));
35+
}
3936
}
Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,56 @@
11
package DynamicProgramming;
2-
// Here is the top-down approach of
2+
// Here is the top-down approach of
33
// dynamic programming
44
public class MemoizationTechniqueKnapsack {
55

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-
}
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+
}
1111

12-
//Returns the value of maximum profit
13-
static int knapSackRec(int W, int wt[], int val[], int n, int[][] dp) {
12+
// Returns the value of maximum profit
13+
static int knapSackRec(int W, int wt[], int val[], int n, int[][] dp) {
1414

15-
// Base condition
16-
if (n == 0 || W == 0)
17-
return 0;
15+
// Base condition
16+
if (n == 0 || W == 0) return 0;
1817

19-
if (dp[n][W] != -1)
20-
return dp[n][W];
18+
if (dp[n][W] != -1) return dp[n][W];
2119

22-
if (wt[n - 1] > W)
20+
if (wt[n - 1] > W)
2321

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);
22+
// Store the value of function call
23+
// stack in table before return
24+
return dp[n][W] = knapSackRec(W, wt, val, n - 1, dp);
25+
else
2726

28-
else
27+
// Return value of table after storing
28+
return dp[n][W] =
29+
max(
30+
(val[n - 1] + knapSackRec(W - wt[n - 1], wt, val, n - 1, dp)),
31+
knapSackRec(W, wt, val, n - 1, dp));
32+
}
2933

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+
static int knapSack(int W, int wt[], int val[], int N) {
3435

35-
static int knapSack(int W, int wt[], int val[], int N) {
36+
// Declare the table dynamically
37+
int dp[][] = new int[N + 1][W + 1];
3638

37-
// Declare the table dynamically
38-
int dp[][] = new int[N + 1][W + 1];
39+
// Loop to initially filled the
40+
// table with -1
41+
for (int i = 0; i < N + 1; i++) for (int j = 0; j < W + 1; j++) dp[i][j] = -1;
3942

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;
43+
return knapSackRec(W, wt, val, N, dp);
44+
}
4545

46-
return knapSackRec(W, wt, val, N, dp);
47-
}
46+
// Driver Code
47+
public static void main(String[] args) {
48+
int val[] = {60, 100, 120};
49+
int wt[] = {10, 20, 30};
4850

49-
//Driver Code
50-
public static void main(String[] args) {
51-
int val[] = { 60, 100, 120 };
52-
int wt[] = { 10, 20, 30 };
51+
int W = 50;
52+
int N = val.length;
5353

54-
int W = 50;
55-
int N = val.length;
56-
57-
System.out.println(knapSack(W, wt, val, N));
58-
}
54+
System.out.println(knapSack(W, wt, val, N));
55+
}
5956
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /