【问题标题】:Induced subgraphs in neo4jNeo4j中的诱导子图
【发布时间】:2013-06-05 14:56:21
【问题描述】:

我在 neo4j 中有一个图,对于给定的节点 N,我想找到在距离 N 不超过 P 步的路径中可到达的所有节点,以及该组节点之间的所有链接。似乎这可以通过 Cypher 或 Traversal 框架实现;一个比另一个更受欢迎吗?我正在使用嵌入式数据库从 Java 执行此操作,并且我需要对子图执行进一步的查询。我四处寻找,并没有找到任何确凿的答案。

【问题讨论】:

    标签: java neo4j subgraph


    【解决方案1】:

    我认为 cypher 是获取所需数据、查询可变长度路径、一些收集和提炼的最简洁方式:

    如果 n 是您的节点 N 的内部 id 并且您的 P 是 5:

    START begin = node(n)             // or e.g. index lookup
    MATCH p = (begin)<-[r*..5]-(end)  // match all paths of length up to 5
    WITH distinct nodes(p) as nodes   // collect the nodes contained in the paths
    MATCH (x)<-[r]-(y)                // find all relationships between nodes
    WHERE x in nodes and y in nodes   // which were found earlier
    RETURN distinct x,r,y             // and deduplicate as you find all pairs twice
    

    这可能不是最有效的方法,但至少http://console.neo4j.org/ 中的执行计划解释表明y in nodesMATCH (x)-[r]-(y) 之前被考虑。

    我想不出避免两次匹配关系的方法,因此返回语句中的distinct

    【讨论】:

    • 谢谢,我会试试的!似乎创建一个新的嵌入式数据库并将图放入其中是对该子图执行附加操作的最佳方式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-13
    • 2014-04-06
    • 2010-09-29
    相关资源
    最近更新 更多