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'); }
public function testSort() { $graph = new Structures_Graph(); $name1 = 'node1'; $node1 = new Structures_Graph_Node(); $node1->setData($name1); $graph->addNode($node1); $name11 = 'node11'; $node11 = new Structures_Graph_Node(); $node11->setData($name11); $graph->addNode($node11); $node1->connectTo($node11); $name12 = 'node12'; $node12 = new Structures_Graph_Node(); $node12->setData($name12); $graph->addNode($node12); $node1->connectTo($node12); $name121 = 'node121'; $node121 = new Structures_Graph_Node(); $node121->setData($name121); $graph->addNode($node121); $node12->connectTo($node121); $name2 = 'node2'; $node2 = new Structures_Graph_Node(); $node2->setData($name2); $graph->addNode($node2); $name21 = 'node21'; $node21 = new Structures_Graph_Node(); $node21->setData($name21); $graph->addNode($node21); $node2->connectTo($node21); $nodes = Structures_Graph_Manipulator_TopologicalSorter::sort($graph); $this->assertCount(2, $nodes[0]); $this->assertEquals('node1', $nodes[0][0]->getData()); $this->assertEquals('node2', $nodes[0][1]->getData()); $this->assertCount(3, $nodes[1]); $this->assertEquals('node11', $nodes[1][0]->getData()); $this->assertEquals('node12', $nodes[1][1]->getData()); $this->assertEquals('node21', $nodes[1][2]->getData()); $this->assertCount(1, $nodes[2]); $this->assertEquals('node121', $nodes[2][0]->getData()); }
/** * Sort a list of arrays of array(downloaded packagefilename) by dependency. * * This uses the topological sort method from graph theory, and the * Structures_Graph package to properly sort dependencies for installation. * @param array an array of downloaded PEAR_Downloader_Packages * @return array array of array(packagefilename, package.xml contents) */ function sortPackagesForInstall(&$packages) { require_once 'Structures/Graph.php'; require_once 'Structures/Graph/Node.php'; require_once 'Structures/Graph/Manipulator/TopologicalSorter.php'; $depgraph = new Structures_Graph(true); $nodes = array(); $reg =& $this->config->getRegistry(); foreach ($packages as $i => $package) { $pname = $reg->parsedPackageNameToString(array('channel' => $package->getChannel(), 'package' => strtolower($package->getPackage()))); $nodes[$pname] = new Structures_Graph_Node(); $nodes[$pname]->setData($packages[$i]); $depgraph->addNode($nodes[$pname]); } $deplinks = array(); foreach ($nodes as $package => $node) { $pf =& $node->getData(); $pdeps = $pf->getDeps(true); if (!$pdeps) { continue; } if ($pf->getPackagexmlVersion() == '1.0') { foreach ($pdeps as $dep) { if ($dep['type'] != 'pkg' || isset($dep['optional']) && $dep['optional'] == 'yes') { continue; } $dname = $reg->parsedPackageNameToString(array('channel' => 'pear.php.net', 'package' => strtolower($dep['name']))); if (isset($nodes[$dname])) { if (!isset($deplinks[$dname])) { $deplinks[$dname] = array(); } $deplinks[$dname][$package] = 1; // dependency is in installed packages continue; } $dname = $reg->parsedPackageNameToString(array('channel' => 'pecl.php.net', 'package' => strtolower($dep['name']))); if (isset($nodes[$dname])) { if (!isset($deplinks[$dname])) { $deplinks[$dname] = array(); } $deplinks[$dname][$package] = 1; // dependency is in installed packages continue; } } } else { // the only ordering we care about is: // 1) subpackages must be installed before packages that depend on them // 2) required deps must be installed before packages that depend on them if (isset($pdeps['required']['subpackage'])) { $t = $pdeps['required']['subpackage']; if (!isset($t[0])) { $t = array($t); } $this->_setupGraph($t, $reg, $deplinks, $nodes, $package); } if (isset($pdeps['group'])) { if (!isset($pdeps['group'][0])) { $pdeps['group'] = array($pdeps['group']); } foreach ($pdeps['group'] as $group) { if (isset($group['subpackage'])) { $t = $group['subpackage']; if (!isset($t[0])) { $t = array($t); } $this->_setupGraph($t, $reg, $deplinks, $nodes, $package); } } } if (isset($pdeps['optional']['subpackage'])) { $t = $pdeps['optional']['subpackage']; if (!isset($t[0])) { $t = array($t); } $this->_setupGraph($t, $reg, $deplinks, $nodes, $package); } if (isset($pdeps['required']['package'])) { $t = $pdeps['required']['package']; if (!isset($t[0])) { $t = array($t); } $this->_setupGraph($t, $reg, $deplinks, $nodes, $package); } if (isset($pdeps['group'])) { if (!isset($pdeps['group'][0])) { $pdeps['group'] = array($pdeps['group']); } foreach ($pdeps['group'] as $group) { if (isset($group['package'])) { $t = $group['package']; if (!isset($t[0])) { $t = array($t); } $this->_setupGraph($t, $reg, $deplinks, $nodes, $package); } } } } } $this->_detectDepCycle($deplinks); foreach ($deplinks as $dependent => $parents) { foreach ($parents as $parent => $unused) { $nodes[$dependent]->connectTo($nodes[$parent]); } } $installOrder = Structures_Graph_Manipulator_TopologicalSorter::sort($depgraph); $ret = array(); for ($i = 0, $count = count($installOrder); $i < $count; $i++) { foreach ($installOrder[$i] as $index => $sortedpackage) { $data =& $installOrder[$i][$index]->getData(); $ret[] =& $nodes[$reg->parsedPackageNameToString(array('channel' => $data->getChannel(), 'package' => strtolower($data->getPackage())))]->getData(); } } $packages = $ret; return; }
<?php require_once 'Structures/Graph.php'; require_once 'Structures/Graph/Node.php'; $nonDirectedGraph = new Structures_Graph(false); $nodes_names = array('a', 'b', 'c', 'd', 'e'); $nodes = array(); foreach ($nodes_names as $node) { /* Create a new node / vertex */ $nodes[$node] = new Structures_Graph_Node(); /* Add the node to the Graph structure */ $nonDirectedGraph->addNode($nodes[$node]); } /** * Specify connections between different nodes. * For example in the following array, 'a-b' * specifies that node 'a' is connected to node 'b'. * Also refer to the figure above. */ $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();
<?php require_once 'Structures/Graph.php'; require_once 'Structures/Graph/Node.php'; $nonDirectedGraph = new Structures_Graph(true); $nodes_names = array('a', 'b', 'c', 'd', 'e'); $nodes = array(); foreach ($nodes_names as $node) { /* Create a new node / vertex */ $nodes[$node] = new Structures_Graph_Node(); /* Add the node to the Graph structure */ $nonDirectedGraph->addNode($nodes[$node]); } /** * Specify connections between different nodes. * For example in the following array, 'a-b' * specifies that node 'a' is connected to node 'b'. * Also refer to the figure above. */ $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]]); } //sets the value of each node to the corresponding value, for example $nodes['a']->setData("a") so that we could know the neighbor foreach ($nodes_names as $n) { $nodes[$n]->setData($n); } /* Get a list of all the nodes in our graph */ $array_nodes = $nonDirectedGraph->getNodes(); /* This is where will save the adj. matrix */
/** * Retorna un grafo representando un conjunto de tablas y sus relaciones * @return Structures_Graph */ static function grafo_relaciones($tablas, $relaciones) { $grafo = new Structures_Graph(true); // Se construyen los nodos $obj = array(); $nodo = null; foreach ($tablas as $tabla) { unset($nodo); $nodo = new Structures_Graph_Node(); $proveedor = isset($tabla['objeto_proveedor']) ? $tabla['objeto_proveedor'] : $tabla['objeto']; $obj[$proveedor] = $nodo; $nodo->setData($tabla); $grafo->addNode($obj[$proveedor]); } //Se agregan los arcos foreach ($relaciones as $asoc) { $padre = $asoc['padre_objeto']; $hijo = $asoc['hijo_objeto']; $obj[$padre]->connectTo($obj[$hijo]); } return $grafo; }
function get_grafo() { $grafo = new Structures_Graph(true); $perfiles = toba_info_permisos::get_perfiles_funcionales($this->s__filtro['proyecto']); //Nodos $miembros = array(); foreach ($perfiles as $perfil) { $nodo = new Structures_Graph_Node(); $nodo->setData($perfil); $nodos[$perfil['usuario_grupo_acc']] =& $nodo; $grafo->addNode($nodo); unset($nodo); //Anulo el nodo, de otra manera sobre escribe todos con los valores del ultimo setData (weird) } //Relaciones foreach ($perfiles as $perfil) { //Necesita pasarle la conexion porque aun no termino la transacción $miembros = toba_info_permisos::get_perfiles_funcionales_miembros($perfil['proyecto'], $perfil['usuario_grupo_acc'], toba::db()); foreach ($miembros as $miembro) { $nodos[$perfil['usuario_grupo_acc']]->connectTo($nodos[$miembro['usuario_grupo_acc_pertenece']]); } } return $grafo; }
$start = microtime(true); for ($target = 1; $target < 10; $target++) { //Select query running and displaying using while loop in bootstrapped table $sql = "SELECT iduser, idtrusted FROM trust WHERE iduser = {$target}"; $result = $conn->query($sql); $verti = array(); $iduser = array(); $idtrusted = array(); while ($row = $result->fetch_assoc()) { $verti[] = $row["iduser"] . '-' . $row["idtrusted"]; $iduser[] = $row["iduser"]; $idtrusted[] = $row["idtrusted"]; } $merged = array_merge($iduser, $idtrusted); $nodes_names = array_unique($merged); $nonDirectedGraphTest = new Structures_Graph(true); foreach ($nodes_names as $node) { /* Create a new node / vertex */ $nodes[$node] = new Structures_Graph_Node(); /* Add the node to the Graph structure */ $nonDirectedGraphTest->addNode($nodes[$node]); } foreach ($verti as $vertex) { $data = preg_split("/-/", $vertex); $nodes[$data[0]]->connectTo($nodes[$data[1]]); } //sets the value of each node to the corresponding value, for example $nodes['a']->setData("a") so that we could know the neighbor foreach ($nodes_names as $n) { $nodes[$n]->setData($n); } $trustedBy = $nodes[$target]->outDegree();