Skip to content

Commit 924e12c

Browse files
committed
Merge remote-tracking branch 'origin/master' into add-vertices-class
Conflicts: lib/Fhaculty/Graph/Algorithm/Degree.php lib/Fhaculty/Graph/Edge/Base.php lib/Fhaculty/Graph/Edge/Directed.php lib/Fhaculty/Graph/Edge/UndirectedId.php lib/Fhaculty/Graph/Graph.php lib/Fhaculty/Graph/Vertex.php lib/Fhaculty/Graph/Walk.php tests/bootstrap.php
2 parents 2b0e963 + 6a1f521 commit 924e12c

35 files changed

+189
-176
lines changed

src/Bipartit.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function getColors()
5151
$colors = array();
5252

5353
// get color for each vertex
54-
foreach ($this->graph->getVertices() as $vid => $startVertex) {
54+
foreach ($this->graph->getVertices()->getMap() as $vid => $startVertex) {
5555
if (!isset($colors[$vid])) {
5656
$queue = array($startVertex);
5757
// initialize each components color
@@ -65,7 +65,7 @@ public function getColors()
6565
$nextColor = 1-$color;
6666

6767
// scan all vertices connected to this vertex
68-
foreach ($vertex->getVerticesEdge() as $vid => $nextVertex) {
68+
foreach ($vertex->getVerticesEdge()->getMap() as $vid => $nextVertex) {
6969
// color unknown, so expect next color for this vertex
7070
if (!isset($colors[$vid])) {
7171
$colors[$vid] = $nextColor;
@@ -92,7 +92,7 @@ public function getColorVertices()
9292
$colors = $this->getColors();
9393
$ret = array(0 => array(), 1 => array());
9494

95-
foreach ($this->graph->getVertices() as $vid => $vertex) {
95+
foreach ($this->graph->getVertices()->getMap() as $vid => $vertex) {
9696
$ret[$colors[$vid]][$vid] = $vertex;
9797
}
9898

@@ -113,7 +113,7 @@ public function createGraphGroups()
113113
$colors = $this->getColors();
114114

115115
$graph = $this->graph->createGraphClone();
116-
foreach ($graph->getVertices() as $vid => $vertex) {
116+
foreach ($graph->getVertices()->getMap() as $vid => $vertex) {
117117
$vertex->setGroup($colors[$vid]);
118118
}
119119

src/Complete.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Complete extends BaseGraph
2626
public function isComplete()
2727
{
2828
// copy of array (separate iterator but same vertices)
29-
$c = $vertices = $this->graph->getVertices();
29+
$c = $vertices = $this->graph->getVertices()->getVector();
3030
// from each vertex
3131
foreach ($vertices as $vertex) {
3232
// to each vertex

src/ConnectedComponents.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class ConnectedComponents extends BaseGraph
2424
* @param Vertex $vertex
2525
* @return Graph
2626
* @throws InvalidArgumentException if given vertex is not from same graph
27-
* @uses AlgorithmSearchBreadthFirst::getVerticesIds()
27+
* @uses AlgorithmSearchBreadthFirst::getVertices()
2828
* @uses Graph::createGraphCloneVertices()
2929
*/
3030
public function createGraphComponentVertex(Vertex $vertex)
@@ -36,6 +36,11 @@ public function createGraphComponentVertex(Vertex $vertex)
3636
return $this->graph->createGraphCloneVertices($this->createSearch($vertex)->getVertices());
3737
}
3838

39+
/**
40+
*
41+
* @param Vertex $vertex
42+
* @return SearchBreadthFirst
43+
*/
3944
private function createSearch(Vertex $vertex)
4045
{
4146
$alg = new SearchBreadthFirst($vertex);
@@ -84,20 +89,20 @@ public function isSingle()
8489
*
8590
* @return int number of components
8691
* @uses Graph::getVertices()
87-
* @uses AlgorithmSearchBreadthFirst::getVerticesIds()
92+
* @uses AlgorithmSearchBreadthFirst::getVertices()
8893
*/
8994
public function getNumberOfComponents()
9095
{
9196
$visitedVertices = array();
9297
$components = 0;
9398

9499
// for each vertices
95-
foreach ($this->graph->getVertices() as $vid => $vertex) {
100+
foreach ($this->graph->getVertices()->getMap() as $vid => $vertex) {
96101
// did I visit this vertex before?
97102
if (!isset($visitedVertices[$vid])) {
98103

99104
// get all vertices of this component
100-
$newVertices = $this->createSearch($vertex)->getVerticesIds();
105+
$newVertices = $this->createSearch($vertex)->getVertices()->getIds();
101106

102107
++$components;
103108

@@ -125,7 +130,7 @@ public function createGraphsComponents()
125130
$graphs = array();
126131

127132
// for each vertices
128-
foreach ($this->graph->getVertices() as $vid => $vertex) {
133+
foreach ($this->graph->getVertices()->getMap() as $vid => $vertex) {
129134
// did I visit this vertex before?
130135
if (!isset($visitedVertices[$vid])) {
131136

@@ -134,7 +139,7 @@ public function createGraphsComponents()
134139
$newVertices = $alg->getVertices();
135140

136141
// mark the vertices of this component as visited
137-
foreach ($newVertices as $vid => $unusedVertex) {
142+
foreach ($newVertices->getIds() as $vid) {
138143
$visitedVertices[$vid] = true;
139144
}
140145

src/Degree.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Fhaculty\Graph\Graph;
77
use Fhaculty\Graph\Vertex;
88
use Fhaculty\Graph\Exception\UnexpectedValueException;
9+
use Fhaculty\Graph\Set\Vertices;
910

1011
/**
1112
* Basic algorithms for working with the degrees of Graphs.
@@ -49,25 +50,25 @@ public function getDegree()
4950
*
5051
* @return int
5152
* @throws Exception if graph is empty or directed
52-
* @uses Vertex::getFirst()
53+
* @uses Vertices::getVertexOrder()
5354
* @uses self::getDegreeVertex()
5455
*/
5556
public function getDegreeMin()
5657
{
57-
return $this->getDegreeVertex(Vertex::getFirst($this->graph->getVertices(), Vertex::ORDER_DEGREE));
58+
return $this->getDegreeVertex($this->graph->getVertices()->getVertexOrder(Vertices::ORDER_DEGREE));
5859
}
5960

6061
/**
6162
* get maximum degree of vertices
6263
*
6364
* @return int
6465
* @throws Exception if graph is empty or directed
65-
* @uses Vertex::getFirst()
66+
* @uses Vertices::getVertexOrder()
6667
* @uses self::getDegreeVertex()
6768
*/
6869
public function getDegreeMax()
6970
{
70-
return $this->getDegreeVertex(Vertex::getFirst($this->graph->getVertices(), Vertex::ORDER_DEGREE, true));
71+
return $this->getDegreeVertex($this->graph->getVertices()->getVertexOrder(Vertices::ORDER_DEGREE, true));
7172
}
7273

7374
/**

src/DetectNegativeCycle.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,22 @@ public function hasCycleNegative()
3636
*
3737
* @return Cycle
3838
* @throws UnderflowException if there's no negative cycle
39-
* @uses AlgorithmSpMooreBellmanFord::getVerticesId()
39+
* @uses AlgorithmSpMooreBellmanFord::getVertices()
4040
*/
4141
public function getCycleNegative()
4242
{
4343
// remember vertices already visited, as they can not lead to a new cycle
4444
$verticesVisited = array();
4545
// check for all vertices
46-
foreach ($this->graph->getVertices() as $vid => $vertex) {
46+
foreach ($this->graph->getVertices()->getMap() as $vid => $vertex) {
4747
// skip vertices already visited
4848
if (!isset($verticesVisited[$vid])) {
4949
// start MBF algorithm on current vertex
5050
$alg = new SpMooreBellmanFord($vertex);
5151

5252
try {
5353
// try to get all connected vertices (or throw new cycle)
54-
foreach ($alg->getVerticesId() as $vid) {
54+
foreach ($alg->getVertices()->getIds() as $vid) {
5555
// getting connected vertices succeeded, so skip over all of them
5656
$verticesVisited[$vid] = true;
5757
// no cycle found, check next vertex...

src/Groups.php

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

55
use Fhaculty\Graph\Algorithm\BaseGraph;
66
use Fhaculty\Graph\Graph;
7+
use Fhaculty\Graph\Set\Vertices;
78

89
class Groups extends BaseGraph
910
{
@@ -66,21 +67,21 @@ public function getGroups()
6667
}
6768

6869
/**
69-
* get array of all vertices in the given group
70+
* get set of all Vertices in the given group
7071
*
7172
* @param int $group
72-
* @return Vertex[]
73+
* @return Vertices
7374
* @uses Vertex::getGroup()
7475
*/
7576
public function getVerticesGroup($group)
7677
{
7778
$vertices = array();
78-
foreach ($this->graph->getVertices() as $vid => $vertex) {
79+
foreach ($this->graph->getVertices()->getMap() as $vid => $vertex) {
7980
if ($vertex->getGroup() === $group) {
8081
$vertices[$vid] = $vertex;
8182
}
8283
}
8384

84-
return $vertices;
85+
return new Vertices($vertices);
8586
}
8687
}

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/MaximumMatching/Flow.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ public function getEdges()
2929
// create temporary flow graph with supersource and supersink
3030
$graphFlow = $this->graph->createGraphCloneEdgeless();
3131

32-
// get all vertices
33-
$vertices = $graphFlow->getVertices();
34-
// above $vertices does NOT contain supersource and supersink, because
35-
// we want to skip over them as they do not have a partition assigned
36-
3732
$superSource = $graphFlow->createVertex()->setLayoutAttribute('label', 's*');
3833
$superSink = $graphFlow->createVertex()->setLayoutAttribute('label', 't*');
3934

@@ -42,7 +37,10 @@ public function getEdges()
4237
$groupB = $groups[1];
4338

4439
// connect supersource s* to set A and supersink t* to set B
45-
foreach ($vertices as $vertex) {
40+
foreach ($graphFlow->getVertices() as $vertex) {
41+
// we want to skip over supersource & supersink as they do not have a partition assigned
42+
if ($vertex === $superSource || $vertex === $superSink) continue;
43+
4644
$group = $vertex->getGroup();
4745

4846
// source

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 {

0 commit comments

Comments
 (0)