Skip to content

Commit 85efcba

Browse files
committed
Move Vertex::getFlow() to Algorithm\Flow::getFlowVertex()
1 parent 051c54f commit 85efcba

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

src/Balance.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Fhaculty\Graph\Algorithm\BaseGraph;
66
use Fhaculty\Graph\Graph;
77
use Fhaculty\Graph\Vertex;
8+
use Fhaculty\Graph\Algorithm\Flow;
89

910
/**
1011
* Basic algorithms for working with the balance of flow graphs
@@ -41,15 +42,17 @@ public function getBalance()
4142
*
4243
* @return boolean
4344
* @see Algorithm\Degree::isBalanced() if you merely want to check indegree=outdegree
44-
* @uses Vertex::getFlow()
45+
* @uses Flow::getFlowVertex()
4546
* @uses Vertex::getBalance()
4647
*/
4748
public function isBalancedFlow()
4849
{
50+
$flow = new Flow($this->graph);
51+
4952
// no need to check for each edge: flow <= capacity (setters already check that)
5053
// check for each vertex: outflow-inflow = balance
5154
foreach ($this->graph->getVertices() as $vertex) {
52-
if ($vertex->getFlow() !== $vertex->getBalance()) {
55+
if ($flow->getFlowVertex($vertex) !== $vertex->getBalance()) {
5356
return false;
5457
}
5558
}

src/Flow.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
use Fhaculty\Graph\Algorithm\BaseGraph;
66
use Fhaculty\Graph\Edge\Base as Edge;
7+
use Fhaculty\Graph\Edge\Directed as EdgeDirected;
78
use Fhaculty\Graph\Graph;
9+
use Fhaculty\Graph\Vertex;
810

911
/**
1012
* Basic algorithms for working with flow graphs
@@ -33,4 +35,43 @@ public function hasFlow()
3335

3436
return false;
3537
}
38+
39+
/**
40+
* Calculates the flow for this Vertex: sum(outflow) - sum(inflow)
41+
*
42+
* Usually, vertices should have a resulting flow of 0: The sum of flows
43+
* entering a vertex must equal the sum of flows leaving a vertex. If the
44+
* resulting flow is < 0, this vertex is considered a sink (i.e. there's
45+
* more flow into this vertex). If the resulting flow is > 0, this vertex
46+
* is considered a "source" (i.e. there's more flow leaving this vertex).
47+
*
48+
* @param Vertex $vertex
49+
* @return float
50+
* @throws UnexpectedValueException if they are undirected edges
51+
* @see Vertex::getBalance()
52+
* @uses Vertex::getEdges()
53+
* @uses Edge::getFlow()
54+
*/
55+
public function getFlowVertex(Vertex $vertex)
56+
{
57+
$sumOfFlow = 0;
58+
59+
foreach ($vertex->getEdges() as $edge) {
60+
if (!($edge instanceof EdgeDirected)) {
61+
throw new UnexpectedValueException("TODO: undirected edges not suported yet");
62+
}
63+
64+
// edge is an outgoing edge of this vertex
65+
if ($edge->hasVertexStart($vertex)) {
66+
// flowing out (flow is "pointing away")
67+
$sumOfFlow += $edge->getFlow();
68+
// this is an ingoing edge
69+
} else {
70+
// flowing in
71+
$sumOfFlow -= $edge->getFlow();
72+
}
73+
}
74+
75+
return $sumOfFlow;
76+
}
3677
}

src/MinimumCostFlow/SuccessiveShortestPath.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
class SuccessiveShortestPath extends Base
2020
{
2121
/**
22-
* @uses Vertex::getFlow()
2322
* @uses Graph::createGraphClone()
2423
* @uses AlgorithmResidualGraph::createGraph()
2524
* @uses AlgorithmSpMooreBellmanFord::getEdgesTo(Vertex $targetVertex)

0 commit comments

Comments
 (0)