Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,8 @@ function Graph(serialized) {
}
// Depth First Search algorithm, inspired by
// Cormen et al. "Introduction to Algorithms" 3rd Ed. p. 604
// This variant includes an additional option
// `includeSourceNodes` to specify whether to include or
// exclude the source nodes from the result (true by default).
// The additional option `includeSourceNodes` specifies whether to
// include or exclude the source nodes from the result (true by default).
// If `sourceNodes` is not specified, all nodes in the graph
// are used as source nodes.
function depthFirstSearch(sourceNodes, includeSourceNodes) {
Expand Down Expand Up @@ -273,13 +272,18 @@ function Graph(serialized) {
function dijkstra() {
initializeSingleSource();
initializePriorityQueue();
while (!priorityQueueEmpty()) {
var _loop_1 = function () {
var u = extractMin();
if (u === null)
return;
return { value: void 0 };
adjacent(u).forEach(function (v) {
relax(u, v);
});
};
while (!priorityQueueEmpty()) {
var state_1 = _loop_1();
if (typeof state_1 === "object")
return state_1.value;
}
}
// Assembles the shortest path by traversing the
Expand Down
37 changes: 18 additions & 19 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ function Graph(serialized?: Serialized) {
// The adjacency list of the graph.
// Keys are node ids.
// Values are adjacent node id arrays.
var edges: Record<NodeId, NodeId[]> = {};
const edges: Record<NodeId, NodeId[]> = {};

// The weights of edges.
// Keys are string encodings of edges.
// Values are weights (numbers).
var edgeWeights: Record<EncodedEdge, EdgeWeight> = {};
const edgeWeights: Record<EncodedEdge, EdgeWeight> = {};

// If a serialized graph was passed into the constructor, deserialize it.
if (serialized) {
Expand Down Expand Up @@ -138,7 +138,7 @@ function Graph(serialized?: Serialized) {
// Computes the indegree for the given node.
// Not very efficient, costs O(E) where E = number of edges.
function indegree(node: NodeId) {
var degree = 0;
let degree = 0;
function check(v: NodeId) {
if (v === node) {
degree++;
Expand All @@ -157,9 +157,8 @@ function Graph(serialized?: Serialized) {

// Depth First Search algorithm, inspired by
// Cormen et al. "Introduction to Algorithms" 3rd Ed. p. 604
// This variant includes an additional option
// `includeSourceNodes` to specify whether to include or
// exclude the source nodes from the result (true by default).
// The additional option `includeSourceNodes` specifies whether to
// include or exclude the source nodes from the result (true by default).
// If `sourceNodes` is not specified, all nodes in the graph
// are used as source nodes.
function depthFirstSearch(
Expand All @@ -174,8 +173,8 @@ function Graph(serialized?: Serialized) {
includeSourceNodes = true;
}

var visited: Record<NodeId, boolean> = {};
var nodeList: NodeId[] = [];
const visited: Record<NodeId, boolean> = {};
const nodeList: NodeId[] = [];

function DFSVisit(node: NodeId) {
if (!visited[node]) {
Expand Down Expand Up @@ -203,8 +202,8 @@ function Graph(serialized?: Serialized) {
// Inspired by https://github.com/relaxedws/lca/blob/master/src/LowestCommonAncestor.php code
// but uses depth search instead of breadth. Also uses some optimizations
function lowestCommonAncestors(node1: NodeId, node2: NodeId) {
var node1Ancestors: NodeId[] = [];
var lcas: NodeId[] = [];
const node1Ancestors: NodeId[] = [];
const lcas: NodeId[] = [];

function CA1Visit(
visited: Record<NodeId, boolean>,
Expand Down Expand Up @@ -297,8 +296,8 @@ function Graph(serialized?: Serialized) {

// Linear search to extract (find and remove) min from q.
function extractMin(): NodeId | null {
var min = Infinity;
var minNode;
let min = Infinity;
let minNode;
Object.keys(q).forEach(function(node) {
if (d[node] < min) {
min = d[node];
Expand All @@ -315,7 +314,7 @@ function Graph(serialized?: Serialized) {
}

function relax(u: NodeId, v: NodeId) {
var w = getEdgeWeight(u, v);
const w = getEdgeWeight(u, v);
if (d[v] > d[u] + w) {
d[v] = d[u] + w;
p[v] = u;
Expand All @@ -326,7 +325,7 @@ function Graph(serialized?: Serialized) {
initializeSingleSource();
initializePriorityQueue();
while (!priorityQueueEmpty()) {
var u = extractMin();
const u = extractMin();
if (u === null) return;
adjacent(u).forEach(function(v) {
relax(u as string, v);
Expand All @@ -337,9 +336,9 @@ function Graph(serialized?: Serialized) {
// Assembles the shortest path by traversing the
// predecessor subgraph from destination to source.
function path() {
var nodeList: NodeId[] & { weight?: EdgeWeight } = [];
var weight = 0;
var node = destination;
const nodeList: NodeId[] & { weight?: EdgeWeight } = [];
let weight = 0;
let node = destination;
while (p[node]) {
nodeList.push(node);
weight += getEdgeWeight(p[node], node);
Expand All @@ -361,15 +360,15 @@ function Graph(serialized?: Serialized) {

// Serializes the graph.
function serialize() {
var serialized: Serialized = {
const serialized: Serialized = {
nodes: nodes().map(function(id) {
return { id: id };
}),
links: []
};

serialized.nodes.forEach(function(node) {
var source = node.id;
const source = node.id;
adjacent(source).forEach(function(target) {
serialized.links.push({
source: source,
Expand Down