@@ -79,6 +79,23 @@ describe('(Async) Shortest Path from source to target on graph', () => {
79
79
expect ( path ) . toStrictEqual ( [ 'A' , 'B' , 'C' ] ) ;
80
80
} ) ;
81
81
82
+ it ( 'find all shortest paths' , async ( ) => {
83
+ const { findShortestPathAsync } = await getAlgorithm ( ) ;
84
+ const { length, allPath } = await findShortestPathAsync ( data , 'A' , 'F' ) ;
85
+ expect ( length ) . toBe ( 2 ) ;
86
+ expect ( allPath [ 0 ] ) . toStrictEqual ( [ 'A' , 'E' , 'F' ] ) ;
87
+ expect ( allPath [ 1 ] ) . toStrictEqual ( [ 'A' , 'D' , 'F' ] ) ;
88
+
89
+ const {
90
+ length : directedLenght ,
91
+ path : directedPath ,
92
+ allPath : directedAllPath ,
93
+ } = await findShortestPathAsync ( data , 'A' , 'F' , true ) ;
94
+ expect ( directedLenght ) . toBe ( 2 ) ;
95
+ expect ( directedAllPath [ 0 ] ) . toStrictEqual ( [ 'A' , 'E' , 'F' ] ) ;
96
+ expect ( directedPath ) . toStrictEqual ( [ 'A' , 'E' , 'F' ] ) ;
97
+ } ) ;
98
+
82
99
it ( 'find all paths' , async ( ) => {
83
100
const { findAllPathAsync } = await getAlgorithm ( ) ;
84
101
const allPaths = await findAllPathAsync ( data , 'A' , 'E' ) ;
@@ -95,4 +112,16 @@ describe('(Async) Shortest Path from source to target on graph', () => {
95
112
expect ( allPaths [ 0 ] ) . toStrictEqual ( [ 'A' , 'D' , 'E' ] ) ;
96
113
expect ( allPaths [ 1 ] ) . toStrictEqual ( [ 'A' , 'E' ] ) ;
97
114
} ) ;
115
+
116
+ it ( 'find all shortest paths in weighted graph' , async ( ) => {
117
+ const { findShortestPathAsync } = await getAlgorithm ( ) ;
118
+ data . edges . forEach ( ( edge , i ) => {
119
+ edge . weight = ( ( i % 2 ) + 1 ) * 2 ;
120
+ if ( edge . source === 'F' && edge . target === 'D' ) edge . weight = 10 ;
121
+ } ) ;
122
+ const { length, path, allPath } = await findShortestPathAsync ( data , 'A' , 'F' , false , 'weight' ) ;
123
+ expect ( length ) . toBe ( 6 ) ;
124
+ expect ( allPath [ 0 ] ) . toStrictEqual ( [ 'A' , 'E' , 'F' ] ) ;
125
+ expect ( path ) . toStrictEqual ( [ 'A' , 'E' , 'F' ] ) ;
126
+ } ) ;
98
127
} ) ;
0 commit comments