如何在R中的igraph中删除节点? [英] How do I delete nodes in igraph in R?

查看:89
本文介绍了如何在R中的igraph中删除节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个脚本,该脚本根据节点的连接数使用igraph来删除R中Barabasi-Albert网络中的节点(我正在尝试从复杂的错误和攻击容忍度"中重新创建一些基本结果网络"(Albert,Jeong和Barabasi的论文).我首先尝试删除五个随机节点,这些节点的数量少于网络中节点的平均连接数.但是,当我尝试删除节点后可视化网络时,它看上去确实有所不同,但是看起来好像节点没有被删除.因此,我不认为脚本可以正常工作.

I'm trying to write a script that deletes nodes in a Barabasi-Albert Network in R using igraph based on the node's number of connections (I'm trying to recreate some basic results from the "Error and attack tolerance of complex networks" paper by Albert, Jeong and Barabasi). I'm starting by trying to delete five random nodes that have less than the average number of connections of a node in the network. However, when I visualize the network after I attempt to delete the nodes, it does look different, but it doesn't look like the nodes are removed. So I'm not convinced the script is working.

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个),我得到以下错误:

The script runs when the number of nodes is smaller (like around 50) but when the number of nodes is higher (around 100) I get the following error:

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

我认为这与节点的命名约定有关,但是我不确定.我是网络科学的新手,也不是那里最好的程序员,因此,我将非常感谢您的帮助.谢谢!

I think it has to do with the naming conventions of the nodes, but I'm not sure. I'm new to network science and I'm not the best programmer out there so I would really appreciate any help. Thanks!

推荐答案

之所以会出现此错误,是因为由于您每次都是从 nrow(bar_mat)中随机抽样,因此您可以选择一个节点已被删除.因此,例如,循环第一次运行并选择第24行.它将运行并删除节点24.它将再次运行并选择第12行.它将运行并删除节点12.它将再次运行并选择第24行.现在,它可以无法运行,因为该图不再具有节点24!否则,您的代码就可以了,确实可以删除节点.

The error is raised because, since you are sampling randomly from nrow(bar_mat) newly each time, you can select a node that has already been deleted. So for example, the loop runs the first time and selects row 24. It will work and delete node 24. It runs again and selects row 12. It will work and delete node 12. It runs again and selects row 24. Now it can't run because the graph does not have node 24 anymore! Otherwise, your code is fine and is indeed deleting nodes.

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

You can solve this a number of ways, for example by sampling the 5 random nodes all at once (here I do 20 to clearly show that nodes have been deleted):

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软件包(v0.3.0)创建于2020-04-07 sup>

Created on 2020-04-07 by the reprex package (v0.3.0)

这篇关于如何在R中的igraph中删除节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆