22
33namespace Fhaculty \Graph \Algorithm \MinimumCostFlow ;
44
5-
65use Fhaculty \Graph \Exception \UnexpectedValueException ;
7-
86use Fhaculty \Graph \Exception \UnderflowException ;
9-
107use Fhaculty \Graph \Edge \Base as Edge ;
118use Fhaculty \Graph \Set \Edges ;
129use Fhaculty \Graph \Algorithm \MaxFlow \EdmondsKarp as MaxFlowEdmondsKarp ;
@@ -29,17 +26,16 @@ public function createGraph()
2926
3027 // connect supersource s* and supersink t* with all "normal" sources and sinks
3128 foreach ($ resultGraph ->getVertices () as $ vertex ) {
32- // $vertex->getFlow();
33- $ flow = $ vertex ->getBalance ();
34- $ b = abs ($ vertex ->getBalance ());
35- // source
36- if ($ flow > 0 ) {
37- $ superSource ->createEdgeTo ($ vertex )->setCapacity ($ b );
38-
39- $ sumBalance += $ flow ;
40- // sink
41- } elseif ($ flow < 0 ) {
42- $ vertex ->createEdgeTo ($ superSink )->setCapacity ($ b );
29+ $ balance = $ vertex ->getBalance ();
30+
31+ if ($ balance > 0 ) {
32+ // positive balance => source capacity
33+ $ superSource ->createEdgeTo ($ vertex )->setCapacity ($ balance );
34+
35+ $ sumBalance += $ balance ;
36+ } elseif ($ balance < 0 ) {
37+ // negative balance => sink capacity (positive)
38+ $ vertex ->createEdgeTo ($ superSink )->setCapacity (-$ balance );
4339 }
4440 }
4541
@@ -62,8 +58,8 @@ public function createGraph()
6258 $ alg = new DetectNegativeCycle ($ residualGraph );
6359 try {
6460 $ clonedEdges = $ alg ->getCycleNegative ()->getEdges ();
65- // no negative cycle found => end algorithm
6661 } catch (UnderflowException $ ignore ) {
62+ // no negative cycle found => end algorithm
6763 break ;
6864 }
6965
0 commit comments