3
3
namespace Fhaculty \Graph \Algorithm \MinimumCostFlow ;
4
4
5
5
use Fhaculty \Graph \Exception \DomainException ;
6
-
7
6
use Fhaculty \Graph \Exception \UnderflowException ;
8
-
9
7
use Fhaculty \Graph \Exception \UnexpectedValueException ;
10
-
11
8
use Fhaculty \Graph \Graph ;
12
9
use Fhaculty \Graph \Vertex ;
13
10
use Fhaculty \Graph \Edge \Base as Edge ;
@@ -21,10 +18,9 @@ class SuccessiveShortestPath extends Base
21
18
{
22
19
/**
23
20
* @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()
28
24
*/
29
25
public function createGraph ()
30
26
{
@@ -40,7 +36,7 @@ public function createGraph()
40
36
// initial flow of edges
41
37
$ edges = $ resultGraph ->getEdges ();
42
38
foreach ($ edges as $ edge ) {
43
- // 0 if weight of edge is positiv
39
+ // 0 if weight of edge is positive
44
40
$ flow = 0 ;
45
41
46
42
// maximal flow if weight of edge is negative
@@ -62,7 +58,7 @@ public function createGraph()
62
58
$ edge ->setFlow ($ flow );
63
59
}
64
60
65
- // return or Exception insite this while
61
+ // return or Exception inside this while
66
62
while (true ) {
67
63
// create residual graph
68
64
$ algRG = new ResidualGraph ($ resultGraph );
@@ -71,25 +67,25 @@ public function createGraph()
71
67
// search for a source
72
68
try {
73
69
$ sourceVertex = $ this ->getVertexSource ($ residualGraph );
74
- // if no source is found the minimum-cost flow is found
75
70
} catch (UnderflowException $ ignore ) {
71
+ // no source is found => minimum-cost flow is found
76
72
break ;
77
73
}
78
74
79
- // search for reachble sink from this source
75
+ // search for reachable target sink from this source
80
76
try {
81
77
$ 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 );
85
81
}
86
82
87
83
// calculate shortest path between source- and target-vertex
88
84
$ algSP = new SpMooreBellmanFord ($ sourceVertex );
89
85
$ edgesOnFlow = $ algSP ->getEdgesTo ($ targetVertex );
90
86
91
87
// 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
93
89
$ newflow = $ this ->graph ->getVertex ($ sourceVertex ->getId ())->getBalance () - $ sourceVertex ->getBalance ();
94
90
$ targetFlow = - ($ this ->graph ->getVertex ($ targetVertex ->getId ())->getBalance () - $ targetVertex ->getBalance ());
95
91
@@ -107,7 +103,7 @@ public function createGraph()
107
103
// add the new flow to the path
108
104
$ this ->addFlow ($ resultGraph , $ edgesOnFlow , $ newflow );
109
105
110
- // add balance to source and remove for the sink
106
+ // add balance to source and remove for the target sink
111
107
$ oriSourceVertex = $ resultGraph ->getVertex ($ sourceVertex ->getId ());
112
108
$ oriTargetVertex = $ resultGraph ->getVertex ($ targetVertex ->getId ());
113
109
0 commit comments