@@ -178,7 +178,7 @@ export function Graph(serialized?: Serialized) {
178178 function depthFirstSearch (
179179 sourceNodes ?: NodeId [ ] ,
180180 includeSourceNodes : boolean = true ,
181- errorOnCycle : boolean = false
181+ errorOnCycle : boolean = false ,
182182 ) {
183183 if ( ! sourceNodes ) {
184184 sourceNodes = nodes ( ) ;
@@ -284,7 +284,7 @@ export function Graph(serialized?: Serialized) {
284284 // Cormen et al. "Introduction to Algorithms" 3rd Ed. p. 613
285285 function topologicalSort (
286286 sourceNodes ?: NodeId [ ] ,
287- includeSourceNodes : boolean = true
287+ includeSourceNodes : boolean = true ,
288288 ) {
289289 return depthFirstSearch ( sourceNodes , includeSourceNodes , true ) . reverse ( ) ;
290290 }
@@ -394,17 +394,18 @@ export function Graph(serialized?: Serialized) {
394394 function shortestPaths ( source : NodeId , destination : NodeId ) {
395395 let path = shortestPath ( source , destination ) ;
396396 const paths = [ path ] ,
397- removedEdges = [ ] ,
397+ removedEdges : any = [ ] ,
398398 weight = path . weight ;
399399 while ( weight ) {
400- if ( hasEdge ( path [ 0 ] , path [ 1 ] ) ) {
401- removeEdge ( path [ 0 ] , path [ 1 ] ) ;
402- removedEdges . push ( [ path [ 0 ] , path [ 1 ] ] ) ;
403- }
404- if ( hasEdge ( path [ 1 ] , path [ 0 ] ) ) {
405- removeEdge ( path [ 1 ] , path [ 0 ] ) ;
406- removedEdges . push ( [ path [ 1 ] , path [ 0 ] ] ) ;
407- }
400+ [
401+ [ path [ 0 ] , path [ 1 ] ] ,
402+ [ path [ 1 ] , path [ 0 ] ] ,
403+ ] . forEach ( ( [ u , v ] ) => {
404+ if ( hasEdge ( u , v ) ) {
405+ removedEdges . push ( { u, v, weight : getEdgeWeight ( u , v ) } ) ;
406+ removeEdge ( u , v ) ;
407+ }
408+ } ) ;
408409 try {
409410 path = shortestPath ( source , destination ) ;
410411 if ( ! path . weight || weight < path . weight ) break ;
@@ -413,7 +414,7 @@ export function Graph(serialized?: Serialized) {
413414 break ;
414415 }
415416 }
416- for ( const [ u , v ] of removedEdges ) addEdge ( u , v ) ;
417+ for ( const { u, v, weight } of removedEdges ) addEdge ( u , v , weight ) ;
417418 return paths ;
418419 }
419420
0 commit comments