11"use strict" ;
2- var __extends = ( this && this . __extends ) || ( function ( ) {
3- var extendStatics = function ( d , b ) {
4- extendStatics = Object . setPrototypeOf ||
5- ( { __proto__ : [ ] } instanceof Array && function ( d , b ) { d . __proto__ = b ; } ) ||
6- function ( d , b ) { for ( var p in b ) if ( Object . prototype . hasOwnProperty . call ( b , p ) ) d [ p ] = b [ p ] ; } ;
7- return extendStatics ( d , b ) ;
8- } ;
9- return function ( d , b ) {
10- if ( typeof b !== "function" && b !== null )
11- throw new TypeError ( "Class extends value " + String ( b ) + " is not a constructor or null" ) ;
12- extendStatics ( d , b ) ;
13- function __ ( ) { this . constructor = d ; }
14- d . prototype = b === null ? Object . create ( b ) : ( __ . prototype = b . prototype , new __ ( ) ) ;
15- } ;
16- } ) ( ) ;
17- var CycleError = /** @class */ ( function ( _super ) {
18- __extends ( CycleError , _super ) ;
19- function CycleError ( message ) {
20- var _this = _super . call ( this , message ) || this ;
21- Object . setPrototypeOf ( _this , CycleError . prototype ) ;
22- return _this ;
2+ class CycleError extends Error {
3+ constructor ( message ) {
4+ super ( message ) ;
5+ Object . setPrototypeOf ( this , CycleError . prototype ) ;
236 }
24- return CycleError ;
25- } ( Error ) ) ;
7+ }
268// A graph data structure with depth-first search and topological sort.
279function Graph ( serialized ) {
2810 // Returned graph instance
29- var graph = {
30- addNode : addNode ,
31- removeNode : removeNode ,
32- nodes : nodes ,
33- adjacent : adjacent ,
34- addEdge : addEdge ,
35- removeEdge : removeEdge ,
36- hasEdge : hasEdge ,
37- setEdgeWeight : setEdgeWeight ,
38- getEdgeWeight : getEdgeWeight ,
39- indegree : indegree ,
40- outdegree : outdegree ,
41- depthFirstSearch : depthFirstSearch ,
42- hasCycle : hasCycle ,
43- lowestCommonAncestors : lowestCommonAncestors ,
44- topologicalSort : topologicalSort ,
45- shortestPath : shortestPath ,
46- serialize : serialize ,
47- deserialize : deserialize
11+ const graph = {
12+ addNode,
13+ removeNode,
14+ nodes,
15+ adjacent,
16+ addEdge,
17+ removeEdge,
18+ hasEdge,
19+ setEdgeWeight,
20+ getEdgeWeight,
21+ indegree,
22+ outdegree,
23+ depthFirstSearch,
24+ hasCycle,
25+ lowestCommonAncestors,
26+ topologicalSort,
27+ shortestPath,
28+ serialize,
29+ deserialize
4830 } ;
4931 // The adjacency list of the graph.
5032 // Keys are node ids.
5133 // Values are adjacent node id arrays.
52- var edges = { } ;
34+ const edges = { } ;
5335 // The weights of edges.
5436 // Keys are string encodings of edges.
5537 // Values are weights (numbers).
56- var edgeWeights = { } ;
38+ const edgeWeights = { } ;
5739 // If a serialized graph was passed into the constructor, deserialize it.
5840 if ( serialized ) {
5941 deserialize ( serialized ) ;
@@ -83,7 +65,7 @@ function Graph(serialized) {
8365 // Gets the list of nodes that have been added to the graph.
8466 function nodes ( ) {
8567 // TODO: Better implementation with set data structure
86- var nodeSet = { } ;
68+ const nodeSet = { } ;
8769 Object . keys ( edges ) . forEach ( function ( u ) {
8870 nodeSet [ u ] = true ;
8971 edges [ u ] . forEach ( function ( v ) {
@@ -110,7 +92,7 @@ function Graph(serialized) {
11092 // Gets the weight of the given edge.
11193 // Returns 1 if no weight was previously set.
11294 function getEdgeWeight ( u , v ) {
113- var weight = edgeWeights [ encodeEdge ( u , v ) ] ;
95+ const weight = edgeWeights [ encodeEdge ( u , v ) ] ;
11496 return weight === undefined ? 1 : weight ;
11597 }
11698 // Adds an edge from node u to node v.
@@ -142,7 +124,7 @@ function Graph(serialized) {
142124 // Computes the indegree for the given node.
143125 // Not very efficient, costs O(E) where E = number of edges.
144126 function indegree ( node ) {
145- var degree = 0 ;
127+ let degree = 0 ;
146128 function check ( v ) {
147129 if ( v === node ) {
148130 degree ++ ;
@@ -163,18 +145,16 @@ function Graph(serialized) {
163145 // include or exclude the source nodes from the result (true by default).
164146 // If `sourceNodes` is not specified, all nodes in the graph
165147 // are used as source nodes.
166- function depthFirstSearch ( sourceNodes , includeSourceNodes , errorOnCycle ) {
167- if ( includeSourceNodes === void 0 ) { includeSourceNodes = true ; }
168- if ( errorOnCycle === void 0 ) { errorOnCycle = false ; }
148+ function depthFirstSearch ( sourceNodes , includeSourceNodes = true , errorOnCycle = false ) {
169149 if ( ! sourceNodes ) {
170150 sourceNodes = nodes ( ) ;
171151 }
172152 if ( typeof includeSourceNodes !== "boolean" ) {
173153 includeSourceNodes = true ;
174154 }
175- var visited = { } ;
176- var visiting = { } ;
177- var nodeList = [ ] ;
155+ const visited = { } ;
156+ const visiting = { } ;
157+ const nodeList = [ ] ;
178158 function DFSVisit ( node ) {
179159 if ( visiting [ node ] && errorOnCycle ) {
180160 throw new CycleError ( "Cycle found" ) ;
@@ -220,8 +200,8 @@ function Graph(serialized) {
220200 // Inspired by https://github.com/relaxedws/lca/blob/master/src/LowestCommonAncestor.php code
221201 // but uses depth search instead of breadth. Also uses some optimizations
222202 function lowestCommonAncestors ( node1 , node2 ) {
223- var node1Ancestors = [ ] ;
224- var lcas = [ ] ;
203+ const node1Ancestors = [ ] ;
204+ const lcas = [ ] ;
225205 function CA1Visit ( visited , node ) {
226206 if ( ! visited [ node ] ) {
227207 visited [ node ] = true ;
@@ -230,7 +210,7 @@ function Graph(serialized) {
230210 lcas . push ( node ) ;
231211 return false ; // found - shortcut
232212 }
233- return adjacent ( node ) . every ( function ( node ) {
213+ return adjacent ( node ) . every ( node => {
234214 return CA1Visit ( visited , node ) ;
235215 } ) ;
236216 }
@@ -245,7 +225,7 @@ function Graph(serialized) {
245225 lcas . push ( node ) ;
246226 }
247227 else if ( lcas . length == 0 ) {
248- adjacent ( node ) . forEach ( function ( node ) {
228+ adjacent ( node ) . forEach ( node => {
249229 CA2Visit ( visited , node ) ;
250230 } ) ;
251231 }
@@ -261,20 +241,19 @@ function Graph(serialized) {
261241 // such that for each visited edge (u, v), u comes before v in the list.
262242 // Amazingly, this comes from just reversing the result from depth first search.
263243 // Cormen et al. "Introduction to Algorithms" 3rd Ed. p. 613
264- function topologicalSort ( sourceNodes , includeSourceNodes ) {
265- if ( includeSourceNodes === void 0 ) { includeSourceNodes = true ; }
244+ function topologicalSort ( sourceNodes , includeSourceNodes = true ) {
266245 return depthFirstSearch ( sourceNodes , includeSourceNodes , true ) . reverse ( ) ;
267246 }
268247 // Dijkstra's Shortest Path Algorithm.
269248 // Cormen et al. "Introduction to Algorithms" 3rd Ed. p. 658
270249 // Variable and function names correspond to names in the book.
271250 function shortestPath ( source , destination ) {
272251 // Upper bounds for shortest path weights from source.
273- var d = { } ;
252+ const d = { } ;
274253 // Predecessors.
275- var p = { } ;
254+ const p = { } ;
276255 // Poor man's priority queue, keyed on d.
277- var q = { } ;
256+ let q = { } ;
278257 function initializeSingleSource ( ) {
279258 nodes ( ) . forEach ( function ( node ) {
280259 d [ node ] = Infinity ;
@@ -299,8 +278,8 @@ function Graph(serialized) {
299278 }
300279 // Linear search to extract (find and remove) min from q.
301280 function extractMin ( ) {
302- var min = Infinity ;
303- var minNode ;
281+ let min = Infinity ;
282+ let minNode ;
304283 Object . keys ( q ) . forEach ( function ( node ) {
305284 if ( d [ node ] < min ) {
306285 min = d [ node ] ;
@@ -316,7 +295,7 @@ function Graph(serialized) {
316295 return minNode ;
317296 }
318297 function relax ( u , v ) {
319- var w = getEdgeWeight ( u , v ) ;
298+ const w = getEdgeWeight ( u , v ) ;
320299 if ( d [ v ] > d [ u ] + w ) {
321300 d [ v ] = d [ u ] + w ;
322301 p [ v ] = u ;
@@ -325,26 +304,21 @@ function Graph(serialized) {
325304 function dijkstra ( ) {
326305 initializeSingleSource ( ) ;
327306 initializePriorityQueue ( ) ;
328- var _loop_1 = function ( ) {
329- var u = extractMin ( ) ;
307+ while ( ! priorityQueueEmpty ( ) ) {
308+ const u = extractMin ( ) ;
330309 if ( u === null )
331- return { value : void 0 } ;
310+ return ;
332311 adjacent ( u ) . forEach ( function ( v ) {
333312 relax ( u , v ) ;
334313 } ) ;
335- } ;
336- while ( ! priorityQueueEmpty ( ) ) {
337- var state_1 = _loop_1 ( ) ;
338- if ( typeof state_1 === "object" )
339- return state_1 . value ;
340314 }
341315 }
342316 // Assembles the shortest path by traversing the
343317 // predecessor subgraph from destination to source.
344318 function path ( ) {
345- var nodeList = [ ] ;
346- var weight = 0 ;
347- var node = destination ;
319+ const nodeList = [ ] ;
320+ let weight = 0 ;
321+ let node = destination ;
348322 while ( p [ node ] ) {
349323 nodeList . push ( node ) ;
350324 weight += getEdgeWeight ( p [ node ] , node ) ;
@@ -363,14 +337,14 @@ function Graph(serialized) {
363337 }
364338 // Serializes the graph.
365339 function serialize ( ) {
366- var serialized = {
340+ const serialized = {
367341 nodes : nodes ( ) . map ( function ( id ) {
368342 return { id : id } ;
369343 } ) ,
370344 links : [ ]
371345 } ;
372346 serialized . nodes . forEach ( function ( node ) {
373- var source = node . id ;
347+ const source = node . id ;
374348 adjacent ( source ) . forEach ( function ( target ) {
375349 serialized . links . push ( {
376350 source : source ,
0 commit comments