Repository navigation
Expand file tree
/
Copy pathknapsack.js
More file actions
73 lines (61 loc) · 1.99 KB
/
Copy pathknapsack.js
File metadata and controls
73 lines (61 loc) · 1.99 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"use strict";
module.exports = function (){
this.valueMatrix = [];
this.keepMatrix = [];
this.setup = function(items, maxWeight){
this.valueMatrix = new Array(maxWeight);
for(let row = 0; row < items.length; row++){
this.valueMatrix[row] = new Array(maxWeight);
}
for(let row = 0; row < items.length; row++){
this.keepMatrix[row] = new Array(maxWeight);
}
};
this.pack = function(items, maxWeight){
this.setup(items, maxWeight);
for(let row = 0; row < items.length; row++){
for(let col = 0; col < maxWeight; col++){
const maxWeight = col + 1;
const weight = items[row].Weight;
const newValue = items[row].Value;
this.valueMatrix[row][col] = 0;
this.keepMatrix[row][col] = false;
// does the current item's weight fit?
if (weight <= maxWeight) {
this.valueMatrix[row][col] = newValue;
this.keepMatrix[row][col] = true;
}
// is this better that what we had?
if (row > 0){
const prevValue = this.valueMatrix[row-1][col];
if (prevValue > newValue){
this.valueMatrix[row][col] = prevValue;
this.keepMatrix[row][col] = false;
// is there a possible remainder?
const remainder = maxWeight - weight;
if (remainder > 0){
const remainderValue = this.valueMatrix[row-1][remainder - 1];
const combinedValue = remainderValue + newValue;
if (combinedValue > this.valueMatrix[row][col]){
this.valueMatrix[row][col] = combinedValue;
this.keepMatrix[row][col] = true;
}
}
}
}
}
}
let packedItems = [];
let i = items.length;
let w = maxWeight;
while(i > 0){
const match = this.keepMatrix[i-1][w-1];
if (match){
packedItems.push(items[i-1].Name);
w = w - items[i-1].Weight;
}
i = i - 1;
}
return packedItems;
};
};