2017-01-15 34 views
1

如何訪問igraph中圖的top3連接組件的ID?igraph獲取連接組件的ID

c <- igraph::components(g, mode = 'weak') 
which(c$membership == which.max(c$csize)) 

將給予最大的 和

which(c$membership == which.max(c$csize-1)) 

相同的結果c$csize-1只會減去-1從所有價值。

回答

2

您可以使用order排序,找出前3大簇的成員,並使用%in%檢查頂點是他們中的一個內:

which(c$membership %in% order(c$csize, decreasing = TRUE)[1:3]) 

  • order(c$csize, decreasing = TRUE)給出了指數(對應於羣集ID),它將按降序對size進行排序;
  • c$membership包含所有頂點的簇ID;
  • 使用%in%來檢查羣集ID是否在前三位;
1

您可以使用tail提取前3個(按大小)組件,然後遍歷這些值以獲取組件的成員。

top3 <- which(c$csize %in% tail(sort(c$csize),3)) 
sapply(top3, function(x) which(c$membership == x))