길이 == 2 인 두 노드 사이의 최단 경로가 아닌가요?
Neo4j에서는 GraphAlgoFactory의 shortestPath() Finder를 사용할 수 있습니다.
Node from_node = index.get("guid", "user_a").getSingle();
Node to_node = index.get("guid", "user_b").getSingle();
if(from_node != null && to_node != null) {
RelationshipExpander expander = Traversal.expanderForAllTypes(Direction.BOTH);
PathFinder<Path> finder = GraphAlgoFactory.shortestPath(expander, 2);
if(finder.findSinglePath(from_node, to_node) != null) {
//Connected by at least 1 common friend
} else {
//Too far apart or not connected at all
}
}
이 공통 친구가있는 사람들 당신을 말할 것이다 :
Node from_node = index.get("guid", "user_a").getSingle();
Node to_node = index.get("guid", "user_b").getSingle();
if(from_node != null && to_node != null) {
RelationshipExpander expander = Traversal.expanderForAllTypes(Direction.BOTH);
PathFinder<Path> finder = GraphAlgoFactory.shortestPath(expander, 2);
Iterable<Path> paths = finder.findAllPaths(from_node, to_node);
if(paths != null) {
for(Path path : paths) {
Relationship relationship = path.relationships().iterator().next();
Node friend_of_friend = relationship.getEndNode();
}
} else {
//Too far apart or not connected at all
}
}
이 코드는 약간 거친이며, 훨씬 쉽게 연결이있는 경우
당신은 GraphDB 기반 답변 이후라고 가정하고 있지만이 종류의 교차 설정은 관계형 DB가 지향하는 것입니다 (예 : 집합 기반 계산) – cdeszaq