/**
  *
  */
 public function createGraph()
 {
     $graph = new Graph();
     $file = $this->getLines();
     $graph->createVertices($this->readInt($file[0]));
     unset($file[0]);
     // set the value of the vertices
     $zeile = 1;
     foreach ($graph->getVertices() as $vertex) {
         $vertex->setBalance($this->readFloat($file[$zeile]));
         unset($file[$zeile]);
         ++$zeile;
     }
     foreach ($file as $zeile) {
         $parts = $this->readLine($zeile, array('vertex', 'vertex', 'float', 'float'), $graph);
         if ($this->directedEdges) {
             $edge = $parts[0]->createEdgeTo($parts[1]);
         } else {
             $edge = $parts[0]->createEdge($parts[1]);
         }
         $edge->setWeight($parts[2]);
         $edge->setCapacity($parts[3]);
     }
     return $graph;
 }
Example #2
0
 public function createGraph()
 {
     $graph = new Graph();
     $file = $this->getLines();
     $vertexCount = $this->readInt($file[0]);
     $edgeCounter = 0;
     if (count($file) !== $vertexCount + 1) {
         throw new UnexpectedValueException('Expects ' . ($vertexCount + 1) . ' lines, but found ' . count($file));
     }
     $graph->createVertices($vertexCount);
     $parts = array_fill(0, $vertexCount, 'int');
     for ($i = 0; $i < $vertexCount; $i++) {
         // Add Vertices
         $this->writeDebugMessage("Adding vertex {$i}, ");
         $thisVertex = $graph->getVertex($i);
         $currentEdgeList = $this->readLine($file[$i + 1], $parts);
         // $currentEdgeList = explode("\t", $file[$i + 1]);
         for ($k = 0; $k < $vertexCount; $k++) {
             // Add edges
             if ($currentEdgeList[$k] != 0) {
                 $this->writeDebugMessage(" and edge #{$edgeCounter}: {$i} -> {$k} ");
                 if ($this->directedEdges) {
                     $thisVertex->createEdgeTo($graph->getVertex($k));
                 } else {
                     $thisVertex->createEdge($graph->getVertex($k));
                 }
                 $edgeCounter++;
             }
         }
         $this->writeDebugMessage("\n");
     }
     return $graph;
 }
Example #3
0
 public function createGraph()
 {
     $start = microtime(true);
     $graph = new Graph();
     $n = $this->numberOfVertices;
     $this->writeDebugMessage("start creating vertices\n");
     $graph->createVertices($n);
     $this->writeDebugMessage("start creating edges\n");
     for ($i = 0; $i < $n; ++$i) {
         $vertex = $graph->getVertex($i);
         if ($this->directedEdges) {
             for ($j = 0; $j < $n; ++$j) {
                 if ($j !== $i) {
                     $vertex->createEdgeTo($graph->getVertex($j));
                 }
             }
         } else {
             for ($j = $i + 1; $j < $n; ++$j) {
                 $vertex->createEdge($graph->getVertex($j));
             }
         }
     }
     $end = microtime(true);
     $this->writeDebugMessage($end - $start . " done ...\n");
     return $graph;
 }
Example #4
0
 public function createGraph()
 {
     $graph = new Graph();
     $file = $this->getLines();
     $countOfAllVertices = $this->readInt($file[0]);
     $countOfVerticesInA = $this->readInt($file[1]);
     if ($countOfVerticesInA > $countOfAllVertices || $countOfVerticesInA < 0) {
         throw new UnexpectedValueException('Invalid value for number of vertices in group 0');
     }
     $graph->createVertices($countOfAllVertices);
     for ($i = 0; $i < $countOfVerticesInA; ++$i) {
         $graph->getVertex($i)->setGroup(0);
     }
     for ($k = $countOfVerticesInA; $k < $countOfAllVertices; ++$k) {
         $graph->getVertex($k)->setGroup(1);
     }
     unset($file[0]);
     unset($file[1]);
     foreach ($file as $zeile) {
         $parts = $this->readLine($zeile, array('vertex', 'vertex'), $graph);
         if ($this->directedEdges) {
             $edge = $parts[0]->createEdgeTo($parts[1]);
         } else {
             $edge = $parts[0]->createEdge($parts[1]);
         }
     }
     $alg = new AlgorithmGroups($graph);
     if (!$alg->isBipartit()) {
         throw new UnexpectedValueException('Graph read from file does not form a valid bipartit graph');
     }
     return $graph;
 }
Example #5
0
 public function createGraph()
 {
     $graph = new Graph();
     $file = $this->getLines();
     $graph->createVertices($this->readInt($file[0]));
     unset($file[0]);
     foreach ($file as $zeile) {
         $parts = $this->readLine($zeile, array('vertex', 'vertex', 'float'), $graph);
         if ($this->directedEdges) {
             $edge = $parts[0]->createEdgeTo($parts[1]);
         } else {
             $edge = $parts[0]->createEdge($parts[1]);
         }
         $edge->setWeight($parts[2]);
     }
     return $graph;
 }
Example #6
0
 public function createGraph()
 {
     $graph = new Graph();
     $file = $this->getLines();
     $vertexCount = $this->readInt($file[0]);
     $this->writeDebugMessage('create ' . $vertexCount . ' vertices');
     $graph->createVertices($vertexCount);
     $this->writeDebugMessage('parse edges');
     unset($file[0]);
     foreach ($file as $zeile) {
         $ends = $this->readLine($zeile, array('from' => 'vertex', 'to' => 'vertex'), $graph);
         if ($this->directedEdges) {
             $ends['from']->createEdgeTo($ends['to']);
         } else {
             $ends['from']->createEdge($ends['to']);
         }
     }
     return $graph;
 }
Example #7
0
    public function testGraphUndirectedWithIsolatedVerticesFirst()
    {
        // a -- b -- c   d
        $graph = new Graph();
        $graph->createVertices(array('a', 'b', 'c', 'd'));
        $graph->getVertex('a')->createEdge($graph->getVertex('b'));
        $graph->getVertex('b')->createEdge($graph->getVertex('c'));
        $expected = <<<VIZ
graph G {
  "d"
  "a" -- "b"
  "b" -- "c"
}

VIZ;
        $this->assertEquals($expected, $this->graphViz->createScript($graph));
    }
Example #8
0
 /**
  * @expectedException InvalidArgumentException
  */
 public function testCreateVerticesContainsInvalid()
 {
     $graph = new Graph();
     $graph->createVertices(array(1, 2, array(), 3));
 }
 /**
  * @param Graph $graph
  * @param int $count
  * @return \Fhaculty\Graph\Vertex[]
  */
 public function generateVertices(Graph $graph, $count = 5)
 {
     for ($i = 1; $i <= $count; $i++) {
         $ids[] = "node_{$i}";
     }
     return $graph->createVertices($ids)->getMap();
 }