Skip to content

Commit

Permalink
Release 2.2.6
Browse files Browse the repository at this point in the history
  • Loading branch information
jdfekete committed Aug 18, 2023
1 parent 967fb09 commit f7a2772
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 40 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "reorder.js",
"version": "2.2.5",
"version": "2.2.6",
"description": "Matrix reordering in JavaScript.",
"keywords": [
"es6",
Expand Down
8 changes: 4 additions & 4 deletions src/bandwidth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
32 changes: 15 additions & 17 deletions src/distance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,20 +14,20 @@ 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]);
}
}
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);
}
Expand All @@ -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) {
Expand All @@ -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++;
Expand All @@ -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++;
Expand All @@ -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)) {
Expand All @@ -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++) {
Expand All @@ -119,4 +118,3 @@ distance.morans = function(matrix) {
return -1 * result;
};
};

18 changes: 11 additions & 7 deletions src/nn_2opt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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];
Expand All @@ -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;
}
Expand Down
18 changes: 9 additions & 9 deletions test/distance-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,43 @@ 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() {
const a = [3, 0];
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() {
Expand All @@ -59,8 +59,8 @@ suite.addBatch({
];
const dist = reorder.distance.morans(mat);
// TODO
}
}
},
},
});

suite.export(module);

0 comments on commit f7a2772

Please sign in to comment.