Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Discovering graph structures

Mariusz Gil
September 27, 2015

Discovering graph structures

Graph is a mathematical construct used to model the relationships between key/value pairs, comprises a set of vertices (nodes) and an arbitrary number of edges (lines) which connect them. There are many ways to implement graphs with PHP, from typical SQL solutions to dedidacted, powerful tools like Apache Giraph or Titan. In this talk I will introduce Neo4j, highly scalable and robust native graph database, used in mission-critical apps by thousands of leading startups, enterprises and governments around the world. We will also talk about managing nodes and traversing methods with PHP.

Mariusz Gil

September 27, 2015
Tweet

More Decks by Mariusz Gil

Other Decks in Programming

Transcript

  1. 90+

  2. C A SPARSE GRAPH B D C A COMPLETE GRAPH

    B D C A DENSE GRAPH B D ADJACENCY LIST ADJACENCY MATRIX
  3. task_id task_status 1 1 2 3 3 2 4 4

    Open In progress Resolved Reopened Closed
  4. use Finite\StatefulInterface; class Document implements StatefulInterface { private $state; public

    function setFiniteState($state) { $this->state = $state; } public function getFiniteState() { return $this->state; } }
  5. use Finite\StateMachine\StateMachine; use Finite\State\State; use Finite\State\StateInterface; // $document = retrieve

    your stateful object $sm = new StateMachine(); // Define states $sm->addState( new State('s1', StateInterface::TYPE_INITIAL) ); $sm->addState('s2'); $sm->addState('s3'); $sm->addState( new State('s4', StateInterface::TYPE_FINAL) );
  6. s4 s3 s2 // Define transitions $sm->addTransition('t12', 's1', 's2'); $sm->addTransition('t23',

    's2', 's3'); $sm->addTransition('t34', 's3', 's4'); $sm->addTransition('t42', 's4', 's2'); // Initialize $sm->setObject($document); $sm->initialize(); // Retrieve current state $sm->getCurrentState(); // Can we process a transition? $sm->can('t34'); s1
  7. /** * @Route("/change-state") */ public function changeStateAction() { // Get

    model // ... // Check user permissions // ... // Check transition and notify observers if ($model->isTransitionAllowed($targetState) { $model->setState($targetState); // Save change // ... } // Finish // ... }
  8. E C G F B A D H I J

    1:30 1:30 1:00 1:45 0:45 0:45 3:00 1:15 1:00 0:40 0:20 0:30 0:30 K L M
  9. @tom @bob @john #php #js #web Tweet X search tweet

    retweet favourite contains search search search follow follow search contains search
  10. require_once 'vendor/autoload.php'; use \Fhaculty\Graph\Graph as Graph; $graph = new Graph();

    // Create some cities $rome = $graph->createVertex('Rome'); $madrid = $graph->createVertex('Madrid'); $cologne = $graph->createVertex('Cologne'); //Bbuild some roads $cologne->createEdgeTo($madrid); $madrid->createEdgeTo($rome); // create loop $rome->createEdgeTo($rome); // Process foreach ($rome->getVerticesEdgeFrom() as $vertex) { echo $vertex->getId().' leads to rome’ . PHP_EOL; }
  11. require_once 'vendor/autoload.php'; $graph = new Fhaculty\Graph\Graph(); $blue = $graph->createVertex('blue'); $blue->setAttribute('graphviz.color',

    'blue'); $red = $graph->createVertex('red'); $red->setAttribute('graphviz.color', 'red'); $edge = $blue->createEdgeTo($red); $edge->setAttribute('graphviz.color', 'grey'); $graphviz = new Graphp\GraphViz\GraphViz(); $graphviz->display($graph);
  12. TYPICAL SQL-WAY C A B mysql> SELECT * FROM `nodes`;

    +----+------+ | id | name | +----+------+ | 1 | A | | 2 | B | | 3 | C | +----+------+ 3 rows in set (0,00 sec) mysql> SELECT * FROM `edges`; +--------+--------+ | s_node | e_node | +--------+--------+ | 1 | 2 | | 2 | 3 | | 3 | 1 | +--------+--------+ 3 rows in set (0,00 sec) CREATE TABLE `nodes` ( `id` int(11) NOT NULL auto_increment, `name` varchar(64) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB; CREATE TABLE `edges` ( `s_node` int(11) NOT NULL, `e_node` int(11) NOT NULL, PRIMARY KEY (`s_node`,`e_node`), ) ENGINE=InnoDB;
  13. TYPICAL SQL-WAY C A B mysql> SELECT * FROM `nodes`;

    +----+------+ | id | name | +----+------+ | 1 | A | | 2 | B | | 3 | C | +----+------+ 3 rows in set (0,00 sec) mysql> SELECT * FROM `edges`; +--------+--------+-------+ | s_node | e_node | float | +--------+--------+-------+ | 1 | 2 | 6.50 | | 2 | 3 | 4.75 | | 3 | 1 | 10.00 | +--------+--------+-------+ 3 rows in set (0,00 sec) CREATE TABLE `nodes` ( `id` int(11) NOT NULL auto_increment, `name` varchar(64) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB; CREATE TABLE `edges` ( `s_node` int(11) NOT NULL, `e_node` int(11) NOT NULL, `weight` float NOT NULL, PRIMARY KEY (`s_node`,`e_node`), ) ENGINE=InnoDB; 10.00 6.50 4.75
  14. SINGLE NODE SETUP wget -O - http://debian.neo4j.org/neotechnology.gpg.key | apt-key add

    - echo 'deb http://debian.neo4j.org/repo stable/' > /etc/apt/sources.list.d/neo4j.list apt-get update apt-get install neo4j service neo4j-service status LINUX EDITION
  15. RUN

  16. require_once 'vendor/autoload.php'; use Neoxygen\NeoClient\ClientBuilder; $client = ClientBuilder::create() ->addConnection('default') ->setAutoFormatResponse(true) ->build();

    $query = 'MATCH (n)-[r]->(m) RETURN *'; $nodes = $client->sendCypherQuery($query) ->getResult() ->getNodes(); foreach ($nodes as $node) { echo sprintf( "%s %s:" . PHP_EOL, $node->getLabel(), $node->getProperty("name") ); }