繁体   English   中英

如何在 R 中删除 igraph 中的节点?

[英]How do I delete nodes in igraph in R?

我正在尝试编写一个脚本,使用 igraph 基于节点的连接数删除 R 中的 Barabasi-Albert 网络中的节点(我试图从“复杂网络的错误和攻击容限”论文中重新创建一些基本结果阿尔伯特、郑和巴拉巴西)。 我首先尝试删除五个随机节点,这些节点的连接数少于网络中节点的平均连接数。 但是,当我在尝试删除节点后可视化网络时,它看起来确实有所不同,但看起来节点并没有被删除。 所以我不相信脚本有效。

nnodes=50 #number of nodes
test.graph<-barabasi.game(nnodes,.5) #create B-A network
test.graph2=test.graph #create a second B-A network for removing nodes
bar_mat=matrix(0,nrow=nnodes,ncol=1) #create empty matrix
for (i in 1:nnodes){
bar_mat[i,]=sum(test.graph[,i]) #sums up the number of connections of each node
}
###Visualizing the network before removing nodes
barabasi.community<-walktrap.community(test.graph) #this is supposed to visualize the most
#connected nodes in the network
members<-membership(barabasi.community)
plot.igraph(test.graph,
        layout=layout.fruchterman.reingold,
        vertex.size=10,
        vertex.label.cex=.5,
        edge.arrow.size=.5,
        mark.groups=list(members),
        mark.col="green"
)
f=c()

for (k in 1:5){ #checking five random nodes
 a=sample(1:nrow(bar_mat),1) #select random node
 if(bar_mat[a,]<=mean(bar_mat)){
  test.graph2 <- delete.vertices(test.graph2,a) # this is supposed to delete 
  #the node based on if it has lower than the average amount of connections
  i=i+1 #counting how many nodes of the five are actually removed
 }
f[k]=a #putting the nodes tested into a vector
a=0 #resetting a
}
###Visualizing network after node removal 
barabasi.community2<-walktrap.community(test.graph2)
members2<-membership(barabasi.community2)
plot.igraph(test.graph2,
        layout=layout.fruchterman.reingold,
        vertex.size=10,
        vertex.label.cex=.5,
        edge.arrow.size=.5,
        mark.groups=list(members2),
        mark.col="pink"
) 

该脚本在节点数较少(如大约 50 个)时运行,但当节点数较多(大约 100 个)时,我收到以下错误:

Error in delete.vertices(test.graph2, a) : 
At iterators.c:759 : Cannot create iterator, invalid vertex id, Invalid vertex id

我认为这与节点的命名约定有关,但我不确定。 我是网络科学的新手,我不是最好的程序员,所以我非常感谢任何帮助。 谢谢!

引发错误的原因是,由于您每次都是从新的nrow(bar_mat)中随机抽样,因此您可以 select 一个已删除的节点。 例如,循环第一次运行并选择第 24 行。它将运行并删除节点 24。它再次运行并选择第 12 行。它将运行并删除节点 12。它再次运行并选择第 24 行。现在它可以' 不运行,因为该图不再有节点 24,否则。 您的代码很好,并且确实在删除节点。

您可以通过多种方式解决此问题,例如一次对 5 个随机节点进行采样(这里我做了 20 个以清楚地显示节点已被删除):

library(igraph)

nnodes=100 #number of nodes
test.graph<-barabasi.game(nnodes,.5) #create B-A network
test.graph2=test.graph #create a second B-A network for removing nodes
bar_mat=matrix(0,nrow=nnodes,ncol=1) #create empty matrix
for (i in 1:nnodes){
  bar_mat[i,]=sum(test.graph[,i]) #sums up the number of connections of each node
}

vcount(test.graph)
#> [1] 100

# Sample 5 random nodes
set.seed(1491)
a=sample(1:nrow(bar_mat), 20)

for (i in a) {
  if(bar_mat[i,]<=mean(bar_mat)){a
    test.graph2 <- delete.vertices(test.graph2,i)
    }
  }
#> Error in delete.vertices(test.graph2, i): At iterators.c:759 : Cannot create iterator, invalid vertex id, Invalid vertex id

vcount(test.graph)
#> [1] 100
vcount(test.graph2)
#> [1] 88

代表 package (v0.3.0) 于 2020 年 4 月 7 日创建

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM