Skip to content

Commit

Permalink
Merge pull request #76 from clue/remove-graph-getvertexfirst
Browse files Browse the repository at this point in the history
Remove Graph::getVertexFirst()
  • Loading branch information
clue committed Dec 27, 2013
2 parents b23aa88 + 471c512 commit fe95b81
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 60 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ you spot any mistakes.
* Fix: Throwing an `UnexpectedValueException` if writing GraphViz Dot script
to a temporary file fails and remove its debugging output
([#77](https://github.com/clue/graph/issues/77) and [#78](https://github.com/clue/graph/issues/78) @Metabor)

* BC break: Remove unneeded alias definitions of `getVertexFirst()`,
`getVertexSource()` and `getVertexTarget()`
[#76]https://github.com/clue/graph/issues/76)):

| Old name | New name |
|---|---|
| `Graph::getVertexFirst()` | `Graph::getVertices()->getVertexFirst()` |
| `Walk::getVertexSource()` | `Walk::getVertices()->getVertexFirst()` |
| `Walk::getVertexTarget()` | `Walk::getVertices()->getVertexLast()` |

## 0.7.0 (2013-09-11)

Expand Down
2 changes: 1 addition & 1 deletion lib/Fhaculty/Graph/Algorithm/ConnectedComponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private function createSearch(Vertex $vertex)
public function isSingle()
{
try {
$vertex = $this->graph->getVertexFirst();
$vertex = $this->graph->getVertices()->getVertexFirst();
}
catch (UnderflowException $e) {
// no first vertex => empty graph => has zero components
Expand Down
2 changes: 1 addition & 1 deletion lib/Fhaculty/Graph/Algorithm/Degree.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Degree extends BaseGraph
public function getDegree()
{
// get initial degree of any start vertex to compare others to
$degree = $this->getDegreeVertex($this->graph->getVertexFirst());
$degree = $this->getDegreeVertex($this->graph->getVertices()->getVertexFirst());

foreach ($this->graph->getVertices() as $vertex) {
/** @var $vertex Vertex */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ public function setUpperLimitMst()

protected function getVertexStart()
{
return $this->graph->getVertexFirst();
// actual start doesn't really matter as we're only considering complete graphs here
return $this->graph->getVertices()->getVertexFirst();
}

protected function getGraph()
Expand All @@ -116,8 +117,7 @@ public function getEdges()
// numEdges 3-12 should work

$this->bestWeight = $this->upperLimit;
// actual start doesn't really matter as we're only considering complete graphs here
$this->startVertex = $this->graph->getVertexFirst();
$this->startVertex = $this->getVertexStart();

$result = $this->step($this->startVertex,
0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(Graph $inputGraph)

protected function getVertexStart()
{
return $this->graph->getVertexFirst();
return $this->graph->getVertices()->getVertexFirst();
}

protected function getGraph()
Expand All @@ -43,7 +43,7 @@ public function getEdges()
$minimumSpanningTreeAlgorithm = new MstKruskal($this->graph);
$minimumSpanningTree = $minimumSpanningTreeAlgorithm->createGraph();

$alg = new SearchDepthFirst($minimumSpanningTree->getVertexFirst());
$alg = new SearchDepthFirst($minimumSpanningTree->getVertices()->getVertexFirst());
// Depth first search in minmum spanning tree (for the eulerian path)

$startVertex = NULL;
Expand Down
4 changes: 2 additions & 2 deletions lib/Fhaculty/Graph/Algorithm/Tree/Undirected.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Undirected extends Tree
*
* @return boolean
* @uses Vertices::isEmpty() to skip null Graphs (a Graph with no Vertices is *NOT* a valid tree)
* @uses Graph::getVertexFirst() to get get get random "root" Vertex to start search from
* @uses Vertices::getVertexFirst() to get get get random "root" Vertex to start search from
* @uses self::getVerticesSubtreeRecursive() to count number of vertices connected to root
*/
public function isTree()
Expand All @@ -60,7 +60,7 @@ public function isTree()
}

// every vertex can represent a root vertex, so just pick one
$root = $this->graph->getVertexFirst();
$root = $this->graph->getVertices()->getVertexFirst();

$vertices = array();
try {
Expand Down
17 changes: 0 additions & 17 deletions lib/Fhaculty/Graph/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,23 +332,6 @@ public function hasVertex($id)
return $this->vertices->hasVertexId($id);
}

/**
* return first vertex found
*
* some algorithms do not need a particular vertex, but merely a (random)
* starting point. this is a convenience function to just pick the first
* vertex from the list of known vertices.
*
* @return Vertex first vertex found in this graph
* @throws UnderflowException if Graph has no vertices
* @see Vertices::getVertexOrder() if you need to apply ordering first
* @uses Vertices::getVertexFirst()
*/
public function getVertexFirst()
{
return $this->vertices->getVertexFirst();
}

/**
* adds a new Edge to the Graph (MUST NOT be called manually!)
*
Expand Down
35 changes: 11 additions & 24 deletions lib/Fhaculty/Graph/Walk.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public static function factoryCycleFromVertices($vertices, $by = null, $desc = f
throw new InvalidArgumentException('Cycle with no edges can not exist');
}

if ($cycle->getVertexSource() !== $cycle->getVertexTarget()) {
if ($cycle->getVertices()->getVertexFirst() !== $cycle->getVertices()->getVertexLast()) {
throw new InvalidArgumentException('Cycle has to start and end at the same vertex');
}

Expand All @@ -167,7 +167,7 @@ public static function factoryCycleFromEdges($edges, Vertex $startVertex)
$cycle = self::factoryFromEdges($edges, $startVertex);

// ensure this walk is actually a cycle by checking start = end
if ($cycle->getVertexTarget() !== $startVertex) {
if ($cycle->getVertices()->getVertexLast() !== $startVertex) {
throw new InvalidArgumentException('The given array of edges does not represent a cycle');
}

Expand Down Expand Up @@ -196,12 +196,13 @@ protected function __construct($vertices, $edges)
* return original graph
*
* @return Graph
* @uses Walk::getVertexSource()
* @uses self::getVertices()
* @uses Vertices::getVertexFirst()
* @uses Vertex::getGraph()
*/
public function getGraph()
{
return $this->getVertexSource()->getGraph();
return $this->getVertices()->getVertexFirst()->getGraph();
}

/**
Expand Down Expand Up @@ -248,33 +249,19 @@ public function getEdges()
* If you need to return set a of all unique Vertices of walk, use
* `Walk::getVertices()->getVerticesDistinct()` instead.
*
* If you need to return the source vertex (first vertex of walk), use
* `Walk::getVertices()->getVertexFirst()` instead.
*
* If you need to return the target/destination vertex (last vertex of walk), use
* `Walk::getVertices()->getVertexLast()` instead.
*
* @return Vertices
*/
public function getVertices()
{
return $this->vertices;
}

/**
* return source vertex (first vertex of walk)
*
* @return Vertex
*/
public function getVertexSource()
{
return $this->vertices->getVertexFirst();
}

/**
* return target vertex (last vertex of walk)
*
* @return Vertex
*/
public function getVertexTarget()
{
return $this->vertices->getVertexLast();
}

/**
* get alternating sequence of vertex, edge, vertex, edge, ..., vertex
*
Expand Down
2 changes: 1 addition & 1 deletion tests/Fhaculty/Graph/Algorithm/Tree/BaseDirectedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function testEmptyGraphDoesNotHaveHeight(BaseDirected $tree)
public function testGraphTree()
{
$graph = $this->createGraphTree();
$root = $graph->getVertexFirst();
$root = $graph->getVertices()->getVertexFirst();

$nonRoot = $graph->getVertices()->getMap();
unset($nonRoot[$root->getId()]);
Expand Down
18 changes: 9 additions & 9 deletions tests/Fhaculty/Graph/WalkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public function testWalkPath()

$this->assertEquals(3, count($walk->getVertices()));
$this->assertEquals(2, count($walk->getEdges()));
$this->assertSame($v1, $walk->getVertexSource());
$this->assertSame($v3, $walk->getVertexTarget());
$this->assertSame($v1, $walk->getVertices()->getVertexFirst());
$this->assertSame($v3, $walk->getVertices()->getVertexLast());
$this->assertSame(array($v1, $e1, $v2, $e2, $v3), $walk->getAlternatingSequence());
$this->assertTrue($walk->isValid());

Expand All @@ -47,7 +47,7 @@ public function testWalkPath()
public function testWalkPathInvalidateByDestroyingVertex(Walk $walk)
{
// delete v3
$walk->getVertexTarget()->destroy();
$walk->getVertices()->getVertexLast()->destroy();

$this->assertFalse($walk->isValid());
}
Expand All @@ -67,8 +67,8 @@ public function testWalkWithinGraph()

$this->assertEquals(2, count($walk->getVertices()));
$this->assertEquals(1, count($walk->getEdges()));
$this->assertSame($v1, $walk->getVertexSource());
$this->assertSame($v2, $walk->getVertexTarget());
$this->assertSame($v1, $walk->getVertices()->getVertexFirst());
$this->assertSame($v2, $walk->getVertices()->getVertexLast());
$this->assertSame(array($v1, $e1, $v2), $walk->getAlternatingSequence());
$this->assertTrue($walk->isValid());

Expand Down Expand Up @@ -99,8 +99,8 @@ public function testWalkLoop()

$this->assertEquals(2, count($walk->getVertices()));
$this->assertEquals(1, count($walk->getEdges()));
$this->assertSame($v1, $walk->getVertexSource());
$this->assertSame($v1, $walk->getVertexTarget());
$this->assertSame($v1, $walk->getVertices()->getVertexFirst());
$this->assertSame($v1, $walk->getVertices()->getVertexLast());
$this->assertTrue($walk->isValid());

return $walk;
Expand Down Expand Up @@ -132,8 +132,8 @@ public function testWalkLoopCycle()

$this->assertEquals(2, count($walk->getVertices()));
$this->assertEquals(1, count($walk->getEdges()));
$this->assertSame($v1, $walk->getVertexSource());
$this->assertSame($v1, $walk->getVertexTarget());
$this->assertSame($v1, $walk->getVertices()->getVertexFirst());
$this->assertSame($v1, $walk->getVertices()->getVertexLast());
$this->assertTrue($walk->isValid());
}

Expand Down

0 comments on commit fe95b81

Please sign in to comment.