Search the movies dataset for the co-stars of Tom Hanks's co-stars, and the person at the top of the results is Tom Hanks. By default, a pattern follows each relationship only once, and a node can be reached as many times as the pattern allows.
If patterns are new to you, start with Cypher Fundamentals, which uses the same movies dataset.
Find the co-stars of Tom Hanks's co-stars
The following query finds the cast of the movies that Tom Hanks's co-stars acted in, and counts the paths to each person:
MATCH (tom:Person {name: 'Tom Hanks'})-[:ACTED_IN]->(:Movie)<-[:ACTED_IN]-(:Person)
-[:ACTED_IN]->(:Movie)<-[:ACTED_IN]-(other:Person)
RETURN other.name AS name, count(*) AS paths
ORDER BY paths DESC, name
LIMIT 5Tom Hanks is at the top with 12 paths, more than twice any other person.
| name | paths |
|---|---|
| Tom Hanks | 12 |
| Tom Cruise | 5 |
| Zach Grenier | 5 |
| Bill Paxton | 4 |
| Cuba Gooding Jr. | 4 |
In six of those 12 paths the middle co-star is Meg Ryan, who shares three films with Tom Hanks. The following graph steps through one of the six, to show that every step uses a different relationship and the path still ends at Tom Hanks:
- 1
Start at Tom Hanks. Follow his
ACTED_INrelationship to Sleepless in Seattle. - 2
Follow Meg Ryan's relationship to the same movie. The relationship is different from the one in step 1.
- 3
Follow Meg Ryan's relationship to You've Got Mail. Different again.
- 4
Follow Tom Hanks's relationship to You've Got Mail. The relationship isn't the one from step 1, so the rule allows it, and the path ends at Tom Hanks.
Relationships unique, nodes duplicated
Under the default match mode in Cypher, DIFFERENT RELATIONSHIPS, each relationship is followed only once per MATCH clause. All four relationships above are different, so the path is valid. There is no restriction on how many times a node can appear in a path, so Tom Hanks can be both the start and the end of the same path.
You can write the mode after MATCH, which makes the default visible. The following query counts the people and paths found by the pattern above:
MATCH DIFFERENT RELATIONSHIPS (tom:Person {name: 'Tom Hanks'})-[:ACTED_IN]->(:Movie)<-[:ACTED_IN]-(:Person)
-[:ACTED_IN]->(:Movie)<-[:ACTED_IN]-(other:Person)
RETURN count(DISTINCT other) AS people, count(*) AS pathsThe result set holds 70 people and 147 paths, the same as without the keyword, because the keyword only names the default. Tom Hanks is one of the 70, and the next section removes him:
| people | paths |
|---|---|
| 70 | 147 |
A relationship variable that appears twice in one pattern, such as (a)-[r]->(m)<-[r]-(b), can never match under this mode, so that query returns no rows.
Remove Tom Hanks with WHERE
To remove Tom Hanks from his own results, compare other with tom in a WHERE clause:
MATCH (tom:Person {name: 'Tom Hanks'})-[:ACTED_IN]->(:Movie)<-[:ACTED_IN]-(:Person)
-[:ACTED_IN]->(:Movie)<-[:ACTED_IN]-(other:Person)
WHERE other <> tom
RETURN count(DISTINCT other) AS people, count(*) AS pathsBecause the filter removes every row where other is the node bound to tom, the count falls from 70 people to 69 and from 147 paths to 135, the 12 paths that ended at Tom Hanks.
| people | paths |
|---|---|
| 69 | 135 |
Require unique nodes with ACYCLIC
Cypher supports an ACYCLIC path mode. The mode rejects any path that contains the same node more than once, which is stricter than the default, and you write it before the path pattern:
MATCH ACYCLIC (tom:Person {name: 'Tom Hanks'})-[:ACTED_IN]->(:Movie)<-[:ACTED_IN]-(:Person)
-[:ACTED_IN]->(:Movie)<-[:ACTED_IN]-(other:Person)
RETURN count(DISTINCT other) AS people, count(*) AS pathsThe counts match the WHERE version, because Tom Hanks is the only node this pattern repeats. ACYCLIC would also have removed a repeated movie or middle co-star, which a WHERE clause covers only if you name it. If you also bind the path to a variable, put the variable first, as in MATCH p = ACYCLIC (...).
| people | paths |
|---|---|
| 69 | 135 |
Choosing a mode
Choose the mode by what a pattern may reuse:
- If you only want each relationship followed once, write
MATCHorMATCH DIFFERENT RELATIONSHIPS. - If you want each node to appear only once as well, write
MATCH ACYCLIC. - If you want no restrictions, write
MATCH REPEATABLE ELEMENTS, the mode for following relationships multiple times in Cypher.
To decide when WHERE NOT is still necessary, read Relationship Uniqueness in Cypher, and When You Need WHERE NOT.
Comments (0)
Loading comments...