$30 off During Our Annual Pro Sale. View Details »

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. DISCOVERING GRAPH STRUCTURES
    Mariusz Gil

    View Slide

  2. ABOUT ME

    View Slide

  3. DEVELOPER / TECH-LEAD / CONSULTANT

    View Slide

  4. View Slide

  5. INSPIRATION
    IS BETTER THAN SOLUTION

    View Slide

  6. DATA

    View Slide

  7. CRUCIAL
    VERY THING IN YOUR APP

    View Slide

  8. CRUCIAL
    MOST THING IN YOUR APP
    STORING PROCESSING

    View Slide

  9. 90+

    View Slide

  10. WHY?

    View Slide

  11. 90+ HYPOTHESIS

    View Slide

  12. dev
    developers
    struct
    set
    list
    tree
    graph

    SQL(struct)

    View Slide

  13. WE ALL SPEAK SQL*
    * INCLUDING 3NF
    THIS IS OUR WAY TO WORK WITH DATA

    View Slide

  14. 7MB QUERY

    View Slide

  15. STORING PROCESSING

    View Slide

  16. 90+ HYPOTHESIS EXTENDED

    View Slide

  17. dev
    developers
    struct
    set
    list
    tree
    graph

    SQL(JSON(struct))

    View Slide

  18. View Slide

  19. View Slide

  20. View Slide

  21. View Slide

  22. GRAPHS

    View Slide

  23. POWERFUL SINCE 1736

    View Slide

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

    View Slide

  25. C
    A
    SPARSE
    GRAPH
    B
    D
    C
    A
    COMPLETE
    GRAPH
    B
    D
    C
    A
    DENSE
    GRAPH
    B
    D
    ADJACENCY LIST
    ADJACENCY MATRIX

    View Slide

  26. DIGRAPH = (VERTICES, DIRECTED EDGES)
    C
    A
    B

    View Slide

  27. type: KNOWS
    VERTICES AND EDGES PROPERTIES
    B
    type: NOTHING
    A
    type: PERSON
    name: John Snow

    View Slide

  28. GRAPHS IN REAL LIFE

    View Slide

  29. View Slide

  30. Open
    In progress
    Resolved
    Reopened Closed

    View Slide

  31. task_id task_status
    1 1
    2 3
    3 2
    4 4
    Open
    In progress
    Resolved
    Reopened Closed

    View Slide

  32. use Finite\StatefulInterface;
    class Document implements StatefulInterface
    {
    private $state;
    public function setFiniteState($state)
    {
    $this->state = $state;
    }
    public function getFiniteState()
    {
    return $this->state;
    }
    }

    View Slide

  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)
    );

    View Slide

  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

    View Slide

  35. HUNDREDS TRANSITIONS
    TENS OF MODELS
    ONE ACTION
    DRY
    HUNDREDS OF THOUSANDS OBJECTS IN DB

    View Slide

  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
    // ...
    }

    View Slide

  37. E
    C
    G
    F
    B
    A
    D
    H
    I
    J
    K
    L
    M

    View Slide

  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

    View Slide

  39. @tom
    @bob
    @john
    #php
    #js
    #web
    Tweet X
    search
    tweet
    retweet
    favourite
    contains
    search
    search
    search
    follow
    follow
    search
    contains
    search

    View Slide

  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;
    }

    View Slide

  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);

    View Slide

  42. SEARCH
    SHORTEST PATH
    SPANNING TREES
    TRAVELING SALESMAN PROBLEM
    MAXIMUM FLOW
    MINIMUM COST FLOW
    MAXIMUM MATCHING
    ALGORITHMS

    View Slide

  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;

    View Slide

  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

    View Slide

  45. View Slide

  46. LARGE
    MEDIUM
    SMALL

    View Slide

  47. GRAPH ORIENTED DATABASE

    View Slide

  48. INSTALL

    View Slide

  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

    View Slide

  50. View Slide

  51. RUN

    View Slide

  52. View Slide

  53. QUERY

    View Slide

  54. View Slide

  55. LIVE DEMO

    View Slide

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

    View Slide

  57. View Slide

  58. http://graphdatabases.com

    View Slide

  59. ALTERNATIVES

    View Slide

  60. View Slide

  61. View Slide

  62. View Slide

  63. TOOLS WILL CHANGE
    IDEAS ARE IMMORTAL

    View Slide

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

    View Slide

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

    View Slide