【发布时间】: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
我认为这与节点的命名约定有关,但我不确定。我是网络科学的新手,我不是最好的程序员,所以我非常感谢任何帮助。谢谢!
【问题讨论】:
标签: r networking nodes igraph