コード例 #1
0
ファイル: Eulerian.php プロジェクト: faozimipa/algorithms
 /**
  * check whether this graph has an eulerian cycle
  *
  * @return boolean
  * @uses ConnectedComponents::isSingle()
  * @uses Degree::getDegreeVertex()
  * @todo isolated vertices should be ignored
  * @todo definition is only valid for undirected graphs
  */
 public function hasCycle()
 {
     $components = new ConnectedComponents($this->graph);
     if ($components->isSingle()) {
         $alg = new Degree($this->graph);
         foreach ($this->graph->getVertices() as $vertex) {
             // uneven degree => fail
             if ($alg->getDegreeVertex($vertex) & 1) {
                 return false;
             }
         }
         return true;
     }
     return false;
 }
コード例 #2
0
 public function testComponents()
 {
     // 1 -- 2, 3 -> 4, 5
     $graph = new Graph();
     $v1 = $graph->createVertex(1);
     $v2 = $graph->createVertex(2);
     $v3 = $graph->createVertex(3);
     $v4 = $graph->createVertex(4);
     $v5 = $graph->createVertex(5);
     $e1 = $v1->createEdge($v2);
     $e2 = $v3->createEdgeTo($v4);
     $alg = new AlgorithmConnected($graph);
     $this->assertEquals(3, $alg->getNumberOfComponents());
     $this->assertFalse($alg->isSingle());
     $graphs = $alg->createGraphsComponents();
     $this->assertCount(3, $graphs);
     $ge = new Graph();
     $ge->createVertex(1)->createEdge($ge->createVertex(2));
     $this->assertGraphEquals($ge, $alg->createGraphComponentVertex($v2));
     $ge = new Graph();
     $ge->createVertex(5);
     $this->assertEquals($ge, $alg->createGraphComponentVertex($v5));
 }