Pro Yearly is on sale from $80 to $50! »

Discovering graph structures

34be88398f623c109b61d23e8215bd23?s=47 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.

34be88398f623c109b61d23e8215bd23?s=128

Mariusz Gil

September 27, 2015
Tweet

Transcript

  1. DISCOVERING GRAPH STRUCTURES Mariusz Gil

  2. ABOUT ME

  3. DEVELOPER / TECH-LEAD / CONSULTANT

  4. None
  5. INSPIRATION IS BETTER THAN SOLUTION

  6. DATA

  7. CRUCIAL VERY THING IN YOUR APP

  8. CRUCIAL MOST THING IN YOUR APP STORING PROCESSING

  9. 90+

  10. WHY?

  11. 90+ HYPOTHESIS

  12. dev developers struct set list tree graph … SQL(struct)

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

    WAY TO WORK WITH DATA
  14. 7MB QUERY

  15. STORING PROCESSING

  16. 90+ HYPOTHESIS EXTENDED

  17. dev developers struct set list tree graph … SQL(JSON(struct))

  18. None
  19. None
  20. None
  21. None
  22. GRAPHS

  23. POWERFUL SINCE 1736

  24. C A GRAPH = (VERTICES, EDGES) B

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

    B D C A DENSE GRAPH B D ADJACENCY LIST ADJACENCY MATRIX
  26. DIGRAPH = (VERTICES, DIRECTED EDGES) C A B

  27. type: KNOWS VERTICES AND EDGES PROPERTIES B type: NOTHING A

    type: PERSON name: John Snow
  28. GRAPHS IN REAL LIFE

  29. None
  30. Open In progress Resolved Reopened Closed

  31. task_id task_status 1 1 2 3 3 2 4 4

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

    function setFiniteState($state) { $this->state = $state; } public function getFiniteState() { return $this->state; } }
  33. 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) );
  34. 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
  35. HUNDREDS TRANSITIONS TENS OF MODELS ONE ACTION DRY HUNDREDS OF

    THOUSANDS OBJECTS IN DB
  36. /** * @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 // ... }
  37. E C G F B A D H I J

    K L M
  38. 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
  39. @tom @bob @john #php #js #web Tweet X search tweet

    retweet favourite contains search search search follow follow search contains search
  40. 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; }
  41. 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);
  42. SEARCH SHORTEST PATH SPANNING TREES TRAVELING SALESMAN PROBLEM MAXIMUM FLOW

    MINIMUM COST FLOW MAXIMUM MATCHING ALGORITHMS
  43. 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;
  44. 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
  45. None
  46. LARGE MEDIUM SMALL

  47. GRAPH ORIENTED DATABASE

  48. INSTALL

  49. 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
  50. None
  51. RUN

  52. None
  53. QUERY

  54. None
  55. LIVE DEMO

  56. 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") ); }
  57. None
  58. http://graphdatabases.com

  59. ALTERNATIVES

  60. None
  61. None
  62. None
  63. TOOLS WILL CHANGE IDEAS ARE IMMORTAL

  64. Dijkstra's algorithm is an algorithm for finding ______________ in graph

  65. @mariuszgil THANKS! https://joind.in/14886