@@ -32,12 +32,12 @@ function Graph(serialized?: Serialized) {
32
32
// The adjacency list of the graph.
33
33
// Keys are node ids.
34
34
// Values are adjacent node id arrays.
35
- var edges : Record < NodeId , NodeId [ ] > = { } ;
35
+ const edges : Record < NodeId , NodeId [ ] > = { } ;
36
36
37
37
// The weights of edges.
38
38
// Keys are string encodings of edges.
39
39
// Values are weights (numbers).
40
- var edgeWeights : Record < EncodedEdge , EdgeWeight > = { } ;
40
+ const edgeWeights : Record < EncodedEdge , EdgeWeight > = { } ;
41
41
42
42
// If a serialized graph was passed into the constructor, deserialize it.
43
43
if ( serialized ) {
@@ -138,7 +138,7 @@ function Graph(serialized?: Serialized) {
138
138
// Computes the indegree for the given node.
139
139
// Not very efficient, costs O(E) where E = number of edges.
140
140
function indegree ( node : NodeId ) {
141
- var degree = 0 ;
141
+ let degree = 0 ;
142
142
function check ( v : NodeId ) {
143
143
if ( v === node ) {
144
144
degree ++ ;
@@ -157,9 +157,8 @@ function Graph(serialized?: Serialized) {
157
157
158
158
// Depth First Search algorithm, inspired by
159
159
// Cormen et al. "Introduction to Algorithms" 3rd Ed. p. 604
160
- // This variant includes an additional option
161
- // `includeSourceNodes` to specify whether to include or
162
- // exclude the source nodes from the result (true by default).
160
+ // The additional option `includeSourceNodes` specifies whether to
161
+ // include or exclude the source nodes from the result (true by default).
163
162
// If `sourceNodes` is not specified, all nodes in the graph
164
163
// are used as source nodes.
165
164
function depthFirstSearch (
@@ -174,8 +173,8 @@ function Graph(serialized?: Serialized) {
174
173
includeSourceNodes = true ;
175
174
}
176
175
177
- var visited : Record < NodeId , boolean > = { } ;
178
- var nodeList : NodeId [ ] = [ ] ;
176
+ const visited : Record < NodeId , boolean > = { } ;
177
+ const nodeList : NodeId [ ] = [ ] ;
179
178
180
179
function DFSVisit ( node : NodeId ) {
181
180
if ( ! visited [ node ] ) {
@@ -203,8 +202,8 @@ function Graph(serialized?: Serialized) {
203
202
// Inspired by https://github.com/relaxedws/lca/blob/master/src/LowestCommonAncestor.php code
204
203
// but uses depth search instead of breadth. Also uses some optimizations
205
204
function lowestCommonAncestors ( node1 : NodeId , node2 : NodeId ) {
206
- var node1Ancestors : NodeId [ ] = [ ] ;
207
- var lcas : NodeId [ ] = [ ] ;
205
+ const node1Ancestors : NodeId [ ] = [ ] ;
206
+ const lcas : NodeId [ ] = [ ] ;
208
207
209
208
function CA1Visit (
210
209
visited : Record < NodeId , boolean > ,
@@ -297,8 +296,8 @@ function Graph(serialized?: Serialized) {
297
296
298
297
// Linear search to extract (find and remove) min from q.
299
298
function extractMin ( ) : NodeId | null {
300
- var min = Infinity ;
301
- var minNode ;
299
+ let min = Infinity ;
300
+ let minNode ;
302
301
Object . keys ( q ) . forEach ( function ( node ) {
303
302
if ( d [ node ] < min ) {
304
303
min = d [ node ] ;
@@ -315,7 +314,7 @@ function Graph(serialized?: Serialized) {
315
314
}
316
315
317
316
function relax ( u : NodeId , v : NodeId ) {
318
- var w = getEdgeWeight ( u , v ) ;
317
+ const w = getEdgeWeight ( u , v ) ;
319
318
if ( d [ v ] > d [ u ] + w ) {
320
319
d [ v ] = d [ u ] + w ;
321
320
p [ v ] = u ;
@@ -326,7 +325,7 @@ function Graph(serialized?: Serialized) {
326
325
initializeSingleSource ( ) ;
327
326
initializePriorityQueue ( ) ;
328
327
while ( ! priorityQueueEmpty ( ) ) {
329
- var u = extractMin ( ) ;
328
+ const u = extractMin ( ) ;
330
329
if ( u === null ) return ;
331
330
adjacent ( u ) . forEach ( function ( v ) {
332
331
relax ( u as string , v ) ;
@@ -337,9 +336,9 @@ function Graph(serialized?: Serialized) {
337
336
// Assembles the shortest path by traversing the
338
337
// predecessor subgraph from destination to source.
339
338
function path ( ) {
340
- var nodeList : NodeId [ ] & { weight ?: EdgeWeight } = [ ] ;
341
- var weight = 0 ;
342
- var node = destination ;
339
+ const nodeList : NodeId [ ] & { weight ?: EdgeWeight } = [ ] ;
340
+ let weight = 0 ;
341
+ let node = destination ;
343
342
while ( p [ node ] ) {
344
343
nodeList . push ( node ) ;
345
344
weight += getEdgeWeight ( p [ node ] , node ) ;
@@ -361,15 +360,15 @@ function Graph(serialized?: Serialized) {
361
360
362
361
// Serializes the graph.
363
362
function serialize ( ) {
364
- var serialized : Serialized = {
363
+ const serialized : Serialized = {
365
364
nodes : nodes ( ) . map ( function ( id ) {
366
365
return { id : id } ;
367
366
} ) ,
368
367
links : [ ]
369
368
} ;
370
369
371
370
serialized . nodes . forEach ( function ( node ) {
372
- var source = node . id ;
371
+ const source = node . id ;
373
372
adjacent ( source ) . forEach ( function ( target ) {
374
373
serialized . links . push ( {
375
374
source : source ,
0 commit comments