@@ -463,6 +463,65 @@ var jsgraphs = jsgraphs || {};
463463
464464 jss . WeightedDiGraph = WeightedDiGraph ;
465465
466+ var FlowEdge = function ( v , w , capacity ) {
467+ this . v = v ;
468+ this . w = w ;
469+ this . capacity = capacity ;
470+ this . flow = 0 ;
471+ } ;
472+
473+ FlowEdge . prototype . residualCapacityTo = function ( x ) {
474+ if ( x == this . v ) {
475+ return this . flow ;
476+ } else {
477+ return this . capacity - this . flow ;
478+ }
479+ } ;
480+
481+ FlowEdge . prototype . addResidualFlowTo = function ( x , deltaFlow ) {
482+ if ( x == this . v ) {
483+ this . flow -= deltaFlow ;
484+ } else if ( x == this . w ) {
485+ this . flow += deltaFlow ;
486+ }
487+ } ;
488+
489+ FlowEdge . prototype . from = function ( ) {
490+ return this . v ;
491+ } ;
492+
493+ FlowEdge . prototype . to = function ( ) {
494+ return this . w ;
495+ } ;
496+
497+ FlowEdge . prototype . other = function ( x ) {
498+ return x == this . v ? this . w : this . v ;
499+ }
500+
501+
502+ jss . FlowEdge = FlowEdge ;
503+
504+ var FlowNetwork = function ( V ) {
505+ this . V = V ;
506+ this . adjList = [ ] ;
507+ for ( var v = 0 ; v < V ; ++ v ) {
508+ this . adjList . push ( [ ] ) ;
509+ }
510+ } ;
511+
512+ FlowNetwork . prototype . addEdge = function ( e ) {
513+ var v = e . from ( ) ;
514+ this . adjList [ v ] . push ( e ) ;
515+ var w = e . other ( v ) ;
516+ this . adjList [ w ] . push ( e ) ;
517+ } ;
518+
519+ FlowNetwork . prototype . adj = function ( v ) {
520+ return this . adjList [ v ] ;
521+ } ;
522+
523+ jss . FlowNetwork = FlowNetwork ;
524+
466525 var DepthFirstSearch = function ( G , s ) {
467526 this . s = s ;
468527 var V = G . V ;
@@ -980,6 +1039,60 @@ var jsgraphs = jsgraphs || {};
9801039 } ;
9811040
9821041 jss . TopologicalSortShortestPaths = TopologicalSortShortestPaths ;
1042+
1043+ var FordFulkerson = function ( G , s , t ) {
1044+ this . value = 0 ;
1045+ var V = G . V ;
1046+ var bottle = Numbers . MAX_VALUE ;
1047+ this . marked = null ;
1048+ this . edgeTo = null ;
1049+ this . s = s ;
1050+ this . t = t ;
1051+ while ( this . hasAugmentedPath ( G ) ) {
1052+
1053+ for ( var x = this . t ; x != this . s ; x = this . edgeTo [ x ] . from ( ) ) {
1054+ bottle = Math . min ( bottle , this . edgeTo [ x ] . residualCapacityTo ( x ) ) ;
1055+ }
1056+
1057+ for ( var x = this . t ; x != this . s ; x = this . edgeTo [ x ] . from ( ) ) {
1058+ this . edgeTo [ x ] . addResidualFlowTo ( x , bottle ) ;
1059+ }
1060+
1061+ this . value += bottle ;
1062+ }
1063+ } ;
1064+
1065+ FordFulkerson . prototype . hasAugmentedPath = function ( G ) {
1066+ var V = G . V ;
1067+ this . marked = [ ] ;
1068+ this . edgeTo = [ ] ;
1069+ for ( var v = 0 ; v < V ; ++ v ) {
1070+ this . marked . push ( false ) ;
1071+ this . edgeTo . push ( null ) ;
1072+ }
1073+
1074+ var queue = new jss . Queue ( ) ;
1075+ queue . push ( this . s ) ;
1076+
1077+ while ( ! queue . isEmpty ( ) ) {
1078+ var v = queue . dequeue ( ) ;
1079+ var adj_v = G . adj ( v ) ;
1080+ this . marked [ v ] = true ;
1081+ for ( var i = 0 ; i < adj_v . length ; ++ i ) {
1082+ var e = adj_v [ i ] ;
1083+ var w = e . other ( v ) ;
1084+ if ( e . residualCapacityTo ( v ) > 0 ) {
1085+ if ( w == this . t ) {
1086+ return true ;
1087+ }
1088+ this . edgeTo [ w ] = e ;
1089+ queue . push ( w ) ;
1090+ }
1091+ }
1092+ }
1093+
1094+ return false ;
1095+ } ;
9831096
9841097} ) ( jsgraphs ) ;
9851098
0 commit comments