Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkSpy25 authored Oct 4, 2022
1 parent 44d9785 commit 0612945
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
Binary file added knapsack.class
Binary file not shown.
37 changes: 37 additions & 0 deletions knapsack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//Ankita Patil

class knapsack {
int knap(int[] wt, int[] val, int W, int n) {
int[][] M = new int[n + 1][W + 1];
for (int i = 0; i <= n; i++) {
for (int w = 0; w <= W; w++) {
if (w == 0 || i == 0)
M[i][w] = 0;
else if (wt[i - 1] > w)
M[i][w] = M[i - 1][w];
else
M[i][w] = Math.max(M[i - 1][w], val[i - 1] + M[i - 1][w - wt[i - 1]]);
}
}
int i = n, k = W;
while (i > 0 && k > 0) {
if (M[i][k] != M[i - 1][k]) {
System.out.println(i);
i = i - 1;
k = k - wt[i];
} else
i = i - 1;
}
return M[n][W];
}

public static void main(String[] args) {
int[] val = new int[] { 10, 4, 9, 11 };
int[] wt = new int[] { 3, 5, 6, 2 };
int W = 7;
int n = val.length;
knapsack ob = new knapsack();
System.out.println("Maximum value in Knapsack: " + ob.knap(wt, val, W, n));
}

}

0 comments on commit 0612945

Please sign in to comment.