Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ktrongnhan fix for 248 #338

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 12 additions & 31 deletions src/algorithms/sets/knapsack-problem/Knapsack.js
Original file line number Diff line number Diff line change
@@ -75,6 +75,9 @@ export default class Knapsack {
const knapsackMatrix = Array(numberOfRows).fill(null).map(() => {
return Array(numberOfColumns + 1).fill(null);
});
const isItemTaken = Array(numberOfRows).fill(false).map(() => {
return Array(numberOfColumns + 1).fill(false);
});

// Fill the first column with zeros since it would mean that there is
// no items we can add to knapsack in case if weight limitation is zero.
@@ -89,6 +92,7 @@ export default class Knapsack {
const itemWeight = this.possibleItems[itemIndex].weight;
const itemValue = this.possibleItems[itemIndex].value;
knapsackMatrix[itemIndex][weightIndex] = itemWeight <= weightIndex ? itemValue : 0;
isItemTaken[itemIndex][weightIndex] = (knapsackMatrix[itemIndex][weightIndex] !== 0);
}

// Go through combinations of how we may add items to knapsack and
@@ -111,42 +115,19 @@ export default class Knapsack {
currentItemValue + knapsackMatrix[itemIndex - 1][weightIndex - currentItemWeight],
knapsackMatrix[itemIndex - 1][weightIndex],
);

isItemTaken[itemIndex][weightIndex] = (knapsackMatrix[itemIndex][weightIndex]
!== knapsackMatrix[itemIndex - 1][weightIndex]);
}
}
}

// Now let's trace back the knapsack matrix to see what items we're going to add
// to the knapsack.
let itemIndex = this.possibleItems.length - 1;
let weightIndex = this.weightLimit;

while (itemIndex > 0) {
const currentItem = this.possibleItems[itemIndex];
const prevItem = this.possibleItems[itemIndex - 1];

// Check if matrix value came from top (from previous item).
// In this case this would mean that we need to include previous item
// to the list of selected items.
if (
knapsackMatrix[itemIndex][weightIndex]
&& knapsackMatrix[itemIndex][weightIndex] === knapsackMatrix[itemIndex - 1][weightIndex]
) {
// Check if there are several items with the same weight but with the different values.
// We need to add highest item in the matrix that is possible to get the highest value.
const prevSumValue = knapsackMatrix[itemIndex - 1][weightIndex];
const prevPrevSumValue = knapsackMatrix[itemIndex - 2][weightIndex];
if (
!prevSumValue
|| (prevSumValue && prevPrevSumValue !== prevSumValue)
) {
this.selectedItems.push(prevItem);
}
} else if (knapsackMatrix[itemIndex - 1][weightIndex - currentItem.weight]) {
this.selectedItems.push(prevItem);
weightIndex -= currentItem.weight;
let capacity = this.weightLimit;
for (let itemIndex = this.possibleItems.length - 1; itemIndex >= 0; itemIndex -= 1) {
if (isItemTaken[itemIndex][capacity]) {
this.selectedItems.push(this.possibleItems[itemIndex]);
capacity -= this.possibleItems[itemIndex].weight;
}

itemIndex -= 1;
}
}

21 changes: 21 additions & 0 deletions src/algorithms/sets/knapsack-problem/__test__/Knapsack.test.js
Original file line number Diff line number Diff line change
@@ -23,6 +23,27 @@ describe('Knapsack', () => {
expect(knapsack.selectedItems[1].toString()).toBe('v4 w3 x 1');
});

it('should solve 0/1 knapsack problem in another trivial case', () => {
const possibleKnapsackItems = [
new KnapsackItem({ value: 3, weight: 2 }),
new KnapsackItem({ value: 4, weight: 3 }),
new KnapsackItem({ value: 5, weight: 4 }),
new KnapsackItem({ value: 7, weight: 5 }),
];

const maxKnapsackWeight = 7;

const knapsack = new Knapsack(possibleKnapsackItems, maxKnapsackWeight);

knapsack.solveZeroOneKnapsackProblem();

expect(knapsack.totalValue).toBe(10);
expect(knapsack.totalWeight).toBe(7);
expect(knapsack.selectedItems.length).toBe(2);
expect(knapsack.selectedItems).toContain(possibleKnapsackItems[0]);
expect(knapsack.selectedItems).toContain(possibleKnapsackItems[3]);
});

it('should solve 0/1 knapsack problem regardless of items order', () => {
const possibleKnapsackItems = [
new KnapsackItem({ value: 5, weight: 4 }),