forked from rahulray11/github-fest-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKnapsackGreedy.js
61 lines (50 loc) · 1.42 KB
/
KnapsackGreedy.js
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
// Calculate Knapsack
function knapsack(arr, maxWeight, size) {
let currentWeight = 0,
finalValue = 0;
let arrRatio = [];
// Find the ratio of each item
for (let i = 0; i < size; i++) {
const { weight, value, name } = arr[i];
const ratio = value / weight;
const payload = { weight, value, name, ratio };
arrRatio.push(payload);
}
// Sort ratio
arrRatio.sort(function (a, b) {
return b.ratio - a.ratio;
});
// Loop for finding best item
function loopWeight(weight, value) {
if (currentWeight + weight <= maxWeight) {
currentWeight += weight;
finalValue += value;
loopWeight(weight, value);
}
}
for (let i = 0; i < size; i++) {
const { weight, value } = arrRatio[i];
loopWeight(weight, value);
}
// Show the final value
console.log(
`Maximum profit = ${finalValue} which total weight = ${currentWeight}`
);
}
function main() {
const knapsackWeight = 31;
const arr = [
{ name: "Diamond", value: 5120, weight: 4 },
{ name: "Zamrud", value: 6580, weight: 6 },
{ name: "Ruby", value: 4720, weight: 3 },
{ name: "Sapphire", value: 5860, weight: 5 },
{ name: "Gold", value: 8125, weight: 8 },
{ name: "Pearl", value: 3250, weight: 3 },
];
const size = arr.length;
console.log(`Knapsack Weight = ${knapsackWeight}`);
console.log(`Item from array: `);
console.table(arr);
knapsack(arr, knapsackWeight, size);
}
main();