Skip to content

Commit aad0ad2

Browse files
committed
Improve comments and throw nested Exception
1 parent 77d4c45 commit aad0ad2

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

src/MinimumCostFlow/SuccessiveShortestPath.php

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
namespace Fhaculty\Graph\Algorithm\MinimumCostFlow;
44

55
use Fhaculty\Graph\Exception\DomainException;
6-
76
use Fhaculty\Graph\Exception\UnderflowException;
8-
97
use Fhaculty\Graph\Exception\UnexpectedValueException;
10-
118
use Fhaculty\Graph\Graph;
129
use Fhaculty\Graph\Vertex;
1310
use Fhaculty\Graph\Edge\Base as Edge;
@@ -21,10 +18,9 @@ class SuccessiveShortestPath extends Base
2118
{
2219
/**
2320
* @uses Graph::createGraphClone()
24-
* @uses AlgorithmResidualGraph::createGraph()
25-
* @uses AlgorithmSpMooreBellmanFord::getEdgesTo(Vertex $targetVertex)
26-
*
27-
* @see AlgorithmMCF::createGraph()
21+
* @uses ResidualGraph::createGraph()
22+
* @uses SpMooreBellmanFord::getEdgesTo(Vertex $targetVertex)
23+
* @see Base::createGraph()
2824
*/
2925
public function createGraph()
3026
{
@@ -40,7 +36,7 @@ public function createGraph()
4036
// initial flow of edges
4137
$edges = $resultGraph->getEdges();
4238
foreach ($edges as $edge) {
43-
// 0 if weight of edge is positiv
39+
// 0 if weight of edge is positive
4440
$flow = 0;
4541

4642
// maximal flow if weight of edge is negative
@@ -62,7 +58,7 @@ public function createGraph()
6258
$edge->setFlow($flow);
6359
}
6460

65-
// return or Exception insite this while
61+
// return or Exception inside this while
6662
while (true) {
6763
// create residual graph
6864
$algRG = new ResidualGraph($resultGraph);
@@ -71,25 +67,25 @@ public function createGraph()
7167
// search for a source
7268
try {
7369
$sourceVertex = $this->getVertexSource($residualGraph);
74-
// if no source is found the minimum-cost flow is found
7570
} catch (UnderflowException $ignore) {
71+
// no source is found => minimum-cost flow is found
7672
break;
7773
}
7874

79-
// search for reachble sink from this source
75+
// search for reachable target sink from this source
8076
try {
8177
$targetVertex = $this->getVertexSink($sourceVertex);
82-
// if no target is found the network has not enough capacity
83-
} catch (UnderflowException $ignore) {
84-
throw new UnexpectedValueException('The graph has not enough capacity for the minimum-cost flow');
78+
} catch (UnderflowException $e) {
79+
// no target found => network does not have enough capacity
80+
throw new UnexpectedValueException('The graph has not enough capacity for the minimum-cost flow', 0, $e);
8581
}
8682

8783
// calculate shortest path between source- and target-vertex
8884
$algSP = new SpMooreBellmanFord($sourceVertex);
8985
$edgesOnFlow = $algSP->getEdgesTo($targetVertex);
9086

9187
// calculate the maximal possible flow
92-
// new flow is the maximal possible flow for this path
88+
// new flow is the maximal possible flow for this path
9389
$newflow = $this->graph->getVertex($sourceVertex->getId())->getBalance() - $sourceVertex->getBalance();
9490
$targetFlow = - ($this->graph->getVertex($targetVertex->getId())->getBalance() - $targetVertex->getBalance());
9591

@@ -107,7 +103,7 @@ public function createGraph()
107103
// add the new flow to the path
108104
$this->addFlow($resultGraph, $edgesOnFlow, $newflow);
109105

110-
// add balance to source and remove for the sink
106+
// add balance to source and remove for the target sink
111107
$oriSourceVertex = $resultGraph->getVertex($sourceVertex->getId());
112108
$oriTargetVertex = $resultGraph->getVertex($targetVertex->getId());
113109

0 commit comments

Comments
 (0)