diff --git a/package-lock.json b/package-lock.json index 7328232..2cb530b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "reorder.js", - "version": "2.2.5", + "version": "2.2.6", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "reorder.js", - "version": "2.2.5", + "version": "2.2.6", "license": "BSD-2-Clause", "dependencies": { "@sgratzl/science": "^2.0.0" diff --git a/package.json b/package.json index 38b617f..37bfd40 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "reorder.js", - "version": "2.2.5", + "version": "2.2.6", "description": "Matrix reordering in JavaScript.", "keywords": [ "es6", diff --git a/src/bandwidth.js b/src/bandwidth.js index 5408554..033b3f2 100644 --- a/src/bandwidth.js +++ b/src/bandwidth.js @@ -3,9 +3,9 @@ import { inverse_permutation } from './permutation'; /** * Compute the bandwidth of a graph, given and order. - * @param {Graph} graph - the graph + * @param {Graph} graph - the graph * @param {list} order - a permutation - * @returns {integer} the bandwidth + * @returns {integer} the bandwidth */ export function bandwidth(graph, order) { if (!order) { @@ -26,10 +26,10 @@ export function bandwidth(graph, order) { /** * Compute the bandwidth of an adjacency matrix, - * i.e. the maximum distace between two endpoints + * i.e. the maximum distace between two endpoints * over all edges. * @param {Matrix} matrix - the matrix - * @returns {integer} the bandwidth + * @returns {integer} the bandwidth */ export function bandwidth_matrix(matrix) { let max = 0; diff --git a/src/distance.js b/src/distance.js index 01d01da..4ccf435 100644 --- a/src/distance.js +++ b/src/distance.js @@ -3,9 +3,9 @@ function isNum(a, b) { } export const distance = {}; -distance.euclidean = function(a, b) { +distance.euclidean = function (a, b) { let s = 0; - for (let i = a.length-1; i >= 0; i--) { + for (let i = a.length - 1; i >= 0; i--) { if (isNum(a[i], b[i])) { const x = a[i] - b[i]; s += x * x; @@ -14,9 +14,9 @@ distance.euclidean = function(a, b) { return Math.sqrt(s); }; -distance.manhattan = function(a, b) { +distance.manhattan = function (a, b) { let s = 0; - for (let i = a.length-1; i >= 0; i--) { + for (let i = a.length - 1; i >= 0; i--) { if (isNum(a[i], b[i])) { s += Math.abs(a[i] - b[i]); } @@ -24,10 +24,10 @@ distance.manhattan = function(a, b) { return s; }; -distance.minkowski = function(p) { +distance.minkowski = function (p) { return (a, b) => { let s = 0; - for (let i = a.length-1; i >= 0; i--) { + for (let i = a.length - 1; i >= 0; i--) { if (isNum(a[i], b[i])) { s += Math.pow(Math.abs(a[i] - b[i]), p); } @@ -36,10 +36,9 @@ distance.minkowski = function(p) { }; }; - -distance.chebyshev = function(a, b) { +distance.chebyshev = function (a, b) { let max = 0; - for (let i = a.length-1; i >= 0; i--) { + for (let i = a.length - 1; i >= 0; i--) { if (isNum(a[i], b[i])) { const x = Math.abs(a[i] - b[i]); if (x > max) { @@ -50,9 +49,9 @@ distance.chebyshev = function(a, b) { return max; }; -distance.hamming = function(a, b) { +distance.hamming = function (a, b) { let d = 0; - for (let i = a.length-1; i >= 0; i--) { + for (let i = a.length - 1; i >= 0; i--) { if (isNum(a[i], b[i])) { if (a[i] !== b[i]) { d++; @@ -62,10 +61,10 @@ distance.hamming = function(a, b) { return d; }; -distance.jaccard = function(a, b) { +distance.jaccard = function (a, b) { let n = 0; let s = 0; - for (let i = a.length-1; i >= 0; i--) { + for (let i = a.length - 1; i >= 0; i--) { if (isNum(a[i], b[i])) { if (a[i] === b[i]) { s++; @@ -79,10 +78,10 @@ distance.jaccard = function(a, b) { return s / n; }; -distance.braycurtis = function(a, b) { +distance.braycurtis = function (a, b) { let s0 = 0; let s1 = 0; - for (let i = a.length-1; i >= 0; i--) { + for (let i = a.length - 1; i >= 0; i--) { const ai = a[i]; const bi = b[i]; if (isNum(ai, bi)) { @@ -101,7 +100,7 @@ distance.braycurtis = function(a, b) { // // N. van Beusekom, W. Meulemans, B. Speckmann, Simultaneous Orderings for Graph Collections // IEEE Transactions on Visualization and Computer Graphics, vol. 28, no. 1, pp. 1-10, Jan. 2022 -distance.morans = function(matrix) { +distance.morans = function (matrix) { let m = 0; const n = matrix.length * matrix[0].length; for (let i = 0; i < matrix.length; i++) { @@ -119,4 +118,3 @@ distance.morans = function(matrix) { return -1 * result; }; }; - diff --git a/src/nn_2opt.js b/src/nn_2opt.js index 97b56e1..ef8e438 100644 --- a/src/nn_2opt.js +++ b/src/nn_2opt.js @@ -30,7 +30,7 @@ export function nn_2opt() { } let lowest_dist_all = Number.MAX_VALUE; let best_order_all = []; - + // Try each row as the initial permutation for NN-2OPT for (let s = 0; s < distanceMatrix.length; s++) { let order = []; @@ -61,9 +61,13 @@ export function nn_2opt() { for (let i = 0; i < order.length; i++) { for (let j = i + 2; j < order.length - 1; j++) { // edge 1: (i,i+1) edge2: (j,j+1) - let currentd = distanceMatrix[order[i]][order[i + 1]] + distanceMatrix[order[j]][order[j + 1]]; - let candidated = distanceMatrix[order[i]][order[j]] + distanceMatrix[order[i + 1]][order[j + 1]]; - if (candidated < currentd) { + let currentd = + distanceMatrix[order[i]][order[i + 1]] + + distanceMatrix[order[j]][order[j + 1]]; + let candidated = + distanceMatrix[order[i]][order[j]] + + distanceMatrix[order[i + 1]][order[j + 1]]; + if (candidated < currentd) { let check = []; for (let k = 0; k < order.length; k++) { check[k] = order[k]; @@ -84,9 +88,9 @@ export function nn_2opt() { } } if (lowest_dist_2opt < lowest_dist_all) { - lowest_dist_all = lowest_dist_2opt; - best_order_all = best_order_2opt; - } + lowest_dist_all = lowest_dist_2opt; + best_order_all = best_order_2opt; + } } return best_order_all; } diff --git a/test/distance-test.js b/test/distance-test.js index 65f17d1..f648052 100644 --- a/test/distance-test.js +++ b/test/distance-test.js @@ -11,14 +11,14 @@ suite.addBatch({ const a = [3, 0]; const b = [0, 4]; assert.equal(reorder.distance.euclidean(a, b), 5); - } + }, }, manhattan: { simple() { const a = [3, 0]; const b = [0, 4]; assert.equal(reorder.distance.manhattan(a, b), 7); - } + }, }, minkowski: { simple() { @@ -26,28 +26,28 @@ suite.addBatch({ const b = [0, 4]; assert.equal(reorder.distance.minkowski(1)(a, b), 7); assert.equal(reorder.distance.minkowski(2)(a, b), 5); - } + }, }, chebyshev: { simple() { const a = [3, 0]; const b = [0, 4]; assert.equal(reorder.distance.chebyshev(a, b), 4); - } + }, }, hamming: { simple() { const a = [3, 0, 2]; const b = [0, 4, 2]; - assert.equal(reorder.distance.jaccard(a, b), 1/3); - } + assert.equal(reorder.distance.jaccard(a, b), 1 / 3); + }, }, braycurtis: { simple() { const a = [1, 0, 1]; const b = [0, 1, 0]; assert.equal(reorder.distance.braycurtis(a, b), 1); - } + }, }, morans: { simple() { @@ -59,8 +59,8 @@ suite.addBatch({ ]; const dist = reorder.distance.morans(mat); // TODO - } - } + }, + }, }); suite.export(module);