11import {
2- Edge ,
2+ EdgeDef ,
33 PriorityQueue ,
44 ShortestPath ,
55 ShortestPathAlgo ,
@@ -12,7 +12,7 @@ export class HeapNode {
1212 ) { }
1313}
1414
15- export class DijkstraShortestPath < T , E extends Edge < T > >
15+ export class DijkstraShortestPath < T , E extends EdgeDef < T > >
1616 implements ShortestPathAlgo < T , E >
1717{
1818 private readonly nodeToIndex = new Map < T , number > ( ) ;
@@ -24,7 +24,7 @@ export class DijkstraShortestPath<T, E extends Edge<T>>
2424 public priorityQueueFactory : ( ) => PriorityQueue < HeapNode > ,
2525 public readonly INF = 1e9 ,
2626 ) {
27- const nodeSet = new Set ( edges . flatMap ( ( e ) => [ e [ 0 ] , e [ 1 ] ] ) ) ;
27+ const nodeSet = new Set ( edges . flatMap ( ( e ) => [ e . from , e . to ] ) ) ;
2828 let index = 0 ;
2929 for ( const node of nodeSet ) {
3030 this . nodes [ index ] = node ;
@@ -33,11 +33,11 @@ export class DijkstraShortestPath<T, E extends Edge<T>>
3333 index ++ ;
3434 }
3535 for ( const edge of edges ) {
36- const fromId = this . nodeToIndex . get ( edge [ 0 ] ) ;
36+ const fromId = this . nodeToIndex . get ( edge . from ) ;
3737 if ( fromId == null ) {
3838 throw new Error ( ) ;
3939 }
40- const toId = this . nodeToIndex . get ( edge [ 1 ] ) ;
40+ const toId = this . nodeToIndex . get ( edge . to ) ;
4141 if ( toId == null ) {
4242 throw new Error ( ) ;
4343 }
@@ -51,7 +51,7 @@ export class DijkstraShortestPath<T, E extends Edge<T>>
5151 calculate (
5252 srcNode : T ,
5353 dstNode : T ,
54- distFn : ( edge : E ) => number = ( e ) => e [ 2 ] ,
54+ distFn : ( edge : E ) => number = ( e ) => e . distance ,
5555 ) : ShortestPath < T > {
5656 const src = this . nodeToIndex . get ( srcNode ) ! ;
5757 if ( src == null ) {
@@ -94,7 +94,7 @@ export class DijkstraShortestPath<T, E extends Edge<T>>
9494 }
9595 if ( dist < 0 ) {
9696 throw new Error (
97- `Negative distance ${ dist } for edge ${ neighbor . edge [ 0 ] } -> ${ neighbor . edge [ 1 ] } ` ,
97+ `Negative distance ${ dist } for edge ${ neighbor . edge . from } -> ${ neighbor . edge . to } ` ,
9898 ) ;
9999 }
100100 const candidate = distances [ node ] + dist ;
0 commit comments