【问题标题】:Counting how many vertices in a neighbourhood have a given attribute with edge weights in igraph for R计算邻域中有多少个顶点具有给定属性,并在 igraph 中为 R 提供边权重
【发布时间】:2014-04-19 03:28:42
【问题描述】:

这与此question 有关。我有一个非常大的图,igraph 中大约有 100,000 个顶点。每个顶点都有一个属性att,它是一个逻辑值。边缘使用正整数权重进行加权。对于每个顶点 v,我想将连接 vatt=T 的顶点的边的边权重相加。

我们可以使用下面的例子

set.seed(42)
g <- erdos.renyi.game(169081, 178058, type="gnm")
V(g)$att <- as.logical(rbinom(vcount(g), 1, 0.5))

【问题讨论】:

    标签: r igraph


    【解决方案1】:

    这是一种获取每个顶点v, 的相邻顶点与att=T 的边权重之和的方法。 Gabor 可能有一种更优雅的方式,速度更快。

    library(igraph)
    set.seed(42)
    g <- erdos.renyi.game(169081, 178058, type="gnm")
    V(g)$att <- as.logical(rbinom(vcount(g), 1, 0.5))
    E(g)$weight <- sample(10, ecount(g), replace=TRUE) #integer weights
    
    sumEdgeWeightsOfEdgesFromVertexV<- function(v){
      totwt <- 0
      #get all neighbors of vertex v
      all_neighbors_of_v <- V(g)[nei(v)]
      #subset to those with att to be TRUE
      att_nei <- as.vector(all_neighbors_of_v[all_neighbors_of_v$att==TRUE])
      #sum all the weights, edge by edge
      for( ver in att_nei) {
        totwt <- totwt + E(g, c(v,ver))$weight
      }
      totwt
    }  
    
    # sapply(V(g), sumEdgeWeightsOfEdgesFromVertexV)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-28
      • 2015-05-07
      • 2019-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多