本文介绍了计算邻域中有多少个顶点具有igraph中R的权重的给定属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与此.我有一个很大的图,在igraph中大约有100,000个顶点.每个顶点都有一个属性att,这是一个逻辑值.边使用正整数权重加权.对于每个顶点v,我想对将v连接到顶点att=T的边的边缘权重求和.

This is related to this question. I have a very large graph, on the order of 100,000 vertices in igraph. Each vertex has an attribute att which is a logical value. Edges are weighted with positive integer weights. For each vertex v, I would like to sum the edge weights of edges that connect v to a vertex where att=T.

我们可以使用以下示例

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

推荐答案

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

Here's one way to get, for each vertex v, the sum of edge weights of neighboring vertices with att=T. Gabor might have a more elegant way that is much faster.

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)

这篇关于计算邻域中有多少个顶点具有igraph中R的权重的给定属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 10:37