【问题标题】:Structural Operators between graphs图之间的结构运算符
【发布时间】:2015-08-28 15:05:22
【问题描述】:

这个问题是前一个问题的“续集”。我是 spark graphx 和 scala 的新手,我想知道如何执行以下操作。

如何将两个图合并成一个新图,使新图具有以下属性:

对两个图的公共边的属性进行平均(或者更一般的方式,在边属性之间应用一个平均函数(边属性是双精度类型))

我们认为公共边=相同的srcId和相同的dstId,并且顶点和边是唯一的。

【问题讨论】:

    标签: scala apache-spark spark-graphx


    【解决方案1】:

    假设您只有两个图并且都包含相同的顶点集而没有重复的边,您可以使用合并边并在新图上使用 groupEdges 方法:

    val graph1: Graph[T,Double] = ???
    val graph2: Graph[T,Double] = ???
    
    Graph(graph1.vertices, graph1.edges.union(graph2.edges))
      .groupEdges((val1, val2) => (val1 + val2) / 2.0)
    

    或者更通用一点:

    Graph(graph1.vertices, graph1.edges.union(graph2.edges))
      .mapEdges(e => (e.attr, 1.0))
      .groupEdges((val1, val2) => (val1._1 + val2._1, val1._2 + val2._2))
      .mapEdges(e => e.attr._1 / e.attr._2)
    

    如果这还不够,您可以组合值并从头开始创建新图表:

    def edgeToPair (e: Edge[Double]) = ((e.srcId, e.dstId), e.attr)
    val pairs1 = graph1.edges.map(edgeToPair)
    val pairs2 = graph2.edges.map(edgeToPair)
    
    // Combine edges
    val newEdges = pairs1.union(pairs2)
      .aggregateByKey((0.0, 0.0))(
        (acc, e) => (acc._1 + e, acc._2 + 1.0),
        (acc1, acc2) => (acc1._1 + acc2._1, acc1._2 + acc2._2)
      ).map{case ((srcId, dstId), (acc, count)) => Edge(srcId, dstId, acc / count)}
    
    // Combine vertices assuming there are no conflicts
    // like different labels
    val newVertices = graph1.vertices.union(graph2.vertices).distinct
    
    // Create new graph
    val newGraph = Graph(newVertices, newEdges)
    

    其中aggregateByKey 可以替换为groupByKey,然后是需要一次所有值的映射,例如中位数。

    【讨论】:

    • 我非常喜欢这个答案。考虑到性能问题,考虑combineByKey 不是更合适吗?你怎么看?
    • 而不是aggregateByKey?
    • 否,而不是groupByKey
    • 计算中位数?我不这么认为。我们必须以一种或另一种方式一次查看所有值。如果唯一级别的数量很少,那么聚合计数可能会减少流量,但否则不太可能有帮助。
    • 你应该看看this answer by Grega Kešpret有没有办法获得不常见的边缘? - 我看不出有什么理由不这样做。不过,根据要求(如标签处理),您可以采用不同的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 2011-05-24
    • 2017-12-28
    • 2014-11-27
    相关资源
    最近更新 更多