我有如下边缘列表(简单示例):
DT <- data.frame(x = c(letters[1:7],"a","b","c","a","d","e","f"), y = c(letters[1:7],"b","a","a","c","f","f","d"))
> DT
x y
1 a a
2 b b
3 c c
4 d d
5 e e
6 f f
7 g g
8 a b
9 b a
10 c a
11 a c
12 d f
13 e f
14 f d
然后,我使用以下代码对此进行了绘制:
require(igraph)
myadj <- get.adjacency(graph.edgelist(as.matrix(DT), directed=FALSE))
my_graph <- graph.adjacency(myadj)
layout <- layout.fruchterman.reingold(my_graph,niter=500,area=vcount(my_graph)^2.3,repulserad=vcount(my_graph)^2.8)
plot(my_graph, vertex.size=10,
vertex.label.cex=1,
edge.arrow.size=0, edge.curved=TRUE,layout=layout)
我现在想去的是提取所有封闭节点的集合。我不确定典型的符号是什么样,但是我可以想象:
node set
1 a 1
2 b 1
3 c 1
4 d 2
5 e 2
6 f 2
7 g 3
我到处寻找函数/算法,但是我认为我无法正确表达我在寻找什么。因为我对图论还很陌生,所以我也不知道一开始就使用正确的格式。使用边列表,邻接矩阵(稀疏或完整)是否最简单?
最佳答案
尝试
data.frame(node = V(my_graph)$name, set = membership(clusters(my_graph)))
# node set
# 1 a 1
# 2 b 1
# 3 c 1
# 4 d 2
# 5 e 2
# 6 f 2
# 7 g 3