-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamicKnapsack.js
More file actions
34 lines (31 loc) · 1.25 KB
/
dynamicKnapsack.js
File metadata and controls
34 lines (31 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const dynamicKnapsack = function (weightCap, weights, values) {
const numItem = weights.length;
const matrix = new Array(numItem);
for (let index = 0; index <= numItem; index++) {
matrix[index] = new Array(weightCap + 1);
for (let weight = 0; weight <= weightCap; weight++) {
if (index === 0 || weight === 0) {
matrix[index][weight] = 0;
// else if the weight of element at index - 1 <= weight:
} else if (weights[index - 1] <= weight) {
// find possible values of including and excluding the item
const prevItemsValue = matrix[index - 1][weight];
const currItemsWeight = weights[index - 1];
const currItemsValue = values[index - 1];
const availWeight = weight - currItemsWeight;
// set element at [index][weight] to max of those values
matrix[index][weight] = Math.max(
prevItemsValue,
matrix[index - 1][availWeight] + currItemsValue,
);
} else {
// set element at [index][weight] to element one above
matrix[index][weight] = matrix[index - 1][weight];
}
}
}
console.log('matrix\n', matrix);
console.log('=>', matrix[numItem][weightCap]);
return matrix[numItem][weightCap];
};
export default dynamicKnapsack;