2014-04-19 34 views
2

這與question有關。我有一個非常大的圖,在igraph中有100,000個頂點。每個頂點有一個屬性att這是一個邏輯值。邊緣用正整數權重加權。對於每個頂點v,我想總結連接v到頂點的邊的權重邊,其中att=T計算鄰域中有多少頂點具有igraph邊緣權重的給定屬性R

我們可以使用下面作爲一個例子

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

回答

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) 
相關問題