Skip to content

Commit d8315c4

Browse files
✨ feat(integerValuesKnapsack): Implement DP on values.
1 parent 3f20fe9 commit d8315c4

7 files changed

+129
-10
lines changed

package.json

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"transform-remove-console",
2828
{
2929
"exclude": [
30+
"debug",
3031
"log",
3132
"error",
3233
"warn"
@@ -75,7 +76,9 @@
7576
"bugs": {
7677
"url": "https://github.com/aureooms/js-knapsack/issues"
7778
},
78-
"dependencies": {},
79+
"dependencies": {
80+
"@aureooms/js-itertools": "^4.1.0"
81+
},
7982
"devDependencies": {
8083
"@babel/cli": "7.11.6",
8184
"@babel/core": "7.11.6",
@@ -101,7 +104,25 @@
101104
"lib"
102105
],
103106
"homepage": "https://aureooms.github.io/js-knapsack",
104-
"keywords": ["agpl", "algorithms", "complete", "computer", "dynamic", "in", "javascript", "knapsack", "meet", "middle", "np", "polynomial", "programming", "pseudo", "reductions", "science", "the"],
107+
"keywords": [
108+
"agpl",
109+
"algorithms",
110+
"complete",
111+
"computer",
112+
"dynamic",
113+
"in",
114+
"javascript",
115+
"knapsack",
116+
"meet",
117+
"middle",
118+
"np",
119+
"polynomial",
120+
"programming",
121+
"pseudo",
122+
"reductions",
123+
"science",
124+
"the"
125+
],
105126
"license": "AGPL-3.0",
106127
"main": "lib/index.js",
107128
"prettier": {

src/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import integerKnapsack from './integerKnapsack';
1+
import integerValuesKnapsack from './integerValuesKnapsack';
2+
import integerWeightsKnapsack from './integerWeightsKnapsack';
23

34
/* eslint import/no-anonymous-default-export: [2, {"allowObject": true}] */
45
export default {
5-
integerKnapsack,
6+
integerValuesKnapsack,
7+
integerWeightsKnapsack,
68
};
79

8-
export {integerKnapsack};
10+
export {integerValuesKnapsack, integerWeightsKnapsack};

src/integerValuesKnapsack.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import assert from 'assert';
2+
import {sum} from '@aureooms/js-itertools';
3+
4+
const integerValuesKnapsack = (
5+
v,
6+
w,
7+
n,
8+
W,
9+
V = sum(v),
10+
m = new w.constructor(V + 1).fill(Number(Infinity)),
11+
) => {
12+
assert(v.length === n);
13+
assert(w.length === n);
14+
assert(Number.isInteger(V) && V >= 0);
15+
assert(m.length >= V + 1);
16+
m[V] = 0;
17+
for (let i = 0; i < n; ++i) {
18+
const wi = w[i];
19+
const vi = v[i];
20+
assert(Number.isInteger(vi) && vi >= 0);
21+
const k = V - vi + 1;
22+
for (let j = 0; j < k; ++j) {
23+
m[j] = Math.min(m[j], m[j + vi] + wi);
24+
}
25+
}
26+
27+
for (let j = 0; j < V; ++j) {
28+
if (m[j] <= W) return V - j;
29+
}
30+
31+
return 0;
32+
};
33+
34+
export default integerValuesKnapsack;

src/integerKnapsack.js renamed to src/integerWeightsKnapsack.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import assert from 'assert';
22

3-
const integerKnapsack = (v, w, n, W, m = new v.constructor(W + 1).fill(0)) => {
3+
const integerWeightsKnapsack = (
4+
v,
5+
w,
6+
n,
7+
W,
8+
m = new v.constructor(W + 1).fill(0),
9+
) => {
410
assert(v.length === n);
511
assert(w.length === n);
612
assert(Number.isInteger(W) && W >= 0);
@@ -18,4 +24,4 @@ const integerKnapsack = (v, w, n, W, m = new v.constructor(W + 1).fill(0)) => {
1824
return m[0];
1925
};
2026

21-
export default integerKnapsack;
27+
export default integerWeightsKnapsack;

test/src/integerKnapsack.js renamed to test/src/integerValuesKnapsack.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import test from 'ava';
22

3-
import {integerKnapsack} from '../../src';
3+
import {integerValuesKnapsack} from '../../src';
44

55
const macro = (t, v, w, n, W, expected) => {
66
t.is(n, v.length);
77
t.is(n, w.length);
8-
const result = integerKnapsack(v, w, n, W);
8+
const result = integerValuesKnapsack(v, w, n, W);
99
t.is(expected, result);
1010
};
1111

1212
macro.title = (title, v, w, n, W, expected) =>
1313
title ||
14-
`integerKnapsack(${JSON.stringify(v)}, ${JSON.stringify(
14+
`integerValuesKnapsack(${JSON.stringify(v)}, ${JSON.stringify(
1515
w,
1616
)}, ${n}, ${W}) = ${expected}`;
1717

test/src/integerWeightsKnapsack.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import test from 'ava';
2+
3+
import {integerWeightsKnapsack} from '../../src';
4+
5+
const macro = (t, v, w, n, W, expected) => {
6+
t.is(n, v.length);
7+
t.is(n, w.length);
8+
const result = integerWeightsKnapsack(v, w, n, W);
9+
t.is(expected, result);
10+
};
11+
12+
macro.title = (title, v, w, n, W, expected) =>
13+
title ||
14+
`integerWeightsKnapsack(${JSON.stringify(v)}, ${JSON.stringify(
15+
w,
16+
)}, ${n}, ${W}) = ${expected}`;
17+
18+
test(
19+
'wikipedia 0-1 knapsack example',
20+
macro,
21+
[505, 352, 458, 220, 354, 414, 498, 545, 473, 543],
22+
[23, 26, 20, 18, 32, 27, 29, 26, 30, 27],
23+
10,
24+
67,
25+
1270,
26+
);
27+
28+
test(
29+
'https://www.geeksforgeeks.org/0-1-knapsack-problem-dp-10',
30+
macro,
31+
[60, 100, 120],
32+
[10, 20, 30],
33+
3,
34+
50,
35+
220,
36+
);

yarn.lock

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,26 @@
22
# yarn lockfile v1
33

44

5+
"@aureooms/js-collections-deque@^4.0.1":
6+
version "4.0.1"
7+
resolved "https://registry.yarnpkg.com/@aureooms/js-collections-deque/-/js-collections-deque-4.0.1.tgz#bd8b827cdb597d061525c380dd704e338c77bd5f"
8+
integrity sha512-dktRVLyep0sUKpqgtG/JseA232KMdlu9t9ePq2wHcng1dZ3+63qW2C+FlKXBktrT1dU92DDxIL6PqnV97kHA1Q==
9+
dependencies:
10+
"@aureooms/js-error" "^4.0.1"
11+
12+
"@aureooms/js-error@^4.0.1":
13+
version "4.0.1"
14+
resolved "https://registry.yarnpkg.com/@aureooms/js-error/-/js-error-4.0.1.tgz#be2740b3f31a337e0828930c9b70d38113b4dd77"
15+
integrity sha512-zsgs6wmnRsKljlusewYiBaFBM/hIJp43b7OwE9ybpLEL4wfXjGP17cJTTAY8gpzALVFwEA5/flxIK0I6gJJLHQ==
16+
17+
"@aureooms/js-itertools@^4.1.0":
18+
version "4.1.0"
19+
resolved "https://registry.yarnpkg.com/@aureooms/js-itertools/-/js-itertools-4.1.0.tgz#c731eaee7de0005c6ea41f2f87cca4bdb2d86ca1"
20+
integrity sha512-P9TfwQPuXk5T5z6QMMfwLLyzh9QsEu4sGWcBk/P/waWgim/xqr+LDTOHVo7WU4QAty+/5CBX8DuW7MrPVfXt5A==
21+
dependencies:
22+
"@aureooms/js-collections-deque" "^4.0.1"
23+
"@aureooms/js-error" "^4.0.1"
24+
525
"@babel/cli@7.11.6":
626
version "7.11.6"
727
resolved "https://registry.yarnpkg.com/@babel/cli/-/cli-7.11.6.tgz#1fcbe61c2a6900c3539c06ee58901141f3558482"

0 commit comments

Comments
 (0)