Skip to content

Instantly share code, notes, and snippets.

@nawroth
Last active December 21, 2015 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nawroth/6319398 to your computer and use it in GitHub Desktop.
Save nawroth/6319398 to your computer and use it in GitHub Desktop.

Finding Paths

Our example graph consists of movies with title and year and actors with a name. Actors have ACTS_IN relationships to movies, which represents the role they played. This relationship also has a role attribute.

We queried and updated the data so far, now let’s find interesting constellations, a.k.a. paths.

CREATE (matrix1:Movie {title : 'The Matrix', year : '1999-03-31'})
CREATE (matrix2:Movie {title : 'The Matrix Reloaded', year : '2003-05-07'})
CREATE (matrix3:Movie {title : 'The Matrix Revolutions', year : '2003-10-27'})
CREATE (keanu:Actor {name:'Keanu Reeves'})
CREATE (laurence:Actor {name:'Laurence Fishburne'})
CREATE (carrieanne:Actor {name:'Carrie-Anne Moss'})
CREATE (keanu)-[:ACTS_IN {role : 'Neo'}]->(matrix1)
CREATE (keanu)-[:ACTS_IN {role : 'Neo'}]->(matrix2)
CREATE (keanu)-[:ACTS_IN {role : 'Neo'}]->(matrix3)
CREATE (laurence)-[:ACTS_IN {role : 'Morpheus'}]->(matrix1)
CREATE (laurence)-[:ACTS_IN {role : 'Morpheus'}]->(matrix2)
CREATE (laurence)-[:ACTS_IN {role : 'Morpheus'}]->(matrix3)
CREATE (carrieanne)-[:ACTS_IN {role : 'Trinity'}]->(matrix1)
CREATE (carrieanne)-[:ACTS_IN {role : 'Trinity'}]->(matrix2)
CREATE (carrieanne)-[:ACTS_IN {role : 'Trinity'}]->(matrix3)

All other movies that actors in ``The Matrix'' acted in ordered by occurrence:

MATCH (theMatrix:Movie), (theMatrix)<-[:ACTS_IN]-(actor)-[:ACTS_IN]->(movie)
WHERE theMatrix.title = "The Matrix"
RETURN movie.title, count(*)
ORDER BY count(*) DESC;

Let’s see who acted in each of these movies:

MATCH (theMatrix:Movie), (theMatrix)<-[:ACTS_IN]-(actor)-[:ACTS_IN]->(movie)
WHERE theMatrix.title = "The Matrix"
RETURN movie.title, collect(actor.name), count(*) as count
ORDER BY count desc;

What about co-acting, that is actors that acted together:

MATCH (theMatrix:Movie),
  (theMatrix)<-[:ACTS_IN]-(actor)-[:ACTS_IN]->(movie)<-[:ACTS_IN]-(colleague)
WHERE theMatrix.title = "The Matrix"
RETURN actor.name, collect(distinct colleague.name);

Who of those other actors acted most often with anyone from the matrix cast?

MATCH (theMatrix:Movie),
  (theMatrix)<-[:ACTS_IN]-(actor)-[:ACTS_IN]->(movie)<-[:ACTS_IN]-(colleague)
WHERE theMatrix.title = "The Matrix"
RETURN colleague.name, count(*)
ORDER BY count(*) DESC LIMIT 10;

Starting with paths, a path is a sequence of nodes and relationships from a start node to an end node.

We know that Trinity loves Neo, but how many paths exist between their actors? We’ll limit the path length and the query as it exhaustively searches the graph otherwise

MATCH (keanu:Actor), (carrieanne:Actor),
  p = (keanu)-[:ACTS_IN*0..5]-(carrieanne)
WHERE keanu.name = "Keanu Reeves" AND carrieanne.name = "Carrie-Anne Moss"
RETURN p, length(p)
LIMIT 10;

Bur that’s a lot of data, we just want to look at the names and titles of the nodes of the path.

MATCH (keanu:Actor), (carrieanne:Actor),
  p = (keanu)-[:ACTS_IN*0..5]-(carrieanne)
WHERE keanu.name = "Keanu Reeves" AND carrieanne.name = "Carrie-Anne Moss"
RETURN extract(n IN nodes(p) |
         coalesce(n.title,n.name)) AS `names and titles`,
       length(p)
ORDER BY length(p)
LIMIT 10 ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment