Skip to content

Commit 2881b3f

Browse files
Ethienne GravelineEti
Ethienne Graveline
and
Eti
authored
Integer partition (#24)
* Fixed error in Integer Partition returns the wrong total for some values of n e.g. when n is 6 returns 14 instead of 11 This more closely resembles the code from referenced in the README: https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/integer-partition * added some simple comments * fixed error in Integer Partition on some values it would print wrong partitions e.g. when n is 6 [3,3,1] is printed but is not a valid output * fixed a typo Co-authored-by: Eti <ethiennegraveline@gmail.com>
1 parent 6a80b21 commit 2881b3f

File tree

1 file changed

+44
-31
lines changed
  • Dynamic Programming/Integer Partition

1 file changed

+44
-31
lines changed

Dynamic Programming/Integer Partition/code.js

+44-31
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,61 @@ const logger = new LogTracer();
88
Layout.setRoot(new VerticalLayout([tracer, logger]));
99
const integer = Randomize.Integer({ min: 5, max: 14 });
1010
const D = [];
11-
const A = [];
11+
const A = "";
1212
for (let i = 0; i <= integer; i++) {
1313
D.push([]);
14-
D[0][i] = 1;
15-
D[i][1] = 1;
16-
for (let j = 0; j <= integer; j++) D[i][j] = 0;
14+
D[i][0] = 1
15+
for (let j = 1; j <= integer; j++) D[i][j] = 0;
1716
}
1817
tracer.set(D);
1918
Tracer.delay();
2019
// }
2120

2221
function partition(A, n, p) {
23-
// logger {
24-
if (n === 0) logger.println(`[${A.join(', ')}]`);
25-
// }
26-
else {
27-
let end = n;
28-
if (p !== 0 && A[p - 1] < n) end = A[p - 1];
29-
for (let i = end; i > 0; i--) {
30-
A[p] = i;
31-
partition(A, n - i, p + 1);
22+
// logger {
23+
if (p == 0) logger.println(`[${A.split('').join(', ')}]`);
24+
// }
25+
else {
26+
if (n > 1) partition(A, n - 1, p);
27+
if (n <= p) partition(n + A, n, p - n);
3228
}
33-
}
3429
}
3530

3631
function integerPartition(n) {
37-
// Calculate number of partitions for all numbers from 1 to n
38-
for (let i = 2; i <= n; i++) {
39-
// We are allowed to use numbers from 2 to i
40-
for (let j = 1; j <= i; j++) {
41-
// Number of partitions without j number + number of partitions with max j
42-
// visualize {
43-
tracer.select(i, j);
44-
Tracer.delay();
45-
// }
46-
D[i][j] = D[i][j - 1] + D[i - j][Math.max(j, i - j)];
47-
// visualize {
48-
tracer.patch(i, j, D[i][j]);
49-
Tracer.delay();
50-
tracer.depatch(i, j);
51-
tracer.deselect(i, j);
52-
// }
32+
33+
// cycle through each cell of matrix
34+
for (let i = 1; i <= n; i++) {
35+
for (let j = 1; j <= n; j++) {
36+
if (i > j) {
37+
// visualize {
38+
tracer.select(i, j);
39+
Tracer.delay();
40+
// }
41+
// set cell to cell above it
42+
D[i][j] = D[i - 1][j];
43+
// visualize {
44+
tracer.patch(i, j, D[i][j]);
45+
Tracer.delay();
46+
tracer.depatch(i, j);
47+
tracer.deselect(i, j);
48+
// }
49+
}
50+
else {
51+
// visualize {
52+
tracer.select(i, j);
53+
Tracer.delay();
54+
// }
55+
// grab above cell and add it to previous cell
56+
const above = D[i - 1][j];
57+
const left = D[i][j - i];
58+
D[i][j] = above + left;
59+
// visualize {
60+
tracer.patch(i, j, D[i][j]);
61+
Tracer.delay();
62+
tracer.depatch(i, j);
63+
tracer.deselect(i, j);
64+
// }
65+
}
5366
}
5467
}
5568
return D[n][n];
@@ -58,7 +71,7 @@ function integerPartition(n) {
5871
// logger {
5972
logger.println(`Partitioning: ${integer}`);
6073
// }
61-
partition(A, integer, 0);
74+
partition(A, integer, integer);
6275
const part = integerPartition(integer);
6376
// logger {
6477
logger.println(part);

0 commit comments

Comments
 (0)