如何在 R 中删除 igraph 中的节点?
Posted
技术标签:
【中文标题】如何在 R 中删除 igraph 中的节点?【英文标题】:How do I delete nodes in igraph in R? 【发布时间】:2020-07-19 19:21:29 【问题描述】:我正在尝试编写一个脚本,根据节点的连接数使用 igraph 在 R 中删除 Barabasi-Albert 网络中的节点(我正在尝试从“复杂的错误和攻击容限”中重新创建一些基本结果网络”由 Albert、Jeong 和 Barabasi 撰写的论文)。我首先尝试删除五个随机节点,这些节点的连接数少于网络中节点的平均连接数。但是,当我在尝试删除节点后可视化网络时,它看起来确实有所不同,但看起来节点并没有被删除。所以我不相信脚本有效。
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
我认为这与节点的命名约定有关,但我不确定。我是网络科学的新手,我不是最好的程序员,所以我非常感谢任何帮助。谢谢!
【问题讨论】:
【参考方案1】:由于每次都是从nrow(bar_mat)
中随机抽样,所以会出现这个错误,你可以选择一个已经被删除的节点。例如,循环第一次运行并选择第 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
由reprex package (v0.3.0) 于 2020-04-07 创建
【讨论】:
以上是关于如何在 R 中删除 igraph 中的节点?的主要内容,如果未能解决你的问题,请参考以下文章