Skip to content

Commit

Permalink
feat: reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
FauxFaux committed Apr 28, 2021
1 parent 1362849 commit 902129a
Show file tree
Hide file tree
Showing 41 changed files with 1,476 additions and 1,257 deletions.
6 changes: 6 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"presets": [
["@babel/preset-env", { "targets": { "node": "current" } }],
"@babel/preset-typescript"
]
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/node_modules
/.vscode
.idea/
dist/
5 changes: 5 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"arrowParens": "always",
"trailingComma": "all",
"singleQuote": true
}
4 changes: 2 additions & 2 deletions lib/alg/components.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var _ = require("../lodash");
var _ = require('../lodash');

module.exports = components;

Expand All @@ -15,7 +15,7 @@ function components(g) {
_.each(g.predecessors(v), dfs);
}

_.each(g.nodes(), function(v) {
_.each(g.nodes(), function (v) {
cmpt = [];
dfs(v);
if (cmpt.length) {
Expand Down
18 changes: 11 additions & 7 deletions lib/alg/dfs.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var _ = require("../lodash");
var _ = require('../lodash');

module.exports = dfs;

Expand All @@ -19,12 +19,12 @@ function dfs(g, vs, order) {

var acc = [];
var visited = {};
_.each(vs, function(v) {
_.each(vs, function (v) {
if (!g.hasNode(v)) {
throw new Error("Graph does not have node: " + v);
throw new Error('Graph does not have node: ' + v);
}

doDfs(g, v, order === "post", visited, navigation, acc);
doDfs(g, v, order === 'post', visited, navigation, acc);
});
return acc;
}
Expand All @@ -33,10 +33,14 @@ function doDfs(g, v, postorder, visited, navigation, acc) {
if (!_.has(visited, v)) {
visited[v] = true;

if (!postorder) { acc.push(v); }
_.each(navigation(v), function(w) {
if (!postorder) {
acc.push(v);
}
_.each(navigation(v), function (w) {
doDfs(g, w, postorder, visited, navigation, acc);
});
if (postorder) { acc.push(v); }
if (postorder) {
acc.push(v);
}
}
}
14 changes: 9 additions & 5 deletions lib/alg/dijkstra-all.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
var dijkstra = require("./dijkstra");
var _ = require("../lodash");
var dijkstra = require('./dijkstra');
var _ = require('../lodash');

module.exports = dijkstraAll;

function dijkstraAll(g, weightFunc, edgeFunc) {
return _.transform(g.nodes(), function(acc, v) {
acc[v] = dijkstra(g, v, weightFunc, edgeFunc);
}, {});
return _.transform(
g.nodes(),
function (acc, v) {
acc[v] = dijkstra(g, v, weightFunc, edgeFunc);
},
{},
);
}
27 changes: 19 additions & 8 deletions lib/alg/dijkstra.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
var _ = require("../lodash");
var PriorityQueue = require("../data/priority-queue");
var _ = require('../lodash');
var PriorityQueue = require('../data/priority-queue');

module.exports = dijkstra;

var DEFAULT_WEIGHT_FUNC = _.constant(1);

function dijkstra(g, source, weightFn, edgeFn) {
return runDijkstra(g, String(source),
return runDijkstra(
g,
String(source),
weightFn || DEFAULT_WEIGHT_FUNC,
edgeFn || function(v) { return g.outEdges(v); });
edgeFn ||
function (v) {
return g.outEdges(v);
},
);
}

function runDijkstra(g, source, weightFn, edgeFn) {
var results = {};
var pq = new PriorityQueue();
var v, vEntry;

var updateNeighbors = function(edge) {
var updateNeighbors = function (edge) {
var w = edge.v !== v ? edge.v : edge.w;
var wEntry = results[w];
var weight = weightFn(edge);
var distance = vEntry.distance + weight;

if (weight < 0) {
throw new Error("dijkstra does not allow negative edge weights. " +
"Bad edge: " + edge + " Weight: " + weight);
throw new Error(
'dijkstra does not allow negative edge weights. ' +
'Bad edge: ' +
edge +
' Weight: ' +
weight,
);
}

if (distance < wEntry.distance) {
Expand All @@ -34,7 +45,7 @@ function runDijkstra(g, source, weightFn, edgeFn) {
}
};

g.nodes().forEach(function(v) {
g.nodes().forEach(function (v) {
var distance = v === source ? 0 : Number.POSITIVE_INFINITY;
results[v] = { distance: distance };
pq.add(v, distance);
Expand Down
10 changes: 6 additions & 4 deletions lib/alg/find-cycles.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
var _ = require("../lodash");
var tarjan = require("./tarjan");
var _ = require('../lodash');
var tarjan = require('./tarjan');

module.exports = findCycles;

function findCycles(g) {
return _.filter(tarjan(g), function(cmpt) {
return cmpt.length > 1 || (cmpt.length === 1 && g.hasEdge(cmpt[0], cmpt[0]));
return _.filter(tarjan(g), function (cmpt) {
return (
cmpt.length > 1 || (cmpt.length === 1 && g.hasEdge(cmpt[0], cmpt[0]))
);
});
}
23 changes: 14 additions & 9 deletions lib/alg/floyd-warshall.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,44 @@
var _ = require("../lodash");
var _ = require('../lodash');

module.exports = floydWarshall;

var DEFAULT_WEIGHT_FUNC = _.constant(1);

function floydWarshall(g, weightFn, edgeFn) {
return runFloydWarshall(g,
return runFloydWarshall(
g,
weightFn || DEFAULT_WEIGHT_FUNC,
edgeFn || function(v) { return g.outEdges(v); });
edgeFn ||
function (v) {
return g.outEdges(v);
},
);
}

function runFloydWarshall(g, weightFn, edgeFn) {
var results = {};
var nodes = g.nodes();

nodes.forEach(function(v) {
nodes.forEach(function (v) {
results[v] = {};
results[v][v] = { distance: 0 };
nodes.forEach(function(w) {
nodes.forEach(function (w) {
if (v !== w) {
results[v][w] = { distance: Number.POSITIVE_INFINITY };
}
});
edgeFn(v).forEach(function(edge) {
edgeFn(v).forEach(function (edge) {
var w = edge.v === v ? edge.w : edge.v;
var d = weightFn(edge);
results[v][w] = { distance: d, predecessor: v };
});
});

nodes.forEach(function(k) {
nodes.forEach(function (k) {
var rowK = results[k];
nodes.forEach(function(i) {
nodes.forEach(function (i) {
var rowI = results[i];
nodes.forEach(function(j) {
nodes.forEach(function (j) {
var ik = rowI[k];
var kj = rowK[j];
var ij = rowI[j];
Expand Down
22 changes: 11 additions & 11 deletions lib/alg/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module.exports = {
components: require("./components"),
dijkstra: require("./dijkstra"),
dijkstraAll: require("./dijkstra-all"),
findCycles: require("./find-cycles"),
floydWarshall: require("./floyd-warshall"),
isAcyclic: require("./is-acyclic"),
postorder: require("./postorder"),
preorder: require("./preorder"),
prim: require("./prim"),
tarjan: require("./tarjan"),
topsort: require("./topsort")
components: require('./components'),
dijkstra: require('./dijkstra'),
dijkstraAll: require('./dijkstra-all'),
findCycles: require('./find-cycles'),
floydWarshall: require('./floyd-warshall'),
isAcyclic: require('./is-acyclic'),
postorder: require('./postorder'),
preorder: require('./preorder'),
prim: require('./prim'),
tarjan: require('./tarjan'),
topsort: require('./topsort'),
};
2 changes: 1 addition & 1 deletion lib/alg/is-acyclic.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var topsort = require("./topsort");
var topsort = require('./topsort');

module.exports = isAcyclic;

Expand Down
4 changes: 2 additions & 2 deletions lib/alg/postorder.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var dfs = require("./dfs");
var dfs = require('./dfs');

module.exports = postorder;

function postorder(g, vs) {
return dfs(g, vs, "post");
return dfs(g, vs, 'post');
}
4 changes: 2 additions & 2 deletions lib/alg/preorder.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var dfs = require("./dfs");
var dfs = require('./dfs');

module.exports = preorder;

function preorder(g, vs) {
return dfs(g, vs, "pre");
return dfs(g, vs, 'pre');
}
10 changes: 5 additions & 5 deletions lib/alg/prim.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var _ = require("../lodash");
var Graph = require("../graph");
var PriorityQueue = require("../data/priority-queue");
var _ = require('../lodash');
var Graph = require('../graph');
var PriorityQueue = require('../data/priority-queue');

module.exports = prim;

Expand All @@ -26,7 +26,7 @@ function prim(g, weightFunc) {
return result;
}

_.each(g.nodes(), function(v) {
_.each(g.nodes(), function (v) {
pq.add(v, Number.POSITIVE_INFINITY);
result.setNode(v);
});
Expand All @@ -40,7 +40,7 @@ function prim(g, weightFunc) {
if (_.has(parents, v)) {
result.setEdge(v, parents[v]);
} else if (init) {
throw new Error("Input graph is not connected: " + g);
throw new Error('Input graph is not connected: ' + g);
} else {
init = true;
}
Expand Down
12 changes: 6 additions & 6 deletions lib/alg/tarjan.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var _ = require("../lodash");
var _ = require('../lodash');

module.exports = tarjan;

Expand All @@ -9,14 +9,14 @@ function tarjan(g) {
var results = [];

function dfs(v) {
var entry = visited[v] = {
var entry = (visited[v] = {
onStack: true,
lowlink: index,
index: index++
};
index: index++,
});
stack.push(v);

g.successors(v).forEach(function(w) {
g.successors(v).forEach(function (w) {
if (!_.has(visited, w)) {
dfs(w);
entry.lowlink = Math.min(entry.lowlink, visited[w].lowlink);
Expand All @@ -37,7 +37,7 @@ function tarjan(g) {
}
}

g.nodes().forEach(function(v) {
g.nodes().forEach(function (v) {
if (!_.has(visited, v)) {
dfs(v);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/alg/topsort.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var _ = require("../lodash");
var _ = require('../lodash');

module.exports = topsort;
topsort.CycleException = CycleException;
Expand Down Expand Up @@ -32,4 +32,4 @@ function topsort(g) {
}

function CycleException() {}
CycleException.prototype = new Error(); // must be an instance of Error to pass testing
CycleException.prototype = new Error(); // must be an instance of Error to pass testing
Loading

0 comments on commit 902129a

Please sign in to comment.