/** * * @return Edges */ public function getEdges() { $returnEdges = array(); // Create minimum spanning tree $minimumSpanningTreeAlgorithm = new MstKruskal($this->graph); $minimumSpanningTree = $minimumSpanningTreeAlgorithm->createGraph(); $alg = new SearchDepthFirst($minimumSpanningTree->getVertices()->getVertexFirst()); // Depth first search in minmum spanning tree (for the eulerian path) $startVertex = NULL; $oldVertex = NULL; // connect vertices in order of the depth first search foreach ($alg->getVertices() as $vertex) { // get vertex from the original graph (not from the depth first search) $vertex = $this->graph->getVertex($vertex->getId()); // need to clone the edge from the original graph, therefore i need the original edge if ($startVertex === NULL) { $startVertex = $vertex; } else { // get edge(s) to clone, multiple edges are possible (returns an array if undirected edge) $returnEdges[] = $oldVertex->getEdgesTo($vertex)->getEdgeFirst(); } $oldVertex = $vertex; } // connect last vertex with start vertex // multiple edges are possible (returns an array if undirected edge) $returnEdges[] = $oldVertex->getEdgesTo($startVertex)->getEdgeFirst(); return new Edges($returnEdges); }
/** * Filters the graph so that only entities connected to the root are left * * Should be run after you've added some set of associations with from() * * @return void */ public function filterConnected() { $alg = new BreadthFirst($this->graph->getVertex($this->root)); $alg->setDirection(BreadthFirst::DIRECTION_REVERSE); $vertices = $alg->getVertices(); $this->graph = $this->graph->createGraphCloneVertices($vertices); }
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; }
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; }
/** * Create edges between versions graph * * @param Graph $graph * @param string $className */ private function createEdges(Graph $graph, $className) { $migrationsAnnotations = $this->reader->getClassMigrationMethodInfo($className); $parentVertex = $graph->hasVertex($className) ? $graph->getVertex($className) : $graph->createVertex($className); foreach ($migrationsAnnotations as $migrationsAnnotation) { if ($migrationsAnnotation->annotation->from) { $fromClass = $migrationsAnnotation->annotation->from; $fromVertex = $graph->hasVertex($fromClass) ? $graph->getVertex($fromClass) : $graph->createVertex($fromClass); if (!$parentVertex->hasEdgeTo($fromVertex)) { $edge = $fromVertex->createEdgeTo($parentVertex); $this->annotations[$this->getEdgeId($edge)] = $migrationsAnnotation; $this->createEdges($graph, $fromClass); } } if ($migrationsAnnotation->annotation->to) { $toClass = $migrationsAnnotation->annotation->to; $fromVertex = $graph->hasVertex($toClass) ? $graph->getVertex($toClass) : $graph->createVertex($toClass); if (!$parentVertex->hasEdgeTo($fromVertex)) { $edge = $parentVertex->createEdgeTo($fromVertex); $this->annotations[$this->getEdgeId($edge)] = $migrationsAnnotation; $this->createEdges($graph, $toClass); } } } }
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; }
public function setUp() { $graph = new Graph(); $graph->createVertex(1); $graph->createVertex(2); // 1 -> 2 $this->edge = $graph->getVertex(1)->createEdge($graph->getVertex(2)); }
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; }
/** * @expectedException UnexpectedValueException */ public function testFailCycle() { $graph = new Graph(); $graph->createVertex(1)->createEdgeTo($graph->createVertex(2)); $graph->getVertex(2)->createEdgeTo($graph->getVertex(1)); $alg = new TopologicalSort($graph); $alg->getVertices(); }
public function testGraphAntiparallelIsSymmetricr() { // 1 -> 2 -> 1 $graph = new Graph(); $graph->createVertex(1)->createEdgeTo($graph->createVertex(2)); $graph->getVertex(2)->createEdgeTo($graph->getVertex(1)); $alg = new AlgorithmSymmetric($graph); $this->assertTrue($alg->isSymmetric()); }
/** * test an edge with capacity remaining */ public function testEdgePartial() { $graph = new Graph(); $graph->createVertex(0)->createEdgeTo($graph->createVertex(1))->setFlow(1)->setCapacity(2)->setWeight(3); $alg = new ResidualGraph($graph); $residual = $alg->createGraph(); $expected = new Graph(); $expected->createVertex(0); $expected->createVertex(1); // remaining edge $expected->getVertex(0)->createEdgeTo($expected->getVertex(1))->setFlow(0)->setCapacity(1)->setWeight(3); // back edge $expected->getVertex(1)->createEdgeTo($expected->getVertex(0))->setFlow(0)->setCapacity(1)->setWeight(-3); $this->assertGraphEquals($expected, $residual); }
public function testGraphEdgeDirections() { // 1 -- 2 -> 3 <- 4 $graph = new Graph(); $graph->createVertex(1)->createEdge($graph->createVertex(2)); $graph->getVertex(2)->createEdgeTo($graph->createVertex(3)); $graph->createVertex(4)->createEdgeTo($graph->getVertex(3)); $alg = new AlgorithmConnected($graph); $this->assertEquals(1, $alg->getNumberOfComponents()); $this->assertTrue($alg->isSingle()); $graphs = $alg->createGraphsComponents(); $this->assertCount(1, $graphs); $this->assertGraphEquals($graph, reset($graphs)); $this->assertGraphEquals($graph, $alg->createGraphComponentVertex($graph->getVertex(1))); }
/** * Build out-tree graph from defined Processes and their relations. * * @return OutTree */ public function buildTree() { if (!$this->tree) { $root = $this->graph->createVertex(0); // Create edges directed from the root node foreach ($this->processes as $processClassName => $processObject) { $vertex = $this->graph->getVertex($processClassName); if (!$processObject->delayMinutes) { // process doesn't depend on anything => link it to the root node $root->createEdgeTo($vertex)->setWeight(0); } else { // link process to its dependency // Throw error if dependency is to not existing vertex if (!$this->graph->hasVertex($processObject->delayAfter)) { throw new \InvalidArgumentException(sprintf('Testcase "%s" has @delayAfter dependency on "%s", but this testcase was not defined.', $processClassName, $processObject->delayAfter)); } $this->graph->getVertex($processObject->delayAfter)->createEdgeTo($vertex)->setWeight($processObject->delayMinutes); } } } $this->tree = new OutTree($this->graph); if (!$this->tree->isTree()) { throw new \InvalidArgumentException(sprintf('Cannot build tree graph from tests dependencies. Probably some cyclic dependency is present.')); } return $this->tree; }
/** * * @param string $dir * @return \Fhaculty\Graph\Graph */ public function createGraph() { $graph = new Graph(); foreach ($this->dependencyGraph->getPackages() as $package) { $name = $package->getName(); $start = $graph->createVertex($name, true); $label = $name; if ($package->getVersion() !== null) { $label .= ': ' . $package->getVersion(); } $this->setLayout($start, array('label' => $label) + $this->layoutVertex); foreach ($package->getOutEdges() as $requires) { $targetName = $requires->getDestPackage()->getName(); $target = $graph->createVertex($targetName, true); $label = $requires->getVersionConstraint(); $edge = $start->createEdgeTo($target); $this->setLayout($edge, array('label' => $label) + $this->layoutEdge); if ($requires->isDevDependency()) { $this->setLayout($edge, $this->layoutEdgeDev); } } } $root = $graph->getVertex($this->dependencyGraph->getRootPackage()->getName()); $this->setLayout($root, $this->layoutVertexRoot); return $graph; }
/** * @expectedException UnexpectedValueException */ public function testVertexWithUndirectedEdgeHasInvalidFlow() { // 1 -- 2 $graph = new Graph(); $graph->createVertex(1)->createEdge($graph->createVertex(2))->setFlow(10); $alg = new AlgorithmFlow($graph); $alg->getFlowVertex($graph->getVertex(1)); }
public function testGraphDirectedLoop() { // 1 -> 1 $graph = new Graph(); $graph->createVertex(1)->createEdgeTo($v1 = $graph->getVertex(1)); $alg = new AlgorithmLoop($graph); $this->assertTrue($alg->hasLoop()); $this->assertTrue($alg->hasLoopVertex($v1)); }
/** * create subgraph for all classes connected to given class (i.e. return it's connected component) * * @param string $class * @return Graph * @throws Exception */ public function createGraphComponent($class) { try { $vertex = $this->graph->getVertex($class); } catch (Exception $e) { throw new Exception('Given class is unknown'); } $alg = new AlgorithmConnectedComponents($this->graph); return $alg->createGraphComponentVertex($vertex); }
public function testGraphMixed() { // 1 -- 2 -> 3 $graph = new Graph(); $graph->createVertex(1)->createEdge($graph->createVertex(2)); $graph->getVertex(2)->createEdgeTo($graph->createVertex(3)); $alg = new AlgorithmDirected($graph); $this->assertTrue($alg->hasDirected()); $this->assertTrue($alg->hasUndirected()); $this->assertTrue($alg->isMixed()); }
public function testAdditionalEdgesToNotAffectCompleteness() { // 1 -> 2 // 1 -- 2 // 2 -> 1 // 1 -> 1 $graph = new Graph(); $graph->createVertex(1)->createEdgeTo($graph->createVertex(2)); $graph->getVertex(1)->createEdge($graph->getVertex(2)); $graph->getVertex(2)->createEdgeTo($graph->getVertex(1)); $graph->getVertex(1)->createEdgeTo($graph->getVertex(1)); $alg = new AlgorithmComplete($graph); $this->assertTrue($alg->isComplete()); }
/** * read given line string into given array of parts (int, float, vertex) * * @param string $line * @param array $parts * @param Graph $graph * @return mixed[] * @throws InvalidArgumentException */ protected function readLine($line, $parts, Graph $graph = NULL) { $ret = array(); $explode = explode("\t", $line); $i = 0; foreach ($parts as $key => $part) { if (!isset($explode[$i])) { throw new InvalidArgumentException('Line does not split into enough parts'); } $value = $explode[$i++]; if ($part === 'int') { $value = $this->readInt($value); } elseif ($part === 'float') { $value = $this->readFloat($value); } elseif ($part === 'vertex') { $value = $graph->getVertex($value); } else { throw new InvalidArgumentException('Invalid type "' . $part . '"'); } $ret[$key] = $value; } return $ret; }
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()); }
/** * @param Graph $graph * @expectedException InvalidArgumentException * @depends testRemoveEdge */ public function testRemoveEdgeInvalid(Graph $graph) { $edge = $graph->getVertex(1)->createEdge($graph->getVertex(2)); $edge->destroy(); $edge->destroy(); }
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)); }
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()); }
} $parents = str_replace($id, '', $parents); $parents = explode(',', $parents); $parents = array_filter($parents); if ($sequence->hasGraph()) { $graph = $sequence->getUnSerializeGraph(); } else { $graph = new Graph(); } switch ($type) { case 'session': $type = SequenceResource::SESSION_TYPE; $sessionInfo = api_get_session_info($id); $name = $sessionInfo['name']; if ($graph->hasVertex($id)) { $main = $graph->getVertex($id); } else { $main = $graph->createVertex($id); } foreach ($parents as $parentId) { if ($graph->hasVertex($parentId)) { $parent = $graph->getVertex($parentId); if (!$parent->hasEdgeTo($main)) { $parent->createEdgeTo($main); } } else { $parent = $graph->createVertex($parentId); $parent->createEdgeTo($main); } } foreach ($parents as $parentId) {