Traversing the Graph

Finding co-actors

Co-actors of Robert Blake

Given this query and what you have learned about graph traversal:

cypher
MATCH (p:Person {name: 'Robert Blake'})-[:ACTED_IN]->(m:Movie),
 (coActors:Person)-[:ACTED_IN]->(m)
RETURN m.title, collect(coActors.name)

How many relationships are traversed to return the result?

  • ✓ 16

Hint

The query finds each movie that Robert Blake acted in. How many ACTED_IN traversals is that?

For each movie, it then finds all actors to acted in that movie. It does not traverse the ACTED_IN relationship from Robert Blake twice.

How many ACTED_IN relationships are traversed?

Solution

This query traverses a total of 16 ACTED_IN relationships.

The query finds each movie that Robert Blake acted in. How many ACTED_IN traversals is that?

For each movie, it then finds all actors to acted in that movie. It does not traverse the ACTED_IN relationship from Robert Blake twice.

Summary

In this challenge, you determined the number of relationships that are traversed during query.

In the next challenge, you will answer another question about query traversal.