1- import { buildEdgeMap } from '@algorithm.ts/graph'
2- import { testOjCodes } from 'jest.setup'
1+ import { TestOjDataProblemKey , testOjCodes } from '@@/fixtures/test-util/oj-data'
2+ import { buildEdgeMap , getShortestPath } from '@algorithm.ts/graph'
3+ import assert from 'assert'
34import type { IBellmanFordGraph } from '../src'
4- import { BellmanFord , bellmanFord } from '../src'
5+ import { BellmanFord , bellmanFord , bellmanFordBigint } from '../src'
56
67describe ( 'basic' , function ( ) {
7- test ( 'no negative cycle' , function ( ) {
8- const graph : IBellmanFordGraph = {
9- N : 4 ,
10- source : 0 ,
11- edges : [
12- { to : 1 , cost : 2 } ,
13- { to : 2 , cost : 2 } ,
14- { to : 3 , cost : 2 } ,
15- { to : 3 , cost : 1 } ,
16- ] ,
17- G : [ [ 0 ] , [ 1 , 2 ] , [ 3 ] , [ ] ] ,
18- }
19- const dist : number [ ] = [ ]
8+ describe ( 'bellmanFord' , function ( ) {
9+ test ( 'no negative cycle' , function ( ) {
10+ const graph : IBellmanFordGraph < number > = {
11+ N : 4 ,
12+ source : 0 ,
13+ edges : [
14+ { to : 1 , cost : 2 } ,
15+ { to : 2 , cost : 2 } ,
16+ { to : 3 , cost : 2 } ,
17+ { to : 3 , cost : 1 } ,
18+ ] ,
19+ G : [ [ 0 ] , [ 1 , 2 ] , [ 3 ] , [ ] ] ,
20+ }
21+
22+ const result = bellmanFord ( graph )
23+ expect ( result . hasNegativeCycle ) . toEqual ( false )
24+
25+ assert ( result . hasNegativeCycle === false )
26+ expect ( result . dist . slice ( 0 , graph . N ) ) . toEqual ( [ 0 , 2 , 4 , 4 ] )
27+ } )
2028
21- expect ( bellmanFord ( graph , { dist } ) ) . toEqual ( true )
22- expect ( dist . slice ( 0 , graph . N ) ) . toEqual ( [ 0 , 2 , 4 , 4 ] )
29+ test ( 'negative cycle' , function ( ) {
30+ const graph : IBellmanFordGraph < number > = {
31+ N : 4 ,
32+ source : 0 ,
33+ edges : [
34+ { to : 1 , cost : - 2 } ,
35+ { to : 0 , cost : - 2 } ,
36+ { to : 3 , cost : 2 } ,
37+ { to : 3 , cost : 1 } ,
38+ ] ,
39+ G : [ [ 0 ] , [ 1 , 2 ] , [ 3 ] , [ ] ] ,
40+ }
41+ expect ( bellmanFord ( graph ) ) . toEqual ( { hasNegativeCycle : true } )
42+ } )
2343 } )
2444
25- test ( 'negative cycle' , function ( ) {
26- const dist : number [ ] = [ ]
27- const graph : IBellmanFordGraph = {
28- N : 4 ,
29- source : 0 ,
30- edges : [
31- { to : 1 , cost : - 2 } ,
32- { to : 0 , cost : - 2 } ,
33- { to : 3 , cost : 2 } ,
34- { to : 3 , cost : 1 } ,
35- ] ,
36- G : [ [ 0 ] , [ 1 , 2 ] , [ 3 ] , [ ] ] ,
37- }
38- expect ( bellmanFord ( graph , { dist } ) ) . toEqual ( false )
45+ describe ( 'bellmanFordBigint' , function ( ) {
46+ test ( 'no negative cycle' , function ( ) {
47+ const graph : IBellmanFordGraph < bigint > = {
48+ N : 4 ,
49+ source : 0 ,
50+ edges : [
51+ { to : 1 , cost : 2n } ,
52+ { to : 2 , cost : 2n } ,
53+ { to : 3 , cost : 2n } ,
54+ { to : 3 , cost : 1n } ,
55+ ] ,
56+ G : [ [ 0 ] , [ 1 , 2 ] , [ 3 ] , [ ] ] ,
57+ }
58+
59+ const result = bellmanFordBigint ( graph )
60+ expect ( result . hasNegativeCycle ) . toEqual ( false )
61+
62+ assert ( result . hasNegativeCycle === false )
63+ expect ( result . dist . slice ( 0 , graph . N ) ) . toEqual ( [ 0n , 2n , 4n , 4n ] )
64+ } )
65+
66+ test ( 'negative cycle' , function ( ) {
67+ const graph : IBellmanFordGraph < bigint > = {
68+ N : 4 ,
69+ source : 0 ,
70+ edges : [
71+ { to : 1 , cost : - 2n } ,
72+ { to : 0 , cost : - 2n } ,
73+ { to : 3 , cost : 2n } ,
74+ { to : 3 , cost : 1n } ,
75+ ] ,
76+ G : [ [ 0 ] , [ 1 , 2 ] , [ 3 ] , [ ] ] ,
77+ }
78+ expect ( bellmanFordBigint ( graph ) ) . toEqual ( { hasNegativeCycle : true } )
79+ } )
3980 } )
4081} )
4182
@@ -58,31 +99,32 @@ describe('shortest path', function () {
5899 { from : Nodes . D , to : Nodes . C , cost : - 5 } ,
59100 ]
60101
61- const graph : IBellmanFordGraph = {
102+ const graph : IBellmanFordGraph < number > = {
62103 N,
63104 source : Nodes . A ,
64105 edges,
65106 G : buildEdgeMap ( N , edges ) ,
66107 }
67108
68- let a2aPath : number [ ] | undefined
69- let a2bPath : number [ ] | undefined
70- let a2cPath : number [ ] | undefined
71- let a2dPath : number [ ] | undefined
72-
73- const _bellmanFord = new BellmanFord ( )
74- const noNegativeCycle : boolean = _bellmanFord . bellmanFord ( graph , undefined , context => {
75- a2aPath = context . getShortestPathTo ( Nodes . A )
76- a2bPath = context . getShortestPathTo ( Nodes . B )
77- a2cPath = context . getShortestPathTo ( Nodes . C )
78- a2dPath = context . getShortestPathTo ( Nodes . D )
109+ const _bellmanFord = new BellmanFord < number > ( {
110+ ZERO : 0 ,
111+ INF : Math . floor ( Number . MAX_SAFE_INTEGER / 2 ) ,
79112 } )
113+ const result = _bellmanFord . bellmanFord ( graph )
114+
115+ expect ( result . hasNegativeCycle ) . toEqual ( false )
116+ assert ( result . hasNegativeCycle === false )
117+ const { bestFrom } = result
80118
81- expect ( noNegativeCycle ) . toEqual ( true )
82- expect ( a2aPath ) . toEqual ( [ Nodes . A ] )
83- expect ( a2bPath ) . toEqual ( [ Nodes . A , Nodes . B ] )
84- expect ( a2cPath ) . toEqual ( [ Nodes . A , Nodes . B , Nodes . C ] )
85- expect ( a2dPath ) . toEqual ( [ Nodes . A , Nodes . B , Nodes . C , Nodes . D ] )
119+ expect ( getShortestPath ( bestFrom , Nodes . A , Nodes . A ) ) . toEqual ( [ Nodes . A ] )
120+ expect ( getShortestPath ( bestFrom , Nodes . A , Nodes . B ) ) . toEqual ( [ Nodes . A , Nodes . B ] )
121+ expect ( getShortestPath ( bestFrom , Nodes . A , Nodes . C ) ) . toEqual ( [ Nodes . A , Nodes . B , Nodes . C ] )
122+ expect ( getShortestPath ( bestFrom , Nodes . A , Nodes . D ) ) . toEqual ( [
123+ Nodes . A ,
124+ Nodes . B ,
125+ Nodes . C ,
126+ Nodes . D ,
127+ ] )
86128 } )
87129
88130 test ( 'with negative cycle' , function ( ) {
@@ -105,28 +147,26 @@ describe('shortest path', function () {
105147 { from : Nodes . D , to : Nodes . B , cost : 1 } ,
106148 ]
107149
108- const graph : IBellmanFordGraph = {
150+ const graph : IBellmanFordGraph < number > = {
109151 N,
110152 source : Nodes . A ,
111153 edges,
112154 G : buildEdgeMap ( N , edges ) ,
113155 }
114-
115- const noNegativeCycle : boolean = bellmanFord ( graph )
116- expect ( noNegativeCycle ) . toEqual ( false )
156+ expect ( bellmanFord ( graph ) ) . toEqual ( { hasNegativeCycle : true } )
117157 } )
118158} )
119159
120160describe ( 'oj' , function ( ) {
121161 // https://leetcode.com/problems/number-of-ways-to-arrive-at-destination/
122162 testOjCodes (
123- 'leetcode/number-of-ways-to-arrive-at-destination' ,
163+ TestOjDataProblemKey . LEETCODE_NUMBER_OF_WAYS_TO_ARRIVE_AT_DESTINATION ,
124164 import ( './oj/number-of-ways-to-arrive-at-destination' ) ,
125165 )
126166
127167 // https://leetcode.com/problems/maximum-path-quality-of-a-graph/
128168 testOjCodes (
129- 'leetcode/maximum-path-quality-of-a-graph' ,
169+ TestOjDataProblemKey . LEETCODE_MAXIMUM_PATH_QUALITY_OF_A_GRAPH ,
130170 import ( './oj/maximum-path-quality-of-a-graph' ) ,
131171 )
132172} )
0 commit comments