Skip to content

Commit

Permalink
Merge pull request #75 from clue/remove-set
Browse files Browse the repository at this point in the history
Remove base `Set`
  • Loading branch information
clue committed Sep 11, 2013
2 parents 3e61948 + 2596ea0 commit 02046da
Show file tree
Hide file tree
Showing 29 changed files with 103 additions and 124 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ you spot any mistakes.
accordingly.
([#72](https://github.com/clue/graph/issues/72))

* BC break: Remove all occurances of `getNumberOfVertices()` and
`getNumberOfEdges()` ([#75](https://github.com/clue/graph/issues/75) and
[#48](https://github.com/clue/graph/issues/48)):

| Old name | New name |
|---|---|
| `$set->getNumberOfVertices()` | `count($set->getVertices())` |
| `$set->getNumberOfEdges()` | `count($set->getEdges())` |

* BC break: Replace base `Set` class with `Set\DualAggregate` interface. This
is unlikely to affect you, but might potentially break your custom
inheritance or polymorphism for algorithms.
([#75](https://github.com/clue/graph/issues/75))

* Feature: Add `Algorithm\ShortestPath\Base::hasVertex(Vertex $vertex)` to check whether
a path to the given Vertex exists ([#62](https://github.com/clue/graph/issues/62)).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Fhaculty\Graph\Algorithm;

use Fhaculty\Graph\Algorithm\Base;
use Fhaculty\Graph\Set;
use Fhaculty\Graph\Set\DualAggregate;
use Fhaculty\Graph\Graph;
use Fhaculty\Graph\Walk;

Expand All @@ -12,12 +12,12 @@
*
* @see Set
*/
abstract class BaseSet extends Base
abstract class BaseDual extends Base
{
/**
* Set to operate on
*
* @var Set
* @var DualAggregate
*/
protected $set;

Expand All @@ -26,7 +26,7 @@ abstract class BaseSet extends Base
*
* @param Graph|Walk|Set $graphOrWalk either the Graph or Walk to operate on (or the common base class Set)
*/
public function __construct(Set $graphOrWalk)
public function __construct(DualAggregate $graphOrWalk)
{
$this->set = $graphOrWalk;
}
Expand Down
3 changes: 1 addition & 2 deletions lib/Fhaculty/Graph/Algorithm/ConnectedComponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ private function createSearch(Vertex $vertex)
* connected here.
*
* @return boolean
* @uses AlgorithmSearchBreadthFirst::getNumberOfVertices()
* @see self::getNumberOfComponents()
*/
public function isSingle()
Expand All @@ -79,7 +78,7 @@ public function isSingle()
}
$alg = $this->createSearch($vertex);

return ($this->graph->getNumberOfVertices() === $alg->getNumberOfVertices());
return (count($this->graph->getVertices()) === count($alg->getVertices()));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Fhaculty/Graph/Algorithm/Directed.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @link http://en.wikipedia.org/wiki/Glossary_of_graph_theory#Direction
* @link http://en.wikipedia.org/wiki/Digraph_%28mathematics%29
*/
class Directed extends BaseSet
class Directed extends BaseDual
{
/**
* checks whether the graph has any directed edges (aka digraph)
Expand Down
2 changes: 1 addition & 1 deletion lib/Fhaculty/Graph/Algorithm/Flow.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @link http://en.wikipedia.org/wiki/Flow_network
* @see Algorithm\Balance
*/
class Flow extends BaseSet
class Flow extends BaseDual
{
/**
* check if this graph has any flow set (any edge has a non-NULL flow)
Expand Down
4 changes: 2 additions & 2 deletions lib/Fhaculty/Graph/Algorithm/Loop.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Fhaculty\Graph\Algorithm;

use Fhaculty\Graph\Algorithm\BaseSet;
use Fhaculty\Graph\Algorithm\BaseDual;
use Fhaculty\Graph\Edge\Base as Edge;
use Fhaculty\Graph\Vertex;

Expand All @@ -14,7 +14,7 @@
*
* @link http://en.wikipedia.org/wiki/Loop_%28graph_theory%29
*/
class Loop extends BaseSet
class Loop extends BaseDual
{
/**
* checks whether this graph has any loops (edges from vertex to itself)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,12 @@ public function createGraph()
* @param Graph $graph
* @return boolean
* @throws Exception if given graph is not a clone of the original graph (each vertex has to be present in both graphs)
* @uses Graph::getNumberOfVertices()
* @uses Graph::getBalanace()
* @uses Graph::getVertex()
*/
private function isBalanceReached(Graph $graph)
{
if ($graph->getNumberOfVertices() !== $this->graph->getNumberOfVertices()) {
if (count($graph->getVertices()) !== count($this->graph->getVertices())) {
throw new DomainException('Given graph does not appear to be a clone of input graph');
}
foreach ($this->graph->getVertices()->getMap() as $vid => $vertex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function getEdges()

// definition of spanning tree: number of edges = number of vertices - 1
// above algorithm does not check isolated edges or may otherwise return multiple connected components => force check
if (count($returnEdges) !== ($this->graph->getNumberOfVertices() - 1)) {
if (count($returnEdges) !== (count($this->graph->getVertices()) - 1)) {
throw new UnexpectedValueException('Graph is not connected');
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Fhaculty/Graph/Algorithm/MinimumSpanningTree/Prim.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function getEdges()
$returnEdges = array();

// iterate n-1 times (per definition, resulting MST MUST have n-1 edges)
for ($i = 0, $n = $this->startVertex->getGraph()->getNumberOfVertices() - 1; $i < $n; ++$i) {
for ($i = 0, $n = count($this->startVertex->getGraph()->getVertices()) - 1; $i < $n; ++$i) {
$markInserted[$vertexCurrent->getId()] = true;

// get unvisited vertex of the edge and add edges from new vertex
Expand Down
2 changes: 1 addition & 1 deletion lib/Fhaculty/Graph/Algorithm/Property/GraphProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ public function isNull()
*/
public function isTrivial()
{
return ($this->graph->getEdges()->isEmpty() && $this->graph->getNumberOfVertices() === 1);
return ($this->graph->getEdges()->isEmpty() && count($this->graph->getVertices()) === 1);
}
}
6 changes: 3 additions & 3 deletions lib/Fhaculty/Graph/Algorithm/Property/WalkProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function hasCycle()
*/
public function isLoop()
{
return ($this->walk->getNumberOfEdges() === 1 && $this->isCycle());
return (count($this->walk->getEdges()) === 1 && $this->isCycle());
}

/**
Expand Down Expand Up @@ -214,7 +214,7 @@ public function hasLoop()
public function isDigon()
{
// exactly 2 edges
return ($this->walk->getNumberOfEdges() === 2 &&
return (count($this->walk->getEdges()) === 2 &&
// no duplicate edges
!$this->hasArrayDuplicates($this->walk->getEdges()->getVector()) &&
// exactly two distinct vertices
Expand All @@ -238,7 +238,7 @@ public function isDigon()
public function isTriangle()
{
// exactly 3 (implicitly distinct) edges
return ($this->walk->getNumberOfEdges() === 3 &&
return (count($this->walk->getEdges()) === 3 &&
// exactly three distinct vertices
count($this->walk->getVertices()->getVerticesDistinct()) === 3 &&
// this is actually a cycle
Expand Down
11 changes: 0 additions & 11 deletions lib/Fhaculty/Graph/Algorithm/Search/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,6 @@ protected function getVerticesAdjacent(Vertex $vertex)
}
}

/**
* get total number of vertices the start vertex is connected to
*
* @return int
* @uses AlgorithmSearch::getVertices()
*/
public function getNumberOfVertices()
{
return count($this->getVertices());
}

/**
* get set of all Vertices that can be reached from start vertex
*
Expand Down
2 changes: 1 addition & 1 deletion lib/Fhaculty/Graph/Algorithm/ShortestPath/Dijkstra.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function getEdges()
$isFirst = true;

// Repeat until all vertices have been marked
$totalCountOfVertices = $this->vertex->getGraph()->getNumberOfVertices();
$totalCountOfVertices = count($this->vertex->getGraph()->getVertices());
for ($i = 0; $i < $totalCountOfVertices; ++$i) {
$currentVertex = NULL;
$currentVertexId = NULL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function getEdges()
// the usal algorithm says we repeat (n-1) times.
// but because we also want to check for loop edges on the start vertex,
// we have to add an additional step:
$numSteps = $this->vertex->getGraph()->getNumberOfVertices();
$numSteps = count($this->vertex->getGraph()->getVertices());
$edges = $this->vertex->getGraph()->getEdges();
$changed = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ protected function getGraph()
*/
public function getEdges()
{
$this->numEdges = $this->graph->getNumberOfVertices();
$this->numEdges = count($this->graph->getVertices());
if ($this->numEdges < 3) {
throw new UnderflowException('Needs at least 3 vertices');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function getEdges()
{
$returnEdges = array();

$n = $this->vertex->getGraph()->getNumberOfVertices();
$n = count($this->vertex->getGraph()->getVertices());

$vertex = $this->vertex;
$visitedVertices = array($vertex->getId() => true);
Expand Down
2 changes: 1 addition & 1 deletion lib/Fhaculty/Graph/Algorithm/Tree/BaseDirected.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function isTree()
}

// check number of vertices reachable from root should match total number of vertices
return ($num === $this->graph->getNumberOfVertices());
return ($num === count($this->graph->getVertices()));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Fhaculty/Graph/Algorithm/Tree/Undirected.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function isTree()
return false;
}

return (count($vertices) === $this->graph->getNumberOfVertices());
return (count($vertices) === count($this->graph->getVertices()));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Fhaculty/Graph/Algorithm/Weight.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
* @link http://en.wikipedia.org/wiki/Glossary_of_graph_theory#Weighted_graphs_and_networks
*/
class Weight extends BaseSet
class Weight extends BaseDual
{
/**
* checks whether this graph has any weighted edges
Expand Down
3 changes: 2 additions & 1 deletion lib/Fhaculty/Graph/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
use Fhaculty\Graph\Set\Vertices;
use Fhaculty\Graph\Set\VerticesMap;
use Fhaculty\Graph\Set\Edges;
use Fhaculty\Graph\Set\DualAggregate;

class Graph extends Set
class Graph implements DualAggregate
{
/**
* @var ExporterInterface|null
Expand Down
52 changes: 0 additions & 52 deletions lib/Fhaculty/Graph/Set.php

This file was deleted.

30 changes: 30 additions & 0 deletions lib/Fhaculty/Graph/Set/DualAggregate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Fhaculty\Graph\Set;

use Fhaculty\Graph\Set\VerticesAggregate;
use Fhaculty\Graph\Set\EdgesAggregate;

/**
* DualAggregate provides access to both its Vertices and its Edges
*
* This is the simple base interface for any Graph-like structure / data type
* which contains a Set of Edges and a Set of Vertices, such as the Graph class
* itself and the Walk class.
*/
interface DualAggregate extends VerticesAggregate, EdgesAggregate
{
/**
* returns a set of ALL Edges in this graph
*
* @return Edges
*/
// abstract public function getEdges();

/**
* returns a set of all Vertices
*
* @return Vertices
*/
// abstract public function getVertices();
}
5 changes: 2 additions & 3 deletions lib/Fhaculty/Graph/Walk.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
namespace Fhaculty\Graph;

use Fhaculty\Graph\Set\Edges;
use Fhaculty\Graph\Set\EdgesAggregate;
use Fhaculty\Graph\Set\Vertices;
use Fhaculty\Graph\Set\VerticesAggregate;
use Fhaculty\Graph\Edge\Base as Edge;
use Fhaculty\Graph\Exception\UnderflowException;
use Fhaculty\Graph\Exception\InvalidArgumentException;
use Fhaculty\Graph\Set\DualAggregate;

/**
* Base Walk class
Expand All @@ -20,7 +19,7 @@
* @link http://en.wikipedia.org/wiki/Glossary_of_graph_theory#Walks
* @see Fhaculty\Graph\Algorithm\Property\WalkProperty for checking special cases, such as cycles, loops, closed trails, etc.
*/
class Walk extends Set implements VerticesAggregate, EdgesAggregate
class Walk implements DualAggregate
{
/**
* construct new walk from given start vertex and given array of edges
Expand Down

0 comments on commit 02046da

Please sign in to comment.