public function testDiscoveryEdges() { $graph = new Graph([1, 2, 3], [[1, 2], [1, 3], [2, 3]], true); $this->assertCount(2, $graph->getVertex(1)->getDiscoveryEdges()); $this->assertCount(1, $graph->getVertex(2)->getDiscoveryEdges()); $this->assertCount(0, $graph->getVertex(3)->getDiscoveryEdges()); $undirected = new Graph([1, 2, 3], [[1, 2], [1, 3], [2, 3]]); $this->assertCount(2, $undirected->getVertex(1)->getDiscoveryEdges()); $this->assertCount(2, $undirected->getVertex(2)->getDiscoveryEdges()); $this->assertCount(2, $undirected->getVertex(3)->getDiscoveryEdges()); }
public function testSourceSinkVerticesAreCorrect() { $graph = new Graph([0, 1, 2, 3, 4], [[0, 1], [1, 2], [2, 3], [3, 4], [1, 3]], true); $sinkArray = $graph->getSinkVertices(); $sourceArray = $graph->getSourceVertices(); $this->assertCount(1, $sourceArray); $this->assertCount(1, $sinkArray); $this->assertTrue(in_array($graph->getVertex(0), $sourceArray)); $this->assertFalse(in_array($graph->getVertex(1), $sourceArray)); $this->assertFalse(in_array($graph->getVertex(1), $sinkArray)); $this->assertFalse(in_array($graph->getVertex(3), $sinkArray)); $this->assertTrue(in_array($graph->getVertex(4), $sinkArray)); }
public function testDFS() { $edges = [[1, 2], [1, 3], [2, 5], [3, 3], [3, 4], [4, 2]]; $undirected = new Graph([1, 2, 3, 4, 5], $edges); $directed = new Graph([1, 2, 3, 4, 5], $edges, true); $this->assertEquals([$directed->getVertex(1), $directed->getVertex(3), $directed->getVertex(4), $directed->getVertex(2), $directed->getVertex(5)], GraphAlgorithms::dfs($directed, 1)); $this->assertEquals([$undirected->getVertex(1), $undirected->getVertex(3), $undirected->getVertex(4), $undirected->getVertex(2), $undirected->getVertex(5)], GraphAlgorithms::dfs($undirected, 1)); $this->assertEquals([$undirected->getVertex(5), $undirected->getVertex(2), $undirected->getVertex(4), $undirected->getVertex(3), $undirected->getVertex(1)], GraphAlgorithms::dfs($undirected, 5)); $this->assertEquals([$directed->getVertex(5)], GraphAlgorithms::dfs($directed, 5)); $graph = new Graph([1, 2], [[1, 2]], true); $visited = []; GraphAlgorithms::dfs($graph, 2, function ($v) use(&$visited) { $visited[] = $v->label; return true; }); $this->assertEquals([2], $visited); }
public function addOutEdgeToId($vertexId, $weight = 1) { $this->addOutEdgeTo($this->graph->getVertex($vertexId), $weight); }