簡體   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