5.3. 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 (:Movie { title: "The Matrix" })<-[:ACTS_IN]-(actor)-[:ACTS_IN]->(movie)
RETURN movie.title, count(*)
ORDER BY count(*) DESC ;
movie.titlecount(*)
2 rows

"The Matrix Revolutions"

3

"The Matrix Reloaded"

3

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

MATCH (:Movie { title: "The Matrix" })<-[:ACTS_IN]-(actor)-[:ACTS_IN]->(movie)
RETURN movie.title, collect(actor.name), count(*) AS count
ORDER BY count DESC ;
movie.titlecollect(actor.name)count
2 rows

"The Matrix Revolutions"

["Carrie-Anne Moss","Laurence Fishburne","Keanu Reeves"]

3

"The Matrix Reloaded"

["Carrie-Anne Moss","Laurence Fishburne","Keanu Reeves"]

3

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

MATCH (:Movie { title: "The Matrix"
  })<-[:ACTS_IN]-(actor)-[:ACTS_IN]->(movie)<-[:ACTS_IN]-(colleague)
RETURN actor.name, collect(DISTINCT colleague.name);
actor.namecollect(distinct colleague.name)
3 rows

"Carrie-Anne Moss"

["Laurence Fishburne","Keanu Reeves"]

"Keanu Reeves"

["Carrie-Anne Moss","Laurence Fishburne"]

"Laurence Fishburne"

["Carrie-Anne Moss","Keanu Reeves"]

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

MATCH (:Movie { title: "The Matrix"
  })<-[:ACTS_IN]-(actor)-[:ACTS_IN]->(movie)<-[:ACTS_IN]-(colleague)
RETURN colleague.name, count(*)
ORDER BY count(*) DESC LIMIT 10;
colleague.namecount(*)
3 rows

"Carrie-Anne Moss"

4

"Keanu Reeves"

4

"Laurence Fishburne"

4

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 the two actors? We’ll limit the path length of the pattern as it exhaustively searches the graph otherwise. This is done by using *0..5 in the pattern relationship.

MATCH p =(:Actor { name: "Keanu Reeves" })-[:ACTS_IN*0..5]-(:Actor { name: "Carrie-Anne Moss" })
RETURN p, length(p)
LIMIT 10;
plength(p)
9 rows

[Node[3]{name:"Keanu Reeves"},:ACTS_IN[0]{role:"Neo"},Node[0]{title:"The Matrix",year:"1999-03-31"},:ACTS_IN[6]{role:"Trinity"},Node[5]{name:"Carrie-Anne Moss"}]

2

[Node[3]{name:"Keanu Reeves"},:ACTS_IN[1]{role:"Neo"},Node[1]{year:"2003-05-07",title:"The Matrix Reloaded"},:ACTS_IN[4]{role:"Morpheus"},Node[4]{name:"Laurence Fishburne"},:ACTS_IN[3]{role:"Morpheus"},Node[0]{title:"The Matrix",year:"1999-03-31"},:ACTS_IN[6]{role:"Trinity"},Node[5]{name:"Carrie-Anne Moss"}]

4

[Node[3]{name:"Keanu Reeves"},:ACTS_IN[2]{role:"Neo"},Node[2]{year:"2003-10-27",title:"The Matrix Revolutions"},:ACTS_IN[5]{role:"Morpheus"},Node[4]{name:"Laurence Fishburne"},:ACTS_IN[3]{role:"Morpheus"},Node[0]{title:"The Matrix",year:"1999-03-31"},:ACTS_IN[6]{role:"Trinity"},Node[5]{name:"Carrie-Anne Moss"}]

4

[Node[3]{name:"Keanu Reeves"},:ACTS_IN[1]{role:"Neo"},Node[1]{year:"2003-05-07",title:"The Matrix Reloaded"},:ACTS_IN[7]{role:"Trinity"},Node[5]{name:"Carrie-Anne Moss"}]

2

[Node[3]{name:"Keanu Reeves"},:ACTS_IN[0]{role:"Neo"},Node[0]{title:"The Matrix",year:"1999-03-31"},:ACTS_IN[3]{role:"Morpheus"},Node[4]{name:"Laurence Fishburne"},:ACTS_IN[4]{role:"Morpheus"},Node[1]{year:"2003-05-07",title:"The Matrix Reloaded"},:ACTS_IN[7]{role:"Trinity"},Node[5]{name:"Carrie-Anne Moss"}]

4

[Node[3]{name:"Keanu Reeves"},:ACTS_IN[2]{role:"Neo"},Node[2]{year:"2003-10-27",title:"The Matrix Revolutions"},:ACTS_IN[5]{role:"Morpheus"},Node[4]{name:"Laurence Fishburne"},:ACTS_IN[4]{role:"Morpheus"},Node[1]{year:"2003-05-07",title:"The Matrix Reloaded"},:ACTS_IN[7]{role:"Trinity"},Node[5]{name:"Carrie-Anne Moss"}]

4

[Node[3]{name:"Keanu Reeves"},:ACTS_IN[2]{role:"Neo"},Node[2]{year:"2003-10-27",title:"The Matrix Revolutions"},:ACTS_IN[8]{role:"Trinity"},Node[5]{name:"Carrie-Anne Moss"}]

2

[Node[3]{name:"Keanu Reeves"},:ACTS_IN[0]{role:"Neo"},Node[0]{title:"The Matrix",year:"1999-03-31"},:ACTS_IN[3]{role:"Morpheus"},Node[4]{name:"Laurence Fishburne"},:ACTS_IN[5]{role:"Morpheus"},Node[2]{year:"2003-10-27",title:"The Matrix Revolutions"},:ACTS_IN[8]{role:"Trinity"},Node[5]{name:"Carrie-Anne Moss"}]

4

[Node[3]{name:"Keanu Reeves"},:ACTS_IN[1]{role:"Neo"},Node[1]{year:"2003-05-07",title:"The Matrix Reloaded"},:ACTS_IN[4]{role:"Morpheus"},Node[4]{name:"Laurence Fishburne"},:ACTS_IN[5]{role:"Morpheus"},Node[2]{year:"2003-10-27",title:"The Matrix Revolutions"},:ACTS_IN[8]{role:"Trinity"},Node[5]{name:"Carrie-Anne Moss"}]

4

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

MATCH p =(:Actor { name: "Keanu Reeves" })-[:ACTS_IN*0..5]-(:Actor { 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;
names and titleslength(p)
9 rows

["Keanu Reeves","The Matrix","Carrie-Anne Moss"]

2

["Keanu Reeves","The Matrix Reloaded","Carrie-Anne Moss"]

2

["Keanu Reeves","The Matrix Revolutions","Carrie-Anne Moss"]

2

["Keanu Reeves","The Matrix Reloaded","Laurence Fishburne","The Matrix","Carrie-Anne Moss"]

4

["Keanu Reeves","The Matrix Revolutions","Laurence Fishburne","The Matrix","Carrie-Anne Moss"]

4

["Keanu Reeves","The Matrix","Laurence Fishburne","The Matrix Reloaded","Carrie-Anne Moss"]

4

["Keanu Reeves","The Matrix Revolutions","Laurence Fishburne","The Matrix Reloaded","Carrie-Anne Moss"]

4

["Keanu Reeves","The Matrix","Laurence Fishburne","The Matrix Revolutions","Carrie-Anne Moss"]

4

["Keanu Reeves","The Matrix Reloaded","Laurence Fishburne","The Matrix Revolutions","Carrie-Anne Moss"]

4