From 33378b17b0183ac84aa6aca05cb317ef1fd02d28 Mon Sep 17 00:00:00 2001 From: Eti Date: Fri, 22 May 2020 18:46:58 -0300 Subject: [PATCH 1/4] 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 --- Dynamic Programming/Integer Partition/code.js | 54 ++++++++++++------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/Dynamic Programming/Integer Partition/code.js b/Dynamic Programming/Integer Partition/code.js index 3618098c..7ead28d7 100644 --- a/Dynamic Programming/Integer Partition/code.js +++ b/Dynamic Programming/Integer Partition/code.js @@ -6,14 +6,13 @@ const { Tracer, Array2DTracer, LogTracer, Randomize, Layout, VerticalLayout } = const tracer = new Array2DTracer(); const logger = new LogTracer(); Layout.setRoot(new VerticalLayout([tracer, logger])); -const integer = Randomize.Integer({ min: 5, max: 14 }); +const integer = Randomize.Integer({ min: 5, max: 14 });; const D = []; const A = []; for (let i = 0; i <= integer; i++) { D.push([]); - D[0][i] = 1; - D[i][1] = 1; - for (let j = 0; j <= integer; j++) D[i][j] = 0; + D[i][0] = 1 + for (let j = 1; j <= integer; j++) D[i][j] = 0; } tracer.set(D); Tracer.delay(); @@ -34,22 +33,37 @@ function partition(A, n, p) { } function integerPartition(n) { - // Calculate number of partitions for all numbers from 1 to n - for (let i = 2; i <= n; i++) { - // We are allowed to use numbers from 2 to i - for (let j = 1; j <= i; j++) { - // Number of partitions without j number + number of partitions with max j - // visualize { - tracer.select(i, j); - Tracer.delay(); - // } - D[i][j] = D[i][j - 1] + D[i - j][Math.max(j, i - j)]; - // visualize { - tracer.patch(i, j, D[i][j]); - Tracer.delay(); - tracer.depatch(i, j); - tracer.deselect(i, j); - // } + + for (let i = 1; i <= n; i++) { + for (let j = 1; j <= n; j++) { + if (i > j) { + // visualize { + tracer.select(i, j); + Tracer.delay(); + // } + D[i][j] = D[i - 1][j]; + // visualize { + tracer.patch(i, j, D[i][j]); + Tracer.delay(); + tracer.depatch(i, j); + tracer.deselect(i, j); + // } + } + else { + // visualize { + tracer.select(i, j); + Tracer.delay(); + // } + const left = D[i - 1][j]; + const above = D[i][j - i]; + D[i][j] = left + above; + // visualize { + tracer.patch(i, j, D[i][j]); + Tracer.delay(); + tracer.depatch(i, j); + tracer.deselect(i, j); + // } + } } } return D[n][n]; From 4e3f6e667901eeb49b0224507ac1025d249619f8 Mon Sep 17 00:00:00 2001 From: Eti Date: Fri, 22 May 2020 18:52:59 -0300 Subject: [PATCH 2/4] added some simple comments --- Dynamic Programming/Integer Partition/code.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Dynamic Programming/Integer Partition/code.js b/Dynamic Programming/Integer Partition/code.js index 7ead28d7..e35c272d 100644 --- a/Dynamic Programming/Integer Partition/code.js +++ b/Dynamic Programming/Integer Partition/code.js @@ -34,6 +34,7 @@ function partition(A, n, p) { function integerPartition(n) { + // cycle through each cell of matrix for (let i = 1; i <= n; i++) { for (let j = 1; j <= n; j++) { if (i > j) { @@ -41,6 +42,7 @@ function integerPartition(n) { tracer.select(i, j); Tracer.delay(); // } + // set cell to cell above it D[i][j] = D[i - 1][j]; // visualize { tracer.patch(i, j, D[i][j]); @@ -54,9 +56,10 @@ function integerPartition(n) { tracer.select(i, j); Tracer.delay(); // } - const left = D[i - 1][j]; - const above = D[i][j - i]; - D[i][j] = left + above; + // grab above cell and add it to previous cell + const above = D[i - 1][j]; + const left = D[i][j - i]; + D[i][j] = above + left; // visualize { tracer.patch(i, j, D[i][j]); Tracer.delay(); From ff6676b82dc9814ff38654dcb09183566e5f8ec7 Mon Sep 17 00:00:00 2001 From: Eti Date: Fri, 22 May 2020 19:46:49 -0300 Subject: [PATCH 3/4] 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 --- Dynamic Programming/Integer Partition/code.js | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/Dynamic Programming/Integer Partition/code.js b/Dynamic Programming/Integer Partition/code.js index e35c272d..100d7291 100644 --- a/Dynamic Programming/Integer Partition/code.js +++ b/Dynamic Programming/Integer Partition/code.js @@ -8,7 +8,7 @@ const logger = new LogTracer(); Layout.setRoot(new VerticalLayout([tracer, logger])); const integer = Randomize.Integer({ min: 5, max: 14 });; const D = []; -const A = []; +const A = ""; for (let i = 0; i <= integer; i++) { D.push([]); D[i][0] = 1 @@ -19,17 +19,13 @@ Tracer.delay(); // } function partition(A, n, p) { - // logger { - if (n === 0) logger.println(`[${A.join(', ')}]`); - // } - else { - let end = n; - if (p !== 0 && A[p - 1] < n) end = A[p - 1]; - for (let i = end; i > 0; i--) { - A[p] = i; - partition(A, n - i, p + 1); + // logger { + if (p == 0) logger.println(`[${A.split('').join(', ')}]`); + // } + else { + if (n > 1) partition(A, n - 1, p); + if (n <= p) partition(n + A, n, p - n); } - } } function integerPartition(n) { @@ -75,7 +71,7 @@ function integerPartition(n) { // logger { logger.println(`Partitioning: ${integer}`); // } -partition(A, integer, 0); +partition(A, integer, integer); const part = integerPartition(integer); // logger { logger.println(part); From 754d22386e366d99176899feec68709051a6afb6 Mon Sep 17 00:00:00 2001 From: Eti Date: Fri, 22 May 2020 19:51:40 -0300 Subject: [PATCH 4/4] fixed a typo --- Dynamic Programming/Integer Partition/code.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dynamic Programming/Integer Partition/code.js b/Dynamic Programming/Integer Partition/code.js index 100d7291..da4d0380 100644 --- a/Dynamic Programming/Integer Partition/code.js +++ b/Dynamic Programming/Integer Partition/code.js @@ -6,7 +6,7 @@ const { Tracer, Array2DTracer, LogTracer, Randomize, Layout, VerticalLayout } = const tracer = new Array2DTracer(); const logger = new LogTracer(); Layout.setRoot(new VerticalLayout([tracer, logger])); -const integer = Randomize.Integer({ min: 5, max: 14 });; +const integer = Randomize.Integer({ min: 5, max: 14 }); const D = []; const A = ""; for (let i = 0; i <= integer; i++) {