Mariusz Gil
September 27, 2015
480

# 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

1. DISCOVERING GRAPH STRUCTURES
Mariusz Gil

3. DEVELOPER / TECH-LEAD / CONSULTANT

4. INSPIRATION
IS BETTER THAN SOLUTION

5. DATA

6. CRUCIAL

7. CRUCIAL
STORING PROCESSING

8. 90+

9. WHY?

10. 90+ HYPOTHESIS

11. dev
developers
struct
set
list
tree
graph

SQL(struct)

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

13. 7MB QUERY

14. STORING PROCESSING

15. 90+ HYPOTHESIS EXTENDED

16. dev
developers
struct
set
list
tree
graph

SQL(JSON(struct))

17. GRAPHS

18. POWERFUL SINCE 1736

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

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

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

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

23. GRAPHS IN REAL LIFE

24. Open
In progress
Resolved
Reopened Closed

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

27. use Finite\StateMachine\StateMachine;
use Finite\State\State;
use Finite\State\StateInterface;
// \$document = retrieve your stateful object
\$sm = new StateMachine();
// Define states
new State('s1', StateInterface::TYPE_INITIAL)
);
new State('s4', StateInterface::TYPE_FINAL)
);

28. s4
s3
s2
// Define transitions
// 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
// ...
}

31. E
C
G
F
B
A
D
H
I
J
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

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

use \Fhaculty\Graph\Graph as Graph;
\$graph = new Graph();
// Create some cities
\$rome = \$graph->createVertex('Rome');
\$cologne = \$graph->createVertex('Cologne');
// create loop
\$rome->createEdgeTo(\$rome);
// Process
foreach (\$rome->getVerticesEdgeFrom() as \$vertex) {
echo \$vertex->getId().' leads to rome’ . PHP_EOL;
}

\$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

39. LARGE
MEDIUM
SMALL

40. GRAPH ORIENTED DATABASE

41. INSTALL

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

43. RUN

44. QUERY

45. LIVE DEMO

use Neoxygen\NeoClient\ClientBuilder;
\$client = ClientBuilder::create()
->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")
);
}

47. http://graphdatabases.com

48. ALTERNATIVES

49. TOOLS WILL CHANGE
IDEAS ARE IMMORTAL

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

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