A Little Graph Theory for Developers - GOTO...

62
A Li%le Graph Theory for the Busy Developer Jim Webber Chief Scien?st, Neo Technology @jimwebber

Transcript of A Little Graph Theory for Developers - GOTO...

Page 1: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

A  Li%le  Graph  Theory  for  the  Busy  Developer  

Jim  Webber  Chief  Scien?st,  Neo  Technology  

@jimwebber  

Page 2: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Roadmap  

•  Imprisoned  data  •  Graph  models  •  Graph  theory  – Local  proper?es,  global  behaviours  – Predic?ve  techniques  

•  Graph  matching  – Predic?ve,  real-­‐?me  analy?cs  for  fun  and    profit  

•  Fin  

Page 3: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really
Page 4: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really
Page 5: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

h%p://www.flickr.com/photos/crazyneighborlady/355232758/  

Page 6: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

h%p://gallery.nen.gov.uk/image82582-­‐.html  

Page 7: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

h%p://www.xtranormal.com/watch/6995033/mongo-­‐db-­‐is-­‐web-­‐scale  

Page 8: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Aggregate-­‐Oriented  Data  h%p://mar?nfowler.com/bliki/AggregateOrientedDatabase.html  

“There is a significant downside - the whole approach works really well when data access is aligned with the aggregates, but what if you want to look at the data in a different way? Order entry naturally stores orders as aggregates, but analyzing product sales cuts across the aggregate structure. The advantage of not using an aggregate structure in the database is that it allows you to slice and dice your data different ways for different audiences.

This is why aggregate-oriented stores talk so much about map-reduce.”

Page 9: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really
Page 10: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

complexity = f(size, connectedness, uniformity)

Page 11: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

h%p://www.bbc.co.uk/london/travel/downloads/tube_map.html  

Page 12: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Property  graphs  

•  Property  graph  model:  – Nodes  with  proper?es  – Named,  directed  rela?onships  with  proper?es  – Rela?onships  have  exactly  one  start  and  end  node  • Which  may  be  the  same  node  

Page 13: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Property  Graph  Model  

Page 14: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Property  Graph  Model  

TRAVELS_WITH  

TRAVELS_WITH  

LOVES  

Page 15: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Property  Graph  Model  

TRAVELS_WITH  

TRAVELS_WITH  

LOVES  

name: the Doctor age: 907 species: Time Lord

first name: Rose late name: Tyler

vehicle: tardis model: Type 40

Page 16: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Labeled  Property  Graph  Model  (Neo4j  2.0)  

TRAVELS_WITH  

TRAVELS_WITH  

LOVES  

name: the Doctor age: 907 species: Time Lord

first name: Rose late name: Tyler

vehicle: tardis model: Type 40

Label:human

Label:timelord

Page 17: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Property  graphs  are  very  whiteboard-­‐friendly  

Page 18: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

h%p://blogs.adobe.com/digitalmarke?ng/analy?cs/predic?ve-­‐analy?cs/predic?ve-­‐analy?cs-­‐and-­‐the-­‐digital-­‐marketer/  

Page 19: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

h%p://en.wikipedia.org/wiki/File:Leonhard_Euler_2.jpg  

Meet  Leonhard  Euler  •  Swiss  mathema?cian  •  Inventor  of  Graph  Theory  (1736)  

Page 20: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

h%p://en.wikipedia.org/wiki/Seven_Bridges_of_Königsberg  

Page 21: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really
Page 22: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Triadic  Closure  name: Kyle

name: Stan name: Kenny

Page 23: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Triadic  Closure  

name: Kyle

name: Stan name: Kenny

name: Kyle

name: Stan name: Kenny FRIEND

Page 24: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Structural  Balance  name: Cartman

name: Craig name: Tweek

Page 25: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Structural  Balance  name: Cartman

name: Craig name: Tweek

name: Cartman

name: Craig name: Tweek FRIEND

Page 26: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Structural  Balance  name: Cartman

name: Craig name: Tweek

name: Cartman

name: Craig name: Tweek ENEMY

Page 27: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Structural  Balance  name: Kyle

name: Stan name: Kenny

name: Kyle

name: Stan name: Kenny FRIEND

Page 28: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Structural  Balance  is  a  key  predic?ve  technique  

And  it’s  domain-­‐agnos?c  

Page 29: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Allies  and  Enemies  

UK  

Germany  France  

Russia   Italy  

Austria  

Page 30: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Allies  and  Enemies  

UK  

Germany  France  

Russia   Italy  

Austria  

Page 31: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Allies  and  Enemies  

UK  

Germany  France  

Russia   Italy  

Austria  

Page 32: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Allies  and  Enemies  

UK  

Germany  France  

Russia   Italy  

Austria  

Page 33: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Allies  and  Enemies  

UK  

Germany  France  

Russia   Italy  

Austria  

Page 34: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Allies  and  Enemies  

UK  

Germany  France  

Russia   Italy  

Austria  

Page 35: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Predic?ng  WWI    [Easley  and  Kleinberg]  

Page 36: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Strong  Triadic  Closure  

It  if  a  node  has  strong  rela3onships  to  two  neighbours,  then  these  neighbours  must  have  at  least  a  weak  rela3onship  between  them.  

[Wikipedia]  

Page 37: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Triadic  Closure  (weak  rela?onship)  

name: Kenny

name: Stan name: Cartman

Page 38: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Triadic  Closure  (weak  rela?onship)  

name: Kenny

name: Stan name: Cartman

name: Kenny

name: Stan name: Cartman FRIEND 50%

Page 39: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Weak  rela?onships  

•  Rela?onships  can  have  “strength”  as  well  as  intent  – Think:  weigh?ng  on  a  rela?onship  in  a  property  graph  

•  Weak  links  play  another  super-­‐important  structural  role  in  graph  theory  – They  bridge  neighbourhoods  

Page 40: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Local  Bridges  

FRIEND

name: Kenny

name: Stan name: Kyle FRIEND FRIE

ND

FRIEND

name: Sally

name: Bebe name: Wendy FRIEND FRIE

ND

FRIEND 50%

name: Cartman FRIEND

ENEMY

Page 41: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Local  Bridge  Property  

“If  a  node  A  in  a  network  sa3sfies  the  Strong  Triadic  Closure  Property  and  is  involved  in  at  least  two  strong  rela3onships,  then  any  local  bridge  it  is  involved  in  must  be  a  weak  rela3onship.”  

[Easley  and  Kleinberg]  

Page 42: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

University  Karate  Club  

Page 43: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Graph  Par??oning  

•  (NP)  Hard  problem  – Recursively  remove  the  spanning  links  between  dense  regions  

– Or  recursively  merge  nodes  into  ever  larger  “subgraph”  nodes  

– Choose  your  algorithm  carefully  –  some  are  be%er  than  others  for  a  given  domain  

•  Can  use  to  (almost  exactly)  predict  the  break  up  of  the  karate  club!  

Page 44: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

University  Karate  Clubs  (predicted  by  Graph  Theory)  

9

Page 45: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

University  Karate  Clubs  (what  actually  happened!)  

Page 46: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really
Page 47: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Cypher  

•  Declara?ve  graph  pa%ern  matching  language  – “SQL  for  graphs”  – Columnar  results  

•  Supports  graph  matching  commands  and  queries  – Find  me  stuff  like  this…  – Aggrega?on,  ordering  and  limit,  etc.  

Page 48: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really
Page 49: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Firstname: Mickey Surname: Smith DoB: 19781006

SKU: 5e175641 Product: Badgers Nadgers Ale

SKU: 2555f258 Product: Peewee Pilsner

Category: beer

SKU: 49d102bc Product: Baby Dry Nights

Category: nappies

Category: baby Category: alcoholic drinks

SKU: 49d102bc Product: XBox 360

Category: consumer electronics

Category: console

BOUGHT BOUGHT

MEMBER_OF

MEMBER_OF MEMBER_OF

MEMBER_OF MEMBER_OF

Page 50: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really
Page 51: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Firstname: * Surname: * DoB: 1996 > x > 1972

Category: beer Category: nappies

BOUGHT Category: game console

Page 52: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Firstname: * Surname: * DoB: 1996 > x > 1972

Category: beer Category: nappies

!BOUGHT Category: game console

Page 53: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

(beer) (nappies)

(console)

(daddy)

() ()

<-[b:BOUGHT]- ()

Page 54: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Fla%en  the  graph  

(daddy)-[:BOUGHT]->()-[:MEMBER_OF]->(nappies)

(daddy)-[:BOUGHT]->()-[:MEMBER_OF]->(beer)

(daddy)-[b:BOUGHT]->()-[:MEMBER_OF]->(console)

Page 55: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Wrap  in  a  Cypher  MATCH  clause  

MATCH (daddy)-[:BOUGHT]->()-[:MEMBER_OF]->(nappies), (daddy)-[:BOUGHT]->()-[:MEMBER_OF]->(beer), (daddy)-[b:BOUGHT]->()-[:MEMBER_OF]->(console)

Page 56: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Cypher  WHERE  clause  

MATCH (daddy)-[:BOUGHT]->()-[:MEMBER_OF]->(nappies),

(daddy)-[:BOUGHT]->()-[:MEMBER_OF]->(beer),

(daddy)-[b:BOUGHT]->()-[:MEMBER_OF]->(console) WHERE b is null

Page 57: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Full  Cypher  query  

START beer=node:categories(category=‘beer’), nappies=node:categories(category=‘nappies’), xbox=node:products(product=‘xbox 360’)

MATCH (daddy)-[:BOUGHT]->()-[:MEMBER_OF]->(beer), (daddy)-[:BOUGHT]->()-[:MEMBER_OF]->(nappies), (daddy)-[b?:BOUGHT]->(xbox)

WHERE b is null

RETURN distinct daddy

Page 58: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Results  

==> +---------------------------------------------+

==> | daddy |

==> +---------------------------------------------+ ==> | Node[15]{name:"Rory Williams",dob:19880121} |

==> +---------------------------------------------+

==> 1 row

==> 0 ms

==>

neo4j-sh (0)$

Page 59: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really
Page 60: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

What  are  graphs  good  for?  •  Recommenda?ons  •  Business  intelligence  •  Social  compu?ng  •  Geospa?al  •  MDM  •  Data  centre  management  •  Web  of  things  •  Genealogy  •  Time  series  data  •  Product  catalogue  •  Web  analy?cs  •  Scien?fic  compu?ng  (especially  bioinforma?cs)  •  Indexing  your  slow  RDBMS  •  And  much  more!  

Page 61: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Visit:  h%p://GraphDatabases.com  

Ian Robinson, Jim Webber & Emil Eifrem

Graph Databases

h

Compliments

of Neo Technology

Page 62: A Little Graph Theory for Developers - GOTO Conferencegotocon.com/dl/goto-chicago-2013/slides/JimWebber... · “There is a significant downside - the whole approach works really

Neo4j  tutorial  on  Thursday!  Neo4j:  h"p://neo4j.org  

Me:  @jimwebber