2
votes

I'm trying to write a query to find all actors/actresses with a Kevin Bacon number of 2, where they acted in a movie with someone who acted in a movie with Kevin Bacon, but they have never acted in a movie with Kevin Bacon themselves. So far I have:

MATCH (a:Actor{name: "Kevin Bacon"})-[:ACTS_IN]->(m:Movie)<-[:ACTS_IN]-(b:Actor)
MATCH (b:Actor)-[:ACTS_IN]->(n:Movie)<-[:ACTS_IN]-(c:Actor)
WHERE b.name <> "Kevin Bacon" AND c.name <> "Kevin Bacon"
RETURN c.name

Where Actor c has a kevin bacon number of 2. But this displays actors with a Kevin Bacon number of 1 as well (they acted in a movie with Kevin Bacon). Any help with filtering out actors with a Kevin Bacon number of 1 would be greatly appreciated

1

1 Answers

2
votes

You're close!

The missing piece is specifying that you do not want your :Actor c to have acted in any movie with Kevin Bacon.

Try this query:

MATCH (a:Actor{name: "Kevin Bacon"})-[:ACTS_IN]->(m:Movie)<-[:ACTS_IN]-(b:Actor)
MATCH (b:Actor)-[:ACTS_IN]->(n:Movie)<-[:ACTS_IN]-(c:Actor)
WHERE c <> a AND NOT (a)-[:ACTS_IN]->()<-[:ACTS_IN]-(c)
RETURN c.name