【问题标题】:Neo4j Count the most connected parent node given a set of child nodeNeo4j 给定一组子节点,计算连接最多的父节点
【发布时间】:2018-12-26 12:17:38
【问题描述】:

假设我有多个树:

A<-{D, E}<-F
B<-{E, G}
C<-{E, H}
//Where only A, B, and C are of (:parent{name:""})
//There rest is child

给定一组子节点:

{E, F} //(:child{name:""})
//Clearly A is the most connected parent even if F is not directly connected to A

问题:在给定子节点集合的情况下,如何找到连接最多的父节点?欢迎任何密码查询、插件功能或程序。帮助。

这是我尝试过但没有运气的方法,因为它计算了两个节点之间的总关系:

MATCH (c:child)--(p:parent)
WHERE c.name IN ['E', 'F']
RETURN p ORDER BY size( (p)--(c) ) DESC LIMIT 1
//Also tried size( (p)--() ) but it count all relationship that the parent node has.

【问题讨论】:

    标签: neo4j cypher


    【解决方案1】:

    您缺少的概念是variable-length relationship patterns。有了这个,你可以从你需要的 :child 节点匹配到 :parent 节点在不同的距离,然后计算父节点出现的次数并占据顶部:

    MATCH (c:child)-[*]->(p:parent) // assumes only incoming rels toward :parent
    WHERE c.name IN ['E', 'F'] // make sure you have an index on :child(name)
    WITH p, count(p) as connected
    RETURN p 
    ORDER BY connected DESC 
    LIMIT 1
    

    【讨论】:

      【解决方案2】:

      好的,所以我尝试了其他方法,但不确定在大型图上工作是否有效(比如 2M 个节点+):

      MATCH path= shortestPath( (c:child)--(p:parent) )
      WHERE c.name IN [...]
      WITH p, collect(path) as cnt
      RETURN p, size(cnt) AS nchild
      ORDER BY nchild DESC LIMIT 1
      

      对此有何看法?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-11
        • 1970-01-01
        相关资源
        最近更新 更多