Skip to content

Commit 3bebe1c

Browse files
committed
Use Set\Vertices for accessing Graph's Vertices
1 parent 88c09b7 commit 3bebe1c

File tree

5 files changed

+18
-16
lines changed

5 files changed

+18
-16
lines changed

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 Vertex::getDegree()
5455
*/
5556
public function getDegreeMin()
5657
{
57-
return Vertex::getFirst($this->graph->getVertices(), Vertex::ORDER_DEGREE)->getDegree();
58+
return $this->graph->getVertices()->getVertexOrder(Vertices::ORDER_DEGREE)->getDegree();
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 Vertex::getDegree()
6768
*/
6869
public function getDegreeMax()
6970
{
70-
return Vertex::getFirst($this->graph->getVertices(), Vertex::ORDER_DEGREE, true)->getDegree();
71+
return $this->graph->getVertices()->getVertexOrder(Vertices::ORDER_DEGREE, true)->getDegree();
7172
}
7273

7374
/**

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/TopologicalSort.php

Lines changed: 3 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\Exception\UnexpectedValueException;
77
use Fhaculty\Graph\Vertex;
8+
use Fhaculty\Graph\Set\Vertices;
89
use Fhaculty\Graph\Graph;
910

1011
/**
@@ -32,11 +33,11 @@ public function getVertices()
3233
// TODO: find alternative to recursive algorithm to avoid hitting recursion limit with ~100 nodes
3334
// TODO: avoid having to reverse all vertices multiple times
3435

35-
foreach(array_reverse($this->graph->getVertices()) as $vertex) {
36+
foreach(array_reverse($this->graph->getVertices()->getVector()) as $vertex) {
3637
$this->visit($vertex, $visited, $tsl);
3738
}
3839

39-
return array_reverse($tsl, true);
40+
return new Vertices(array_reverse($tsl, true));
4041
}
4142

4243
protected function visit(Vertex $vertex, array &$visited, array &$tsl)

tests/ShortestPath/BreadthFirstTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Fhaculty\Graph\Edge\Base as Edge;
77
use Fhaculty\Graph\Algorithm\ShortestPath\BreadthFirst;
88
use Fhaculty\Graph\Loader\CompleteGraph;
9+
use Fhaculty\Graph\Set\Vertices;
910

1011
class BreadthFirstTest extends PHPUnit_Framework_TestCase
1112
{
@@ -24,7 +25,7 @@ public function testOne()
2425
$edge->destroy();
2526
}
2627

27-
$start = Vertex::getFirst($graph, Vertex::ORDER_RANDOM);
28+
$start = $graph->getVertices()->getVertexOrder(Vertices::ORDER_RANDOM);
2829
$start->setLayoutAttribute('shape', 'doublecircle');
2930

3031
// actually start breadth search

tests/TopologicalSortTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public function testGraphEmpty()
1313

1414
$alg = new TopologicalSort($graph);
1515

16-
$this->assertSame(array(), $alg->getVertices());
16+
$this->assertInstanceOf('Fhaculty\Graph\Set\Vertices', $alg->getVertices());
17+
$this->assertTrue($alg->getVertices()->isEmpty());
1718
}
1819

1920
public function testGraphIsolated()
@@ -24,7 +25,7 @@ public function testGraphIsolated()
2425

2526
$alg = new TopologicalSort($graph);
2627

27-
$this->assertSame(array(1 => $graph->getVertex(1), 2 => $graph->getVertex(2)), $alg->getVertices());
28+
$this->assertSame(array($graph->getVertex(1), $graph->getVertex(2)), $alg->getVertices()->getVector());
2829
}
2930

3031
public function testGraphSimple()
@@ -34,7 +35,7 @@ public function testGraphSimple()
3435

3536
$alg = new TopologicalSort($graph);
3637

37-
$this->assertSame(array(1 => $graph->getVertex(1), 2 => $graph->getVertex(2)), $alg->getVertices());
38+
$this->assertSame(array($graph->getVertex(1), $graph->getVertex(2)), $alg->getVertices()->getVector());
3839
}
3940

4041
/**

0 commit comments

Comments
 (0)