Skip to content

Commit

Permalink
Merge pull request #175 from clue-labs/createedge
Browse files Browse the repository at this point in the history
Move creating edges to `Graph`
  • Loading branch information
clue authored Oct 4, 2019
2 parents e9e3be2 + b2bc0d8 commit 9ce805c
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 112 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ $madrid = $graph->createVertex('Madrid');
$cologne = $graph->createVertex('Cologne');

// build some roads
$cologne->createEdgeTo($madrid);
$madrid->createEdgeTo($rome);
$graph->createEdgeDirected($cologne, $madrid);
$graph->createEdgeDirected($madrid, $rome);
// create loop
$rome->createEdgeTo($rome);
$graph->createEdgeDirected($rome, $rome);
```

Let's see which city (Vertex) has a road (i.e. an edge pointing) to Rome:
Expand Down
8 changes: 5 additions & 3 deletions src/Edge/Directed.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Graphp\Graph\Edge;

use Graphp\Graph\Exception\InvalidArgumentException;
use Graphp\Graph\Graph;
use Graphp\Graph\Set\Vertices;
use Graphp\Graph\Vertex;

Expand All @@ -23,12 +24,13 @@ class Directed extends Base
private $to;

/**
* create a new directed Edge from Vertex $from to Vertex $to
* [Internal] Create a new directed Edge from Vertex $from to Vertex $to
*
* @param Vertex $from start/source Vertex
* @param Vertex $to end/target Vertex
* @see Vertex::createEdgeTo() to create directed edges
* @see Vertex::createEdge() to create undirected edges
* @see Graph::createEdgeDirected() to create directed edges
* @see Graph::createEdgeUndirected() to create undirected edges
* @internal
*/
public function __construct(Vertex $from, Vertex $to)
{
Expand Down
7 changes: 5 additions & 2 deletions src/Edge/Undirected.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Graphp\Graph\Edge;

use Graphp\Graph\Exception\InvalidArgumentException;
use Graphp\Graph\Graph;
use Graphp\Graph\Vertex;
use Graphp\Graph\Set\Vertices;

Expand All @@ -23,11 +24,13 @@ class Undirected extends Base
private $b;

/**
* create a new undirected edge between given vertices
* [Internal] Create a new undirected edge between given vertices
*
* @param Vertex $a
* @param Vertex $b
* @see Vertex::createEdge() instead
* @see Graph::createEdgeUndirected() to create undirected edges
* @see Graph::createEdgeDirected() to create directed edges
* @internal
*/
public function __construct(Vertex $a, Vertex $b)
{
Expand Down
47 changes: 41 additions & 6 deletions src/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Graphp\Graph\Attribute\AttributeBagReference;
use Graphp\Graph\Edge\Base as Edge;
use Graphp\Graph\Edge\Directed as EdgeDirected;
use Graphp\Graph\Edge\Undirected as EdgeUndirected;
use Graphp\Graph\Exception\BadMethodCallException;
use Graphp\Graph\Exception\InvalidArgumentException;
use Graphp\Graph\Exception\OutOfBoundsException;
Expand Down Expand Up @@ -111,7 +112,7 @@ public function createGraphCloneEdgeless()
$graph->getAttributeBag()->setAttributes($this->getAttributeBag()->getAttributes());
// TODO: set additional graph attributes
foreach ($this->getVertices() as $originalVertex) {
$vertex = $graph->createVertexClone($originalVertex);
$graph->createVertexClone($originalVertex);
// $graph->vertices[$vid] = $vertex;
}

Expand Down Expand Up @@ -169,6 +170,40 @@ public function createGraphCloneVertices($vertices)
return $graph;
}

/**
* Creates a new undirected (bidirectional) edge between the given two vertices.
*
* @param Vertex $a
* @param Vertex $b
* @return EdgeUndirected
* @throws InvalidArgumentException
*/
public function createEdgeUndirected(Vertex $a, Vertex $b)
{
if ($a->getGraph() !== $this) {
throw new InvalidArgumentException('Vertices have to be within this graph');
}

return new EdgeUndirected($a, $b);
}

/**
* Creates a new directed edge from the given start vertex to given target vertex
*
* @param Vertex $source source vertex
* @param Vertex $target target vertex
* @return EdgeDirected
* @throws InvalidArgumentException
*/
public function createEdgeDirected(Vertex $source, Vertex $target)
{
if ($source->getGraph() !== $this) {
throw new InvalidArgumentException('Vertices have to be within this graph');
}

return new EdgeDirected($source, $target);
}

/**
* create new clone of the given edge between adjacent vertices
*
Expand Down Expand Up @@ -202,8 +237,8 @@ public function createEdgeCloneInverted(Edge $originalEdge)
* @return Edge new edge in this graph
* @uses Edge::getVertices()
* @uses Graph::getVertex()
* @uses Vertex::createEdge() to create a new undirected edge if given edge was undrected
* @uses Vertex::createEdgeTo() to create a new directed edge if given edge was directed
* @uses Vertex::createEdgeUndirected() to create a new undirected edge if given edge was undrected
* @uses Vertex::createEdgeDirected() to create a new directed edge if given edge was directed
* @uses Edge::getWeight()
* @uses Edge::setWeight()
* @uses Edge::getFlow()
Expand All @@ -221,10 +256,10 @@ private function createEdgeCloneInternal(Edge $originalEdge, $ia, $ib)
$b = $this->getVertex($ends[$ib]);

if ($originalEdge instanceof EdgeDirected) {
$newEdge = $a->createEdgeTo($b);
$newEdge = $this->createEdgeDirected($a, $b);
} else {
// create new edge between new a and b
$newEdge = $a->createEdge($b);
$newEdge = $this->createEdgeUndirected($a, $b);
}
// TODO: copy edge attributes
$newEdge->getAttributeBag()->setAttributes($originalEdge->getAttributeBag()->getAttributes());
Expand Down Expand Up @@ -337,7 +372,7 @@ public function addVertex(Vertex $vertex)
* @param Edge $edge instance of the new Edge
* @return void
* @internal
* @see Vertex::createEdge() instead!
* @see Graph::createEdgeUndirected() instead!
*/
public function addEdge(Edge $edge)
{
Expand Down
29 changes: 1 addition & 28 deletions src/Vertex.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Graphp\Graph\Attribute\AttributeBagReference;
use Graphp\Graph\Edge\Base as Edge;
use Graphp\Graph\Edge\Directed as EdgeDirected;
use Graphp\Graph\Edge\Undirected as EdgeUndirected;
use Graphp\Graph\Exception\BadMethodCallException;
use Graphp\Graph\Exception\InvalidArgumentException;
use Graphp\Graph\Set\Edges;
Expand Down Expand Up @@ -126,39 +125,13 @@ public function getId()
return $this->id;
}

/**
* create new directed edge from this start vertex to given target vertex
*
* @param Vertex $vertex target vertex
* @return EdgeDirected
* @throws InvalidArgumentException
* @uses Graph::addEdge()
*/
public function createEdgeTo(Vertex $vertex)
{
return new EdgeDirected($this, $vertex);
}

/**
* add new undirected (bidirectional) edge between this vertex and given vertex
*
* @param Vertex $vertex
* @return EdgeUndirected
* @throws InvalidArgumentException
* @uses Graph::addEdge()
*/
public function createEdge(Vertex $vertex)
{
return new EdgeUndirected($this, $vertex);
}

/**
* add the given edge to list of connected edges (MUST NOT be called manually)
*
* @param Edge $edge
* @return void
* @internal
* @see self::createEdge() instead!
* @see Graph::createEdgeUndirected() instead!
*/
public function addEdge(Edge $edge)
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Edge/EdgeAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function setUp()
$graph->createVertex(2);

// 1 -> 2
$this->edge = $graph->getVertex(1)->createEdge($graph->getVertex(2));
$this->edge = $graph->createEdgeUndirected($graph->getVertex(1), $graph->getVertex(2));
}

public function testCanSetFlowAndCapacity()
Expand Down
6 changes: 3 additions & 3 deletions tests/Edge/EdgeBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abstract class EdgeBaseTest extends AbstractAttributeAwareTest
*/
protected $edge;

abstract protected function createEdge();
abstract protected function createEdgeUndirected();

/**
* @return Edge
Expand All @@ -31,7 +31,7 @@ public function setUp()
$this->v1 = $this->graph->createVertex(1);
$this->v2 = $this->graph->createVertex(2);

$this->edge = $this->createEdge();
$this->edge = $this->createEdgeUndirected();
}

public function testEdgeVertices()
Expand Down Expand Up @@ -113,6 +113,6 @@ public function testRemoveWithLoop()

protected function createAttributeAware()
{
return $this->createEdge();
return $this->createEdgeUndirected();
}
}
6 changes: 3 additions & 3 deletions tests/Edge/EdgeDirectedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

class EdgeDirectedTest extends EdgeBaseTest
{
protected function createEdge()
protected function createEdgeUndirected()
{
// 1 -> 2
return $this->v1->createEdgeTo($this->v2);
return $this->graph->createEdgeDirected($this->v1, $this->v2);
}

protected function createEdgeLoop()
{
// 1 --\
// ^ |
// \---/
return $this->v1->createEdgeTo($this->v1);
return $this->graph->createEdgeDirected($this->v1, $this->v1);
}

public function testVerticesEnds()
Expand Down
6 changes: 3 additions & 3 deletions tests/Edge/EdgeUndirectedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

class EdgeUndirectedTest extends EdgeBaseTest
{
protected function createEdge()
protected function createEdgeUndirected()
{
// 1 -- 2
return $this->v1->createEdge($this->v2);
return $this->graph->createEdgeUndirected($this->v1, $this->v2);
}

protected function createEdgeLoop()
{
// 1 --\
// | |
// \---/
return $this->v1->createEdge($this->v1);
return $this->graph->createEdgeUndirected($this->v1, $this->v1);
}

public function testVerticesEnds()
Expand Down
Loading

0 comments on commit 9ce805c

Please sign in to comment.