Mariusz Gil
September 27, 2015
500

# 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

## Transcript

12. ### WE ALL SPEAK SQL* * INCLUDING 3NF THIS IS OUR

WAY TO WORK WITH DATA

20. ### C A SPARSE GRAPH B D C A COMPLETE GRAPH

B D C A DENSE GRAPH B D ADJACENCY LIST ADJACENCY MATRIX

22. ### type: KNOWS VERTICES AND EDGES PROPERTIES B type: NOTHING A

type: PERSON name: John Snow

25. ### task_id task_status 1 1 2 3 3 2 4 4

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

function setFiniteState(\$state) { \$this->state = \$state; } public function getFiniteState() { return \$this->state; } }

28. ### 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
29. ### HUNDREDS TRANSITIONS TENS OF MODELS ONE ACTION DRY HUNDREDS OF

THOUSANDS OBJECTS IN DB
30. ### /** * @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 // ... }

K L M
32. ### 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

34. ### 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; }
35. ### 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);
36. ### SEARCH SHORTEST PATH SPANNING TREES TRAVELING SALESMAN PROBLEM MAXIMUM FLOW

MINIMUM COST FLOW MAXIMUM MATCHING ALGORITHMS
37. ### 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;
38. ### 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

42. ### 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

46. ### 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") ); }