Skip to content

Commit 89e999f

Browse files
committed
Improve code style
1 parent 7034a99 commit 89e999f

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

src/MinimumCostFlow/CycleCanceling.php

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22

33
namespace Fhaculty\Graph\Algorithm\MinimumCostFlow;
44

5-
65
use Fhaculty\Graph\Exception\UnexpectedValueException;
7-
86
use Fhaculty\Graph\Exception\UnderflowException;
9-
107
use Fhaculty\Graph\Edge\Base as Edge;
118
use Fhaculty\Graph\Set\Edges;
129
use 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

Comments
 (0)