2
2
3
3
namespace Fhaculty \Graph \Algorithm \MinimumCostFlow ;
4
4
5
-
6
5
use Fhaculty \Graph \Exception \UnexpectedValueException ;
7
-
8
6
use Fhaculty \Graph \Exception \UnderflowException ;
9
-
10
7
use Fhaculty \Graph \Edge \Base as Edge ;
11
8
use Fhaculty \Graph \Set \Edges ;
12
9
use Fhaculty \Graph \Algorithm \MaxFlow \EdmondsKarp as MaxFlowEdmondsKarp ;
@@ -29,17 +26,16 @@ public function createGraph()
29
26
30
27
// connect supersource s* and supersink t* with all "normal" sources and sinks
31
28
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 );
43
39
}
44
40
}
45
41
@@ -62,8 +58,8 @@ public function createGraph()
62
58
$ alg = new DetectNegativeCycle ($ residualGraph );
63
59
try {
64
60
$ clonedEdges = $ alg ->getCycleNegative ()->getEdges ();
65
- // no negative cycle found => end algorithm
66
61
} catch (UnderflowException $ ignore ) {
62
+ // no negative cycle found => end algorithm
67
63
break ;
68
64
}
69
65
0 commit comments