Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions src/Edge/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,28 +257,6 @@ public function destroy()
}
}

/**
* create new clone of this edge between adjacent vertices
*
* @return self new edge
* @uses Graph::createEdgeClone()
*/
public function createEdgeClone()
{
return $this->getGraph()->createEdgeClone($this);
}

/**
* create new clone of this edge inverted (in opposite direction) between adjacent vertices
*
* @return self new edge
* @uses Graph::createEdgeCloneInverted()
*/
public function createEdgeCloneInverted()
{
return $this->getGraph()->createEdgeCloneInverted($this);
}

/**
* do NOT allow cloning of objects
*
Expand Down
25 changes: 0 additions & 25 deletions src/Walk.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,31 +203,6 @@ public function getGraph()
return $this->getVertices()->getVertexFirst()->getGraph();
}

/**
* create new graph clone with only vertices and edges actually in the walk
*
* do not add duplicate vertices and edges for loops and intersections, etc.
*
* @return Graph
* @uses Walk::getEdges()
* @uses Graph::createGraphCloneEdges()
*/
public function createGraph()
{
// create new graph clone with only edges of walk
$graph = $this->getGraph()->createGraphCloneEdges($this->getEdges());
$vertices = $this->getVertices()->getMap();
// get all vertices
foreach ($graph->getVertices()->getMap() as $vid => $vertex) {
if (!isset($vertices[$vid])) {
// remove those not present in the walk (isolated vertices, etc.)
$vertex->destroy();
}
}

return $graph;
}

/**
* return set of all Edges of walk (in sequence visited in walk, may contain duplicates)
*
Expand Down
18 changes: 0 additions & 18 deletions tests/Edge/EdgeBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,6 @@ public function testEdgeToInvalid()
$this->edge->getVertexToFrom($v3);
}

public function testClone()
{
$edge = $this->edge->createEdgeClone();

$this->assertEdgeEquals($this->edge, $edge);
}

public function testCloneDoubleInvertedIsOriginal()
{
$edgeInverted = $this->edge->createEdgeCloneInverted();

$this->assertInstanceOf(get_class($this->edge), $edgeInverted);

$edge = $edgeInverted->createEdgeCloneInverted();

$this->assertEdgeEquals($this->edge, $edge);
}

public function testLoop()
{
$edge = $this->createEdgeLoop();
Expand Down
32 changes: 32 additions & 0 deletions tests/GraphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,38 @@ public function testCreateVerticesContainsInvalid()
$graph->createVertices(array(1, 2, array(), 3));
}

public function testCloneInvertedUndirectedIsAlmostOriginal()
{
// 1 -- 2
$graph = new Graph();
$v1 = $graph->createVertex(1);
$v2 = $graph->createVertex(2);
$edge = $graph->createEdgeUndirected($v1, $v2);

$edgeInverted = $graph->createEdgeCloneInverted($edge);

$this->assertInstanceOf(get_class($edge), $edgeInverted);

$this->assertEquals($edge->getVertices()->getVector(), array_reverse($edgeInverted->getVertices()->getVector()));
}

public function testCloneDoubleInvertedDirectedEdgeIsOriginal()
{
// 1 -> 2
$graph = new Graph();
$v1 = $graph->createVertex(1);
$v2 = $graph->createVertex(2);
$edge = $graph->createEdgeDirected($v1, $v2);

$edgeInverted = $graph->createEdgeCloneInverted($edge);

$this->assertInstanceOf(get_class($edge), $edgeInverted);

$edgeInvertedAgain = $graph->createEdgeCloneInverted($edgeInverted);

$this->assertEdgeEquals($edge, $edgeInvertedAgain);
}

public function testRemoveEdge()
{
// 1 -- 2
Expand Down
61 changes: 40 additions & 21 deletions tests/WalkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ public function testWalkPath()
$this->assertSame(array($v1, $e1, $v2, $e2, $v3), $walk->getAlternatingSequence());
$this->assertTrue($walk->isValid());

$graphClone = $walk->createGraph();
$this->assertGraphEquals($graph, $graphClone);

return $walk;
}

Expand Down Expand Up @@ -74,19 +71,12 @@ public function testWalkWithinGraph()
$this->assertSame(array($v1, $e1, $v2), $walk->getAlternatingSequence());
$this->assertTrue($walk->isValid());

$graphExpected = new Graph();
$graphExpected->createEdgeDirected($graphExpected->createVertex(1), $graphExpected->createVertex(2));

$this->assertGraphEquals($graphExpected, $walk->createGraph());

// construct same partial walk "1 -- 2"
$walkVertices = Walk::factoryFromVertices(array($v1, $v2));

$this->assertEquals(2, count($walkVertices->getVertices()));
$this->assertEquals(1, count($walkVertices->getEdges()));

$this->assertGraphEquals($graphExpected, $walkVertices->createGraph());

return $walk;
}

Expand Down Expand Up @@ -169,7 +159,7 @@ public function testWalkCycleInvalid()
Walk::factoryCycleFromEdges(array($e1), $v1);
}

public function testLoopCycle()
public function testFactoryCycleFromEdgesWithLoopCycle()
{
// 1 --\
// ^ |
Expand All @@ -179,39 +169,67 @@ public function testLoopCycle()
$e1 = $graph->createEdgeDirected($v1, $v1);

$cycle = Walk::factoryCycleFromEdges(array($e1), $v1);
$this->assertGraphEquals($graph, $cycle->createGraph());

$this->assertCount(2, $cycle->getVertices());
$this->assertCount(1, $cycle->getEdges());
$this->assertSame($v1, $cycle->getVertices()->getVertexFirst());
$this->assertSame($v1, $cycle->getVertices()->getVertexLast());
$this->assertTrue($cycle->isValid());
}

public function testFactoryCycleFromPredecessorMapWithLoopCycle()
{
// 1 --\
// ^ |
// \---/
$graph = new Graph();
$v1 = $graph->createVertex(1);
$graph->createEdgeDirected($v1, $v1);

$cycle = Walk::factoryCycleFromPredecessorMap(array(1 => $v1), $v1);
$this->assertGraphEquals($graph, $cycle->createGraph());

$this->assertCount(2, $cycle->getVertices());
$this->assertCount(1, $cycle->getEdges());
$this->assertSame($v1, $cycle->getVertices()->getVertexFirst());
$this->assertSame($v1, $cycle->getVertices()->getVertexLast());
$this->assertTrue($cycle->isValid());
}

public function testFactoryCycleFromVerticesWithLoopCycle()
{
// 1 --\
// ^ |
// \---/
$graph = new Graph();
$v1 = $graph->createVertex(1);
$graph->createEdgeDirected($v1, $v1);

$cycle = Walk::factoryCycleFromVertices(array($v1, $v1));
$this->assertGraphEquals($graph, $cycle->createGraph());

$this->assertCount(2, $cycle->getVertices());
$this->assertCount(1, $cycle->getEdges());
$this->assertSame($v1, $cycle->getVertices()->getVertexFirst());
$this->assertSame($v1, $cycle->getVertices()->getVertexLast());
$this->assertTrue($cycle->isValid());

return $v1;
}


/**
*
* @param Vertex $v1
* @depends testLoopCycle
* @expectedException InvalidArgumentException
*/
public function testFactoryCycleFromVerticesIncomplete(Vertex $v1)
public function testFactoryCycleFromVerticesThrowsWhenCycleIsIncomplete()
{
$graph = new Graph();
$v1 = $graph->createVertex(1);

// should actually be [v1, v1]
Walk::factoryCycleFromVertices(array($v1));
}

/**
* @expectedException InvalidArgumentException
*/
public function testInvalidPredecessors()
public function testFactoryCycleFromPredecessorMapThrowsForInvalidPredecessors()
{
$graph = new Graph();
$v1 = $graph->createVertex(1);
Expand All @@ -222,6 +240,7 @@ public function testInvalidPredecessors()
public function testFactoryFromVertices()
{
// 1 -- 2
// | |
// \----/
$graph = new Graph();
$v1 = $graph->createVertex(1);
Expand Down