Skip to content

Commit

Permalink
Merge pull request #25 from curran/manual-es6-cleanup
Browse files Browse the repository at this point in the history
Manual es6 cleanup
  • Loading branch information
jdfekete authored Jan 2, 2021
2 parents f817ee8 + 86d1bb5 commit 9754f23
Show file tree
Hide file tree
Showing 60 changed files with 803 additions and 1,012 deletions.
43 changes: 16 additions & 27 deletions src/adjacent_exchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@ import { inverse_permutation } from './permutation';
function count_in_crossings(graph, v, w, inv) {
const v_edges = graph.inEdges(v);
const w_edges = graph.inEdges(w);
let iv;
let iw;
let p0;
let cross = 0;

for (iw = 0; iw < w_edges.length; iw++) {
p0 = inv[w_edges[iw].target.index];
for (iv = 0; iv < v_edges.length; iv++) {
for (let iw = 0; iw < w_edges.length; iw++) {
const p0 = inv[w_edges[iw].target.index];
for (let iv = 0; iv < v_edges.length; iv++) {
if (inv[v_edges[iv].target.index] > p0) cross++;
}
}
Expand All @@ -30,14 +27,11 @@ function count_in_crossings(graph, v, w, inv) {
function count_out_crossings(graph, v, w, inv) {
const v_edges = graph.outEdges(v);
const w_edges = graph.outEdges(w);
let iv;
let iw;
let p0;
let cross = 0;

for (iw = 0; iw < w_edges.length; iw++) {
p0 = inv[w_edges[iw].source.index];
for (iv = 0; iv < v_edges.length; iv++) {
for (let iw = 0; iw < w_edges.length; iw++) {
const p0 = inv[w_edges[iw].source.index];
for (let iv = 0; iv < v_edges.length; iv++) {
if (inv[v_edges[iv].source.index] > p0) cross++;
}
}
Expand All @@ -55,25 +49,20 @@ function count_out_crossings(graph, v, w, inv) {
export function adjacent_exchange(graph, layer1, layer2) {
layer1 = layer1.slice();
layer2 = layer2.slice();
let i;
let v;
let w;
let c0;
let c1;
const inv_layer1 = inverse_permutation(layer1);
const inv_layer2 = inverse_permutation(layer2);
let swapped = true;
let improved = 0;

while (swapped) {
swapped = false;
for (i = 0; i < layer1.length - 1; i++) {
v = layer1[i];
w = layer1[i + 1];
for (let i = 0; i < layer1.length - 1; i++) {
const v = layer1[i];
const w = layer1[i + 1];
// should reduce the in crossing and the out crossing
// otherwise what we gain horizontally is lost vertically
c0 = count_out_crossings(graph, v, w, inv_layer2);
c1 = count_out_crossings(graph, w, v, inv_layer2);
const c0 = count_out_crossings(graph, v, w, inv_layer2);
const c1 = count_out_crossings(graph, w, v, inv_layer2);
if (c0 > c1) {
layer1[i] = w;
layer1[i + 1] = v;
Expand All @@ -83,11 +72,11 @@ export function adjacent_exchange(graph, layer1, layer2) {
improved += c0 - c1;
}
}
for (i = 0; i < layer2.length - 1; i++) {
v = layer2[i];
w = layer2[i + 1];
c0 = count_in_crossings(graph, v, w, inv_layer1);
c1 = count_in_crossings(graph, w, v, inv_layer1);
for (let i = 0; i < layer2.length - 1; i++) {
const v = layer2[i];
const w = layer2[i + 1];
const c0 = count_in_crossings(graph, v, w, inv_layer1);
const c1 = count_in_crossings(graph, w, v, inv_layer1);
if (c0 > c1) {
layer2[i] = w;
layer2[i + 1] = v;
Expand Down
45 changes: 20 additions & 25 deletions src/all_pairs_distance.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,29 @@ export function all_pairs_distance(graph, comps) {
*/
export function all_pairs_distance_floyd_warshall(graph, comp) {
const dist = infinities(comp.length, comp.length);
let i;
let j;
let k;
let inv;
// Floyd Warshall,
// see http://ai-depot.com/BotNavigation/Path-AllPairs.html
// O(n^3) unfortunately

inv = inverse_permutation(comp);
const inv = inverse_permutation(comp);

for (i = 0; i < comp.length; i++) dist[i][i] = 0;
for (let i = 0; i < comp.length; i++) dist[i][i] = 0;

const build_dist = (e) => {
if (e.source == e.target) return;
if (!(e.source.index in inv) || !(e.target.index in inv)) return; // ignore edges outside of comp
const u = inv[e.source.index],
v = inv[e.target.index];
const u = inv[e.source.index];
const v = inv[e.target.index];
dist[v][u] = dist[u][v] = graph.distance(e.index);
};
for (i = 0; i < comp.length; i++) {
for (let i = 0; i < comp.length; i++) {
graph.edges(comp[i]).forEach(build_dist);
}

for (k = 0; k < comp.length; k++) {
for (i = 0; i < comp.length; i++)
for (let k = 0; k < comp.length; k++) {
for (let i = 0; i < comp.length; i++)
if (dist[i][k] != Infinity) {
for (j = 0; j < comp.length; j++)
for (let j = 0; j < comp.length; j++)
if (dist[k][j] != Infinity && dist[i][j] > dist[i][k] + dist[k][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
dist[j][i] = dist[i][j];
Expand Down Expand Up @@ -83,25 +79,22 @@ export function floyd_warshall_with_path(graph, comp) {
const dist = infinities(comp.length, comp.length);
const next = Array(comp.length);
const directed = graph.directed();
let i;
let j;
let k;
let inv;

// Floyd Warshall,
// see http://ai-depot.com/BotNavigation/Path-AllPairs.html
// O(n^3) unfortunately

inv = inverse_permutation(comp);
const inv = inverse_permutation(comp);

for (i = 0; i < comp.length; i++) {
for (let i = 0; i < comp.length; i++) {
dist[i][i] = 0;
next[i] = Array(comp.length);
}

const build_dist = (e) => {
if (e.source == e.target) return;
const u = inv[e.source.index],
v = inv[e.target.index];
const u = inv[e.source.index];
const v = inv[e.target.index];
dist[u][v] = graph.distance(e);
next[u][v] = v;
if (!directed) {
Expand All @@ -110,13 +103,13 @@ export function floyd_warshall_with_path(graph, comp) {
}
};

for (i = 0; i < comp.length; i++) {
for (let i = 0; i < comp.length; i++) {
graph.edges(comp[i]).forEach(build_dist);
}

for (k = 0; k < comp.length; k++) {
for (i = 0; i < comp.length; i++) {
for (j = 0; j < comp.length; j++) {
for (let k = 0; k < comp.length; k++) {
for (let i = 0; i < comp.length; i++) {
for (let j = 0; j < comp.length; j++) {
if (dist[i][j] > dist[i][k] + dist[k][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
next[i][j] = next[i][k];
Expand All @@ -140,7 +133,9 @@ export function floyd_warshall_with_path(graph, comp) {
* @return {list} a list of nodes in the shortest path from u to v
*/
export function floyd_warshall_path(next, u, v) {
if (next[u][v] === undefined) return [];
if (next[u][v] === undefined) {
return [];
}
const path = [u];
while (u != v) {
u = next[u][v];
Expand Down
9 changes: 3 additions & 6 deletions src/bandwidth.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ export function bandwidth(graph, order) {

const inv = inverse_permutation(order);
const links = graph.links();
let i;
let e;
let d;
let max = 0;

for (i = 0; i < links.length; i++) {
e = links[i];
d = Math.abs(inv[e.source.index] - inv[e.target.index]);
for (let i = 0; i < links.length; i++) {
const e = links[i];
const d = Math.abs(inv[e.source.index] - inv[e.target.index]);
max = Math.max(max, d);
}
return max;
Expand Down
70 changes: 31 additions & 39 deletions src/barycenter_order.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,81 +25,73 @@ function median(neighbors) {
if (neighbors.length === 2) return (neighbors[0] + neighbors[1]) / 2;
neighbors.sort(cmp_number);
if (neighbors.length % 2) return neighbors[(neighbors.length - 1) / 2];
const rm = neighbors.length / 2,
lm = rm - 1,
rspan = neighbors[neighbors.length - 1] - neighbors[rm],
lspan = neighbors[lm] - neighbors[0];
const rm = neighbors.length / 2;
const lm = rm - 1;
const rspan = neighbors[neighbors.length - 1] - neighbors[rm];
const lspan = neighbors[lm] - neighbors[0];
if (lspan == rspan) return (neighbors[lm] + neighbors[rm]) / 2;
else return (neighbors[lm] * rspan + neighbors[rm] * lspan) / (lspan + rspan);
}

export function barycenter(graph, comp, max_iter) {
const nodes = graph.nodes();
let layer1;
let layer2;
let crossings;
let iter;
let best_layer1;
let best_layer2;
let best_crossings;
let best_iter;
let layer;
let inv_layer = {};
let i;
let v;
let neighbors;
let med;

layer1 = comp.filter((n) => graph.outDegree(n) !== 0);
layer2 = comp.filter((n) => graph.inDegree(n) !== 0);
const layer1 = comp.filter((n) => graph.outDegree(n) !== 0);
const layer2 = comp.filter((n) => graph.inDegree(n) !== 0);
if (comp.length < 3) {
return [layer1, layer2, count_crossings(graph, layer1, layer2)];
}

if (!max_iter) max_iter = 24;
else if (max_iter % 2 == 1) max_iter++; // want even number of iterations

inv_layer = inverse_permutation(layer2);
let inv_layer = inverse_permutation(layer2);

let best_crossings = count_crossings(graph, layer1, layer2);
let best_layer1 = layer1.slice();
let best_layer2 = layer2.slice();
let best_iter = 0;

best_crossings = count_crossings(graph, layer1, layer2);
best_layer1 = layer1.slice();
best_layer2 = layer2.slice();
best_iter = 0;
const inv_neighbor = (e) => {
const n = e.source == v ? e.target : e.source;
return inv_layer[n.index];
},
barycenter_sort = (a, b) => {
let d = med[a] - med[b];
if (d === 0) {
// If both values are equal,
// place the odd degree vertex on the left of the even
// degree vertex
d = (graph.edges(b).length % 2) - (graph.edges(a).length % 2);
}
if (d < 0) return -1;
else if (d > 0) return 1;
return 0;
};
let v;
const inv_neighbor = (e) =>
inv_layer[e.source == v ? e.target : e.source.index];

const barycenter_sort = (a, b) => {
let d = med[a] - med[b];
if (d === 0) {
// If both values are equal,
// place the odd degree vertex on the left of the even
// degree vertex
d = (graph.edges(b).length % 2) - (graph.edges(a).length % 2);
}
if (d < 0) return -1;
else if (d > 0) return 1;
return 0;
};

for (
layer = layer1, iter = 0;
iter < max_iter;
iter++, layer = layer == layer1 ? layer2 : layer1
) {
med = {};
for (i = 0; i < layer.length; i++) {
for (let i = 0; i < layer.length; i++) {
// Compute the median/barycenter for this node and set
// its (real) value into node.pos
v = nodes[layer[i]];
if (layer == layer1) neighbors = graph.outEdges(v.index);
else neighbors = graph.inEdges(v.index);
neighbors = neighbors.map(inv_neighbor);
med[v.index] = +median(neighbors);
//console.log('median['+i+']='+med[v.index]);
}
layer.sort(barycenter_sort);
for (i = 0; i < layer.length; i++) inv_layer = inverse_permutation(layer);
for (let i = 0; i < layer.length; i++)
inv_layer = inverse_permutation(layer);
crossings = count_crossings(graph, layer1, layer2);
if (crossings < best_crossings) {
best_crossings = crossings;
Expand Down
26 changes: 11 additions & 15 deletions src/bfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@ import { flatten, cmp_number } from './utils';
export function bfs(graph, v, fn) {
const q = new Queue();
const discovered = {};
let i;
let e;
let v2;
let edges;
q.push(v);
discovered[v] = true;
fn(v, undefined);
while (q.length) {
v = q.shift();
fn(v, v);
edges = graph.edges(v);
for (i = 0; i < edges.length; i++) {
e = edges[i];
v2 = graph.other(e, v).index;
const edges = graph.edges(v);
for (let i = 0; i < edges.length; i++) {
const e = edges[i];
const v2 = graph.other(e, v).index;
if (!discovered[v2]) {
q.push(v2);
discovered[v2] = true;
Expand All @@ -41,18 +37,18 @@ export function all_pairs_distance_bfs(graph, comps) {
if (!comps) comps = [graph.nodes_indices()];
const nodes = comps.reduce(flatten).sort(cmp_number);
const mat = Array(nodes.length);
let i;
let j;
let dist;

for (i = 0; i < nodes.length; i++) mat[i] = Array(nodes.length);
for (let i = 0; i < nodes.length; i++) {
mat[i] = Array(nodes.length);
}

for (i = 0; i < nodes.length; i++) {
dist = bfs_distances(graph, i);
for (j in dist) {
for (let i = 0; i < nodes.length; i++) {
const dist = bfs_distances(graph, i);
for (let j in dist) {
mat[i][j] = dist[j];
mat[j][i] = dist[j];
}
}

return mat;
}
7 changes: 3 additions & 4 deletions src/bfs_order.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import { bfs } from './bfs';
export const bfs_order = (graph, comps) => {
if (!comps) comps = graph.components();

let i;
let comp;
const order = [];

for (i = 0; i < comps.length; i++) {
comp = comps[i];
for (let i = 0; i < comps.length; i++) {
const comp = comps[i];
bfs(graph, comp[0], (v, c) => {
if (c >= 0 && v != c) order.push(v);
});
}

return order;
};
Loading

0 comments on commit 9754f23

Please sign in to comment.