@@ -17,6 +17,11 @@ function output(graph, name){
1717 outputGraph ( graph . serialize ( ) , name ) ;
1818}
1919
20+ function withWeight ( nodeList , weight ) {
21+ nodeList . weight = weight ;
22+ return nodeList ;
23+ }
24+
2025describe ( "Graph" , function ( ) {
2126
2227 describe ( "Data structure" , function ( ) {
@@ -337,14 +342,14 @@ describe("Graph", function() {
337342
338343 it ( "Should compute shortest path on a single edge." , function ( ) {
339344 var graph = Graph ( ) . addEdge ( "a" , "b" ) ;
340- assert . deepEqual ( graph . shortestPath ( "a" , "b" ) , [ "a" , "b" ] ) ;
345+ assert . deepEqual ( graph . shortestPath ( "a" , "b" ) , withWeight ( [ "a" , "b" ] , 1 ) ) ;
341346 } ) ;
342347
343348 it ( "Should compute shortest path on two edges." , function ( ) {
344349 var graph = Graph ( )
345350 . addEdge ( "a" , "b" )
346351 . addEdge ( "b" , "c" ) ;
347- assert . deepEqual ( graph . shortestPath ( "a" , "c" ) , [ "a" , "b" , "c" ] ) ;
352+ assert . deepEqual ( graph . shortestPath ( "a" , "c" ) , withWeight ( [ "a" , "b" , "c" ] , 2 ) ) ;
348353 } ) ;
349354
350355 it ( "Should compute shortest path on example from Cormen text (p. 659)." , function ( ) {
@@ -358,8 +363,9 @@ describe("Graph", function() {
358363 . addEdge ( "y" , "z" , 2 )
359364 . addEdge ( "x" , "z" , 4 )
360365 . addEdge ( "z" , "x" , 6 ) ;
361- assert . deepEqual ( graph . shortestPath ( "s" , "z" ) , [ "s" , "y" , "z" ] ) ;
362- assert . deepEqual ( graph . shortestPath ( "s" , "x" ) , [ "s" , "y" , "t" , "x" ] ) ;
366+
367+ assert . deepEqual ( graph . shortestPath ( "s" , "z" ) , withWeight ( [ "s" , "y" , "z" ] , 5 + 2 ) ) ;
368+ assert . deepEqual ( graph . shortestPath ( "s" , "x" ) , withWeight ( [ "s" , "y" , "t" , "x" ] , 5 + 3 + 1 ) ) ;
363369 } ) ;
364370
365371 it ( "Should throw error if source node not in graph." , function ( ) {
@@ -384,7 +390,7 @@ describe("Graph", function() {
384390 . addEdge ( "a" , "b" )
385391 . addEdge ( "b" , "c" )
386392 . addEdge ( "d" , "e" ) ;
387- assert . deepEqual ( graph . shortestPath ( "a" , "c" ) , [ "a" , "b" , "c" ] ) ;
393+ assert . deepEqual ( graph . shortestPath ( "a" , "c" ) , withWeight ( [ "a" , "b" , "c" ] , 2 ) ) ;
388394 } ) ;
389395 } ) ;
390396} ) ;
0 commit comments