본문 바로가기

DKE/Neo4j

[Neo4j] MATCH (Shortest path) / 2023.02.07

https://neo4j.com/docs/cypher-manual/current/clauses/match/#query-shortest-path

 

MATCH - Cypher Manual

The `MATCH` clause is used to search for the pattern described in it.

neo4j.com

 

*Example Graph

1. Single shortest path

shortestPath() 함수를 사용하여 최단 경로 찾기

#Martin Sheen 과 Oliver Stone사이의 15홉이내 중 최단 경로 찾기
MATCH
  (martin:Person {name: 'Martin Sheen'}),
  (oliver:Person {name: 'Oliver Stone'}),
  p = shortestPath((martin)-[*..15]-(oliver))
RETURN p

2. Single shortest path with predicates (생략)

3. All shortest paths

allShortestPaths() 함수 사용하여 모든 최단 경로 찾기

#Martin Sheen 과 Michael Douglas 사이에 모든 최단 경로 찾기
MATCH 
(martin:Person {name: 'Martin Sheen'} ),  
(michael:Person {name: 'Michael Douglas'}),
p = allShortestPaths((martin)-[*]-(michael))
RETURN p