Skip to content

Commit 538abac

Browse files
algorithm: knapsack (#69)
1 parent e693ba1 commit 538abac

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

dynamic_programing/Knapsack.ts

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* @function knapsack
3+
* @description Given weights and values of n (numberOfItems) items, put these items in a knapsack of capacity to get the maximum total value in the knapsack. In other words, given two integer arrays values[0..n-1] and weights[0..n-1] which represent values and weights associated with n items respectively. Also given an integer capacity which represents knapsack capacity, find out the maximum value subset of values[] such that sum of the weights of this subset is smaller than or equal to capacity. You cannot break an item, either pick the complete item or don’t pick it (0-1 property).
4+
* @Complexity_Analysis
5+
* Space complexity - O(1)
6+
* Time complexity (independent of input) : O(numberOfItems * capacity)
7+
*
8+
* @return maximum value subset of values[] such that sum of the weights of this subset is smaller than or equal to capacity.
9+
* @see [Knapsack](https://www.geeksforgeeks.org/0-1-knapsack-problem-dp-10/)
10+
* @example knapsack(3, 8, [3, 4, 5], [30, 50, 60]) = 90
11+
*/
12+
export const knapsack = (
13+
capacity: number,
14+
weights: number[],
15+
values: number[]
16+
) => {
17+
if (weights.length != values.length) {
18+
throw new Error(
19+
"weights and values arrays should have same number of elements"
20+
);
21+
}
22+
23+
const numberOfItems = weights.length;
24+
25+
// Declaring a data structure to store calculated states/values
26+
let dp: number[][] = new Array(numberOfItems + 1);
27+
28+
for (let i = 0; i < dp.length; i++) {
29+
// Placing an array at each index of dp to make it a 2d matrix
30+
dp[i] = new Array(capacity + 1);
31+
}
32+
33+
// Loop traversing each state of dp
34+
for (let i = 0; i < numberOfItems; i++) {
35+
for (let j = 0; j <= capacity; j++) {
36+
if (i == 0) {
37+
if (j >= weights[i]) {
38+
// grab the first item if it's weight is less than remaining weight (j)
39+
dp[i][j] = values[i];
40+
} else {
41+
// if weight[i] is more than remaining weight (j) leave it
42+
dp[i][j] = 0;
43+
}
44+
} else if (j < weights[i]) {
45+
// if weight of current item (weights[i]) is more than remaining weight (j), leave the current item and just carry on previous items
46+
dp[i][j] = dp[i - 1][j];
47+
} else {
48+
// select the maximum of (if current weight is collected thus adding it's value) and (if current weight is not collected thus not adding it's value)
49+
dp[i][j] = Math.max(
50+
dp[i - 1][j - weights[i]] + values[i],
51+
dp[i - 1][j]
52+
);
53+
}
54+
}
55+
}
56+
57+
// Return the final maximized value at last position of dp matrix
58+
return dp[numberOfItems - 1][capacity];
59+
};
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { knapsack } from "../Knapsack";
2+
3+
const cases: [number, number[], number[], number][] = [
4+
[15, [6, 5, 6, 6, 3, 7], [5, 6, 4, 6, 5, 2], 17],
5+
[50, [10, 20, 30], [60, 100, 120], 220],
6+
[8, [3, 4, 5], [30, 50, 60], 90],
7+
[
8+
5,
9+
[1, 1, 1, 1, 1],
10+
[1000000000, 1000000000, 1000000000, 1000000000, 1000000000],
11+
5000000000,
12+
],
13+
];
14+
15+
describe("Knapsack Algorithm Test", () => {
16+
test.each(cases)(
17+
"given %p capacity available, with weights %p and values %p, knapsack should return %p",
18+
(capacity, weights, values, expectedResult) => {
19+
const result = knapsack(capacity, weights, values);
20+
expect(result).toBe(expectedResult);
21+
}
22+
);
23+
});

0 commit comments

Comments
 (0)