public function testIsAcyclicTrue() { $graph = new Structures_Graph(); $node1 = new Structures_Graph_Node(); $graph->addNode($node1); $node2 = new Structures_Graph_Node(); $graph->addNode($node2); $node1->connectTo($node2); $node3 = new Structures_Graph_Node(); $graph->addNode($node3); $node2->connectTo($node3); $this->assertTrue(Structures_Graph_Manipulator_AcyclicTest::isAcyclic($graph), 'Graph is acyclic'); }
/** * * sort returns the graph's nodes, sorted by topological order. * * The result is an array with * as many entries as topological levels. Each entry in this array is an array of nodes within * the given topological level. * * @return array The graph's nodes, sorted by topological order. * @access public */ function sort(&$graph) { // We only sort graphs if (!is_a($graph, 'Structures_Graph')) { return Pear::raiseError('Structures_Graph_Manipulator_TopologicalSorter::sort received an object that is not a Structures_Graph', STRUCTURES_GRAPH_ERROR_GENERIC); } if (!Structures_Graph_Manipulator_AcyclicTest::isAcyclic($graph)) { return Pear::raiseError('Structures_Graph_Manipulator_TopologicalSorter::sort received an graph that has cycles', STRUCTURES_GRAPH_ERROR_GENERIC); } Structures_Graph_Manipulator_TopologicalSorter::_sort($graph); $result = array(); // Fill out result array $nodes =& $graph->getNodes(); $nodeKeys = array_keys($nodes); foreach ($nodeKeys as $key) { if (!array_key_exists($nodes[$key]->getMetadata('topological-sort-level'), $result)) { $result[$nodes[$key]->getMetadata('topological-sort-level')] = array(); } $result[$nodes[$key]->getMetadata('topological-sort-level')][] =& $nodes[$key]; $nodes[$key]->unsetMetadata('topological-sort-level'); } return $result; }
*/ $vertices = array('a-b', 'b-c', 'b-d', 'd-c', 'c-e', 'e-d'); foreach ($vertices as $vertex) { $data = preg_split("/-/", $vertex); $nodes[$data[0]]->connectTo($nodes[$data[1]]); } $test1 = array('a', 'b'); $test2 = array('c', 'd'); $test3 = array_merge($test1, $test2); print_r($test3); echo $nodes['b']->inDegree(); // returns 3 echo $nodes['b']->outDegree(); // returns 3 require_once 'Structures/Graph/Manipulator/AcyclicTest.php'; $t = new Structures_Graph_Manipulator_AcyclicTest(); if ($t->isAcyclic($nonDirectedGraph)) { echo "cyclic"; } /* Get a list of all the nodes in our graph */ $array_nodes = $nonDirectedGraph->getNodes(); /* This is where will save the adj. matrix */ $adj_matrix = array(); /* Reset the matrix to all '0's */ foreach ($nodes_names as $row) { foreach ($nodes_names as $col) { $adj_matrix[$row][$col] = 0; } } /* Now build the adj. matrix */ foreach ($array_nodes as $nd) {
/** * * isAcyclic returns true if a graph contains no cycles, false otherwise. * * @return boolean true iff graph is acyclic * @access public */ function isAcyclic(&$graph) { // We only test graphs if (!is_a($graph, 'Structures_Graph')) { return Pear::raiseError('Structures_Graph_Manipulator_AcyclicTest::isAcyclic received an object that is not a Structures_Graph', STRUCTURES_GRAPH_ERROR_GENERIC); } if (!$graph->isDirected()) { return false; } // Only directed graphs may be acyclic return Structures_Graph_Manipulator_AcyclicTest::_isAcyclic($graph); }
function hay_ciclos($tablas, $relaciones) { $tester = new Structures_Graph_Manipulator_AcyclicTest(); $grafo = toba_datos_relacion::grafo_relaciones($tablas, $relaciones); return !$tester->isAcyclic($grafo); }
/** * * isAcyclic returns true if a graph contains no cycles, false otherwise. * * @return boolean true iff graph is acyclic * @access public */ function getCycle(&$graph) { // We only test graphs if (!is_a($graph, 'Structures_Graph')) { throw new Exception('Structures_Graph_Manipulator_AcyclicTest::isAcyclic received an object that is not a Structures_Graph'); } if (!$graph->isDirected()) { return false; } // Only directed graphs may be acyclic return Structures_Graph_Manipulator_AcyclicTest::_isAcyclic($graph, true); }
function validar_ciclos() { $tester = new Structures_Graph_Manipulator_AcyclicTest(); $grafo = $this->get_grafo(); $aciclico = $tester->isAcyclic($grafo); if (!$aciclico) { $ciclo = array(); foreach ($tester->getCycle($grafo) as $nodo) { $data = $nodo->getData(); $ciclo[] = $data['nombre']; } $perfiles = implode(', ', $ciclo); throw new toba_error_usuario("Existe un ciclo en la asignación de las membresías entre los perfiles: <b>{$perfiles}</b>.<br><br>Por favor quite alguna membresía."); } }