Neo4J图谱获取最短路径的Java代码实现

hutuyun-ad-banner.png ai-qy.gif

使用Java代码实现获取Neo4j图谱的最短路径。

1.cypher查询

MATCH (n1:GraphNode { id: '16560' }),(n2:GraphNode { id: '12312' }), p = shortestPath((n1)-[*]-(n2))
WHERE length(p)> 1
RETURN p

2.Java实现

@Component
public interface GraphNodeRepository extends Neo4jRepository<GraphNode,String> {

    @Query("MATCH (n1:GraphNode { id: {sourceId} }),(n2:GraphNode { id: {targetId} }), p = shortestPath((n1)-[*]->(n2))\n" +
            "WHERE length(p)> 1\n" +
            "RETURN p")
    List<GraphNode> calculateShortestPath(@Param("sourceId") String sourceId,@Param("targetId") String targetId);
}

References:

  1. Neo4J Java API – Shortest Path for specific node label or excluding a particular relationship
  2. neo4j-documentation:CalculateShortestPath.java
  3. Graph algorithm examples
  4. Shortest path planning
赞(0)
未经允许禁止转载:优米格 » Neo4J图谱获取最短路径的Java代码实现

评论 抢沙发

合作&反馈&投稿

商务合作、问题反馈、投稿,欢迎联系

广告合作侵权联系