如何从 R 中的网络扩展给定节点周围的社区(通过设置我自己的种子节点)?

How can I expand a community around a given node (by setting my own seed nodes) from a network in R?

{library(igraph)
g <- graph.famous("Zachary")
SelectedSeeds <- c(1,34)
no_Hope <- 2
mat_nei<-matrix(list(),nrow = length(SelectedSeeds),ncol = 1)
for(i in 1:length(SelectedSeeds)){
  mat_nei[i,1]<-neighborhood(g,no_Hope,SelectedSeeds[i],mode = c("all","out","in"),mindist = 0)
}
#flatenning list into vectors
l1<-unlist(mat_nei[1], recursive = TRUE, use.names = TRUE)
l2<-unlist(mat_nei[2], recursive = TRUE, use.names = TRUE)       
}

以上代码为 "SelectedSeeds" 创建了一个邻里社区。在此如何找到节点的成员资格?

你想要下面这样的东西吗?

l1_l2_both <- intersect(l1, l2)
#  [1]  1  2  3  4  9 14 20 32 31 10 28 29 33 34 25 26
l1_not_l2 <- setdiff(l1, l2)
#  [1]  5  6  7  8 11 12 13 18 22 17
l2_not_l1 <- setdiff(l2, l1)
# [1] 15 16 19 21 23 24 27 30
not_l1_not_l2 <- setdiff(as.numeric(V(g)), union(l1,l2))
# numeric(0)

V(g)$color <- 'green'
V(g)[V(g) %in% l1_not_l2]$color <- 'red'
V(g)[V(g) %in% l2_not_l1]$color <- 'blue'
plot(g)

或等效

community <- rep(0, length(V(g)))
community[V(g) %in% l1_l2_both] <- 1
community[V(g) %in% l1_not_l2] <- 2
community[V(g) %in% l2_not_l1] <- 3
community
#  [1] 1 1 1 1 2 2 2 2 1 1 2 2 2 1 3 3 2 2 3 1 3 2 3 3 1 1 3 1 1 3 1 1 1 1
plot(g, vertex.color=community)