Skip to content

Commit 844e149

Browse files
committed
Use Set\Edges in algorithms
1 parent ab9df5d commit 844e149

File tree

11 files changed

+32
-21
lines changed

11 files changed

+32
-21
lines changed

src/MaxFlow/EdmondsKarp.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Fhaculty\Graph\Graph;
1818
use Fhaculty\Graph\Vertex;
1919
use Fhaculty\Graph\Edge\Base as Edge;
20+
use Fhaculty\Graph\Set\Edges;
2021
use Fhaculty\Graph\Algorithm\Base;
2122
use Fhaculty\Graph\Algorithm\ResidualGraph;
2223
use Fhaculty\Graph\Exception;
@@ -86,7 +87,7 @@ public function createGraph()
8687
// If path exists add the new flow to graph
8788
if ($pathFlow) {
8889
// 2. get max flow from path
89-
$maxFlowValue = Edge::getFirst($pathFlow->getEdges(), Edge::ORDER_CAPACITY)->getCapacity();
90+
$maxFlowValue = $pathFlow->getEdges()->getEdgeOrder(Edges::ORDER_CAPACITY)->getCapacity();
9091

9192
// 3. add flow to path
9293
foreach ($pathFlow->getEdges() as $edge) {

src/MaximumMatching/Base.php

Lines changed: 2 additions & 1 deletion
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\Edge\Base as Edge;
8+
use Fhaculty\Graph\Set\Edges;
89

910
abstract class Base extends BaseGraph
1011
{
@@ -35,7 +36,7 @@ public function createGraph()
3536
/**
3637
* create new resulting graph with minimum-cost flow on edges
3738
*
38-
* @return Edge[]
39+
* @return Edges
3940
*/
4041
abstract public function getEdges();
4142
}

src/MinimumCostFlow/Base.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Fhaculty\Graph\Algorithm\Weight as AlgorithmWeight;
77
use Fhaculty\Graph\Exception\UnderflowException;
88
use Fhaculty\Graph\Edge\Base as Edge;
9+
use Fhaculty\Graph\Set\Edges;
910
use Fhaculty\Graph\Exception\UnexpectedValueException;
1011
use Fhaculty\Graph\Graph;
1112

@@ -32,14 +33,14 @@ protected function checkBalance()
3233
* helper used to add $newFlow to original edges of $clonedEdges in graph $resultGraph
3334
*
3435
* @param Graph $resultGraph graph to look for original edges
35-
* @param Edge[] $clonedEdges array of cloned edges to be modified
36+
* @param Edges $clonedEdges set of cloned edges to be modified
3637
* @param number $newFlow flow to add
3738
* @uses Graph::getEdgeClone()
3839
* @uses Graph::getEdgeCloneInverted()
3940
* @uses Edge::getFlow()
4041
* @uses Edge::setFlow()
4142
*/
42-
protected function addFlow(Graph $resultGraph, $clonedEdges, $newFlow)
43+
protected function addFlow(Graph $resultGraph, Edges $clonedEdges, $newFlow)
4344
{
4445
foreach ($clonedEdges as $clonedEdge) {
4546
try {

src/MinimumCostFlow/CycleCanceling.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace Fhaculty\Graph\Algorithm\MinimumCostFlow;
44

5+
56
use Fhaculty\Graph\Exception\UnexpectedValueException;
67

78
use Fhaculty\Graph\Exception\UnderflowException;
89

910
use Fhaculty\Graph\Edge\Base as Edge;
11+
use Fhaculty\Graph\Set\Edges;
1012
use Fhaculty\Graph\Algorithm\MaxFlow\EdmondsKarp as MaxFlowEdmondsKarp;
1113
use Fhaculty\Graph\Algorithm\DetectNegativeCycle;
1214
use Fhaculty\Graph\Algorithm\ResidualGraph;
@@ -66,7 +68,7 @@ public function createGraph()
6668
}
6769

6870
// calculate maximal possible flow = minimum capacity remaining for all edges
69-
$newFlow = Edge::getFirst($clonedEdges, Edge::ORDER_CAPACITY_REMAINING)->getCapacityRemaining();
71+
$newFlow = $clonedEdges->getEdgeOrder(Edges::ORDER_CAPACITY_REMAINING)->getCapacityRemaining();
7072

7173
// set flow on original graph
7274
$this->addFlow($resultGraph, $clonedEdges, $newFlow);

src/MinimumCostFlow/SuccessiveShortestPath.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Fhaculty\Graph\Vertex;
1313
use Fhaculty\Graph\Edge\Base as Edge;
1414
use Fhaculty\Graph\Edge\Directed as EdgeDirected;
15+
use Fhaculty\Graph\Set\Edges;
1516
use Fhaculty\Graph\Algorithm\ShortestPath\MooreBellmanFord as SpMooreBellmanFord;
1617
use Fhaculty\Graph\Algorithm\ResidualGraph;
1718
use Fhaculty\Graph\Algorithm\Search\BreadthFirst as SearchBreadthFirst;
@@ -98,7 +99,7 @@ public function createGraph()
9899
}
99100

100101
// get minimum of capacity remaining on path
101-
$minCapacity = Edge::getFirst($edgesOnFlow, Edge::ORDER_CAPACITY_REMAINING)->getCapacityRemaining();
102+
$minCapacity = $edgesOnFlow->getEdgeOrder(Edges::ORDER_CAPACITY_REMAINING)->getCapacityRemaining();
102103
if ($minCapacity < $newflow) {
103104
$newflow = $minCapacity;
104105
}

src/MinimumSpanningTree/Base.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Fhaculty\Graph\Algorithm\MinimumSpanningTree;
44

55
use Fhaculty\Graph\Algorithm\Base as AlgorithmBase;
6+
use Fhaculty\Graph\Set\Edges;
67

78
abstract class Base extends AlgorithmBase
89
{
@@ -24,7 +25,7 @@ abstract protected function getGraph();
2425
/**
2526
* get all edges on minimum spanning tree
2627
*
27-
* @return Edge[]
28+
* @return Edges
2829
*/
2930
abstract public function getEdges();
3031
}

src/MinimumSpanningTree/Kruskal.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected function getGraph()
3636

3737
/**
3838
*
39-
* @return Edge[]
39+
* @return Edges
4040
*/
4141
public function getEdges()
4242
{

src/ShortestPath/Base.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Fhaculty\Graph\Exception\InvalidArgumentException;
99
use Fhaculty\Graph\Vertex;
1010
use Fhaculty\Graph\Edge\Base as Edge;
11+
use Fhaculty\Graph\Set\Edges;
1112
use Fhaculty\Graph\Set\Vertices;
1213

1314
abstract class Base extends BaseVertex
@@ -162,10 +163,10 @@ public function createGraph()
162163
* get cheapest edges (lowest weight) for given map of vertex predecessors
163164
*
164165
* @param Vertex[] $predecessor
165-
* @return Edge[]
166+
* @return Edges
166167
* @uses Graph::getVertices()
167168
* @uses Vertex::getEdgesTo()
168-
* @uses Edge::getFirst()
169+
* @uses Edges::getEdgeOrder()
169170
*/
170171
protected function getEdgesCheapestPredecesor(array $predecessor)
171172
{
@@ -180,17 +181,17 @@ protected function getEdgesCheapestPredecesor(array $predecessor)
180181
$predecesVertex = $predecessor[$vid];
181182

182183
// get cheapest edge
183-
$edges []= Edge::getFirst($predecesVertex->getEdgesTo($vertex), Edge::ORDER_WEIGHT);
184+
$edges []= $predecesVertex->getEdgesTo($vertex)->getEdgeOrder(Edges::ORDER_WEIGHT);
184185
}
185186
}
186187

187-
return $edges;
188+
return new Edges($edges);
188189
}
189190

190191
/**
191192
* get all edges on shortest path for this vertex
192193
*
193-
* @return Edge[]
194+
* @return Edges
194195
*/
195196
abstract public function getEdges();
196197
}

src/TravelingSalesmanProblem/MinimumSpanningTree.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Fhaculty\Graph\Graph;
66
use Fhaculty\Graph\Edge\Base as Edge;
7+
use Fhaculty\Graph\Set\Edges;
78
use Fhaculty\Graph\Algorithm\MinimumSpanningTree\Kruskal as MstKruskal;
89
use Fhaculty\Graph\Algorithm\Search\BreadthFirst as SearchDepthFirst;
910

@@ -32,7 +33,7 @@ protected function getGraph()
3233

3334
/**
3435
*
35-
* @return Edge[]
36+
* @return Edges
3637
*/
3738
public function getEdges()
3839
{
@@ -58,16 +59,16 @@ public function getEdges()
5859
$startVertex = $vertex;
5960
} else {
6061
// get edge(s) to clone, multiple edges are possible (returns an array if undirected edge)
61-
$returnEdges []= Edge::getFirst($oldVertex->getEdgesTo($vertex));
62+
$returnEdges []= $oldVertex->getEdgesTo($vertex)->getEdgeFirst();
6263
}
6364

6465
$oldVertex = $vertex;
6566
}
6667

6768
// connect last vertex with start vertex
6869
// multiple edges are possible (returns an array if undirected edge)
69-
$returnEdges []= Edge::getFirst($oldVertex->getEdgesTo($startVertex));
70+
$returnEdges []= $oldVertex->getEdgesTo($startVertex)->getEdgeFirst();
7071

71-
return $returnEdges;
72+
return new Edges($returnEdges);
7273
}
7374
}

src/TravelingSalesmanProblem/NearestNeighbor.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Fhaculty\Graph\Vertex;
88
use Fhaculty\Graph\Edge\Base as Edge;
9+
use Fhaculty\Graph\Set\Edges;
910
use \SplPriorityQueue;
1011

1112
class NearestNeighbor extends Base
@@ -33,7 +34,7 @@ protected function getGraph()
3334

3435
/**
3536
*
36-
* @return Edge[]
37+
* @return Edges
3738
*/
3839
public function getEdges()
3940
{
@@ -85,8 +86,8 @@ public function getEdges()
8586
// check if there is a way from end edge to start edge
8687
// get first connecting edge
8788
// connect the last vertex with the start vertex
88-
$returnEdges []= Edge::getFirst($vertex->getEdgesTo($this->vertex));
89+
$returnEdges []= $vertex->getEdgesTo($this->vertex)->getEdgeFirst();
8990

90-
return $returnEdges;
91+
return new Edges($returnEdges);
9192
}
9293
}

0 commit comments

Comments
 (0)