This commit is contained in:
Xes
2025-08-14 22:41:49 +02:00
parent 2de81ccc46
commit 8ce45119b6
39774 changed files with 4309466 additions and 0 deletions

View File

@@ -0,0 +1,197 @@
<?php
use Fhaculty\Graph\Graph;
use Fhaculty\Graph\Set\Vertices;
use Graphp\Algorithms\Tree\BaseDirected;
abstract class BaseDirectedTest extends TestCase
{
/**
*
* @param Graph $graph
* @return BaseDirected
*/
abstract protected function createTreeAlg(Graph $graph);
/**
* @return Graph
*/
abstract protected function createGraphNonTree();
/**
* @return Graph
*/
abstract protected function createGraphTree();
/**
* @return Graph
*/
abstract protected function createGraphParallelEdge();
public function testNullGraph()
{
$graph = new Graph();
$tree = $this->createTreeAlg($graph);
$this->assertFalse($tree->isTree());
$this->assertTrue($tree->getVerticesLeaf()->isEmpty());
$this->assertTrue($tree->getVerticesInternal()->isEmpty());
return $tree;
}
/**
* @param BaseDirected $tree
* @depends testNullGraph
* @expectedException UnderflowException
*/
public function testEmptyGraphDoesNotHaveRootVertex(BaseDirected $tree)
{
$tree->getVertexRoot();
}
/**
* @param BaseDirected $tree
* @depends testNullGraph
* @expectedException UnderflowException
*/
public function testEmptyGraphDoesNotHaveDegree(BaseDirected $tree)
{
$tree->getDegree();
}
/**
* @param BaseDirected $tree
* @depends testNullGraph
* @expectedException UnderflowException
*/
public function testEmptyGraphDoesNotHaveHeight(BaseDirected $tree)
{
$tree->getHeight();
}
public function testGraphTree()
{
$graph = $this->createGraphTree();
$root = $graph->getVertices()->getVertexFirst();
$nonRoot = $graph->getVertices()->getMap();
unset($nonRoot[$root->getId()]);
$nonRoot = new Vertices($nonRoot);
$c1 = $nonRoot->getVertexFirst();
$tree = $this->createTreeAlg($graph);
$this->assertTrue($tree->isTree());
$this->assertSame($root, $tree->getVertexRoot());
$this->assertSame($graph->getVertices()->getVector(), $tree->getVerticesSubtree($root)->getVector());
$this->assertSame($nonRoot->getVector(), $tree->getVerticesChildren($root)->getVector());
$this->assertSame($nonRoot->getVector(), $tree->getVerticesDescendant($root)->getVector());
$this->assertSame($nonRoot->getVector(), $tree->getVerticesLeaf()->getVector());
$this->assertSame(array(), $tree->getVerticesInternal()->getVector());
$this->assertSame($root, $tree->getVertexParent($c1));
$this->assertSame(array(), $tree->getVerticesChildren($c1)->getVector());
$this->assertSame(array(), $tree->getVerticesDescendant($c1)->getVector());
$this->assertSame(array($c1), $tree->getVerticesSubtree($c1)->getVector());
$this->assertEquals(2, $tree->getDegree());
$this->assertEquals(0, $tree->getDepthVertex($root));
$this->assertEquals(1, $tree->getDepthVertex($c1));
$this->assertEquals(1, $tree->getHeight());
$this->assertEquals(1, $tree->getHeightVertex($root));
$this->assertEquals(0, $tree->getHeightvertex($c1));
return $tree;
}
/**
*
* @param BaseDirected $tree
* @depends testGraphTree
* @expectedException UnderflowException
*/
public function testGraphTreeRootDoesNotHaveParent(BaseDirected $tree)
{
$root = $tree->getVertexRoot();
$tree->getVertexParent($root);
}
public function testNonTree()
{
$graph = $this->createGraphNonTree();
$tree = $this->createTreeAlg($graph);
$this->assertFalse($tree->isTree());
}
/**
* @expectedException UnexpectedValueException
*/
public function testNonTreeVertexHasMoreThanOneParent()
{
$graph = $this->createGraphNonTree();
$tree = $this->createTreeAlg($graph);
$tree->getVertexParent($graph->getVertex('v3'));
}
public function testGraphWithParallelEdgeIsNotTree()
{
$graph = $this->createGraphParallelEdge();
$tree = $this->createTreeAlg($graph);
$this->assertFalse($tree->isTree());
}
public function testGraphWithLoopIsNotTree()
{
// v1 -> v1
$graph = new Graph();
$graph->createVertex('v1')->createEdgeTo($graph->getVertex('v1'));
$tree = $this->createTreeAlg($graph);
$this->assertFalse($tree->isTree());
}
/**
* @expectedException UnexpectedValueException
*/
public function testGraphWithLoopCanNotGetSubgraph()
{
// v1 -> v1
$graph = new Graph();
$graph->createVertex('v1')->createEdgeTo($graph->getVertex('v1'));
$tree = $this->createTreeAlg($graph);
$tree->getVerticesSubtree($graph->getVertex('v1'));
}
public function testGraphWithUndirectedEdgeIsNotTree()
{
// v1 -- v2
$graph = new Graph();
$graph->createVertex('v1')->createEdge($graph->createVertex('v2'));
$tree = $this->createTreeAlg($graph);
$this->assertFalse($tree->isTree());
}
public function testGraphWithMixedEdgesIsNotTree()
{
// v1 -> v2 -- v3 -> v4
$graph = new Graph();
$graph->createVertex('v1')->createEdgeTo($graph->createVertex('v2'));
$graph->getVertex('v2')->createEdge($graph->createVertex('v3'));
$graph->getVertex('v3')->createEdgeTo($graph->createVertex('v4'));
$tree = $this->createTreeAlg($graph);
$this->assertFalse($tree->isTree());
}
}

View File

@@ -0,0 +1,48 @@
<?php
use Fhaculty\Graph\Graph;
use Graphp\Algorithms\Tree\InTree;
class InTreeTest extends BaseDirectedTest
{
protected function createGraphTree()
{
// c1 -> root <- c2
$graph = new Graph();
$root = $graph->createVertex();
$c1 = $graph->createVertex();
$c1->createEdgeTo($root);
$c2 = $graph->createVertex();
$c2->createEdgeTo($root);
return $graph;
}
protected function createTreeAlg(Graph $graph)
{
return new InTree($graph);
}
protected function createGraphNonTree()
{
// v1 -> v2 <- v3 -> v4
$graph = new Graph();
$graph->createVertex('v1')->createEdgeTo($graph->createVertex('v2'));
$graph->createVertex('v3')->createEdgeTo($graph->getVertex('v2'));
$graph->getVertex('v3')->createEdgeTo($graph->createVertex('v4'));
return $graph;
}
protected function createGraphParallelEdge()
{
// v1 <- v2, v1 <- v2
$graph = new Graph();
$graph->createVertex('v2')->createEdgeTo($graph->createVertex('v1'));
$graph->getVertex('v2')->createEdgeTo($graph->getVertex('v1'));
return $graph;
}
}

View File

@@ -0,0 +1,48 @@
<?php
use Fhaculty\Graph\Graph;
use Graphp\Algorithms\Tree\OutTree;
class OutTreeTest extends BaseDirectedTest
{
protected function createGraphTree()
{
// c1 <- root -> c2
$graph = new Graph();
$root = $graph->createVertex();
$c1 = $graph->createVertex();
$root->createEdgeTo($c1);
$c2 = $graph->createVertex();
$root->createEdgeTo($c2);
return $graph;
}
protected function createTreeAlg(Graph $graph)
{
return new OutTree($graph);
}
protected function createGraphNonTree()
{
// v1 -> v3 <- v2 -> v4
$graph = new Graph();
$graph->createVertex('v1')->createEdgeTo($graph->createVertex('v3'));
$graph->createVertex('v2')->createEdgeTo($graph->getVertex('v3'));
$graph->getVertex('v2')->createEdgeTo($graph->createVertex('v4'));
return $graph;
}
protected function createGraphParallelEdge()
{
// v1 -> v2, v1 -> v2
$graph = new Graph();
$graph->createVertex('v1')->createEdgeTo($graph->createVertex('v2'));
$graph->getVertex('v1')->createEdgeTo($graph->getVertex('v2'));
return $graph;
}
}

View File

@@ -0,0 +1,113 @@
<?php
use Fhaculty\Graph\Graph;
use Graphp\Algorithms\Tree\Undirected;
class UndirectedTest extends TestCase
{
protected function createTree(Graph $graph)
{
return new Undirected($graph);
}
public function testNullGraph()
{
$graph = new Graph();
$tree = $this->createTree($graph);
$this->assertFalse($tree->isTree());
$this->assertTrue($tree->getVerticesInternal()->isEmpty());
$this->assertTrue($tree->getVerticesLeaf()->isEmpty());
}
public function testGraphTrivial()
{
$graph = new Graph();
$graph->createVertex('v1');
$tree = $this->createTree($graph);
$this->assertTrue($tree->isTree());
$this->assertSame(array(), $tree->getVerticesInternal()->getVector());
$this->assertSame(array(), $tree->getVerticesLeaf()->getVector());
}
public function testGraphSimplePair()
{
// v1 -- v2
$graph = new Graph();
$graph->createVertex('v1')->createEdge($graph->createVertex('v2'));
$tree = $this->createTree($graph);
$this->assertTrue($tree->isTree());
$this->assertSame(array(), $tree->getVerticesInternal()->getVector());
$this->assertSame($graph->getVertices()->getVector(), $tree->getVerticesLeaf()->getVector());
}
public function testGraphSimpleLine()
{
// v1 -- v2 -- v3
$graph = new Graph();
$graph->createVertex('v1')->createEdge($graph->createVertex('v2'));
$graph->getVertex('v2')->createEdge($graph->createVertex('v3'));
$tree = $this->createTree($graph);
$this->assertTrue($tree->isTree());
$this->assertSame(array($graph->getVertex('v2')), $tree->getVerticesInternal()->getVector());
$this->assertSame(array($graph->getVertex('v1'), $graph->getVertex('v3')), $tree->getVerticesLeaf()->getVector());
}
public function testGraphPairParallelIsNotTree()
{
// v1 -- v2 -- v1
$graph = new Graph();
$graph->createVertex('v1')->createEdge($graph->createVertex('v2'));
$graph->getVertex('v1')->createEdge($graph->getVertex('v2'));
$tree = $this->createTree($graph);
$this->assertFalse($tree->isTree());
}
public function testGraphLoopIsNotTree()
{
// v1 -- v1
$graph = new Graph();
$graph->createVertex('v1')->createEdge($graph->getVertex('v1'));
$tree = $this->createTree($graph);
$this->assertFalse($tree->isTree());
}
public function testGraphCycleIsNotTree()
{
// v1 -- v2 -- v3 -- v1
$graph = new Graph();
$graph->createVertex('v1')->createEdge($graph->createVertex('v2'));
$graph->getVertex('v2')->createEdge($graph->createVertex('v3'));
$graph->getVertex('v3')->createEdge($graph->getVertex('v1'));
$tree = $this->createTree($graph);
$this->assertFalse($tree->isTree());
}
public function testGraphDirectedIsNotTree()
{
// v1 -> v2
$graph = new Graph();
$graph->createVertex('v1')->createEdgeTo($graph->createVertex('v2'));
$tree = $this->createTree($graph);
$this->assertFalse($tree->isTree());
}
public function testGraphMixedIsNotTree()
{
// v1 -- v2 -> v3
$graph = new Graph();
$graph->createVertex('v1')->createEdge($graph->createVertex('v2'));
$graph->getVertex('v2')->createEdgeTo($graph->createVertex('v3'));
$tree = $this->createTree($graph);
$this->assertFalse($tree->isTree());
}
}