0

我有一个网络。其中节点连接如下:

A-B
B-C
A-C
B-D
E-F
F-G
H-G
H-E

我希望它们像 ABCD 和 EFGH 一样聚集在一起。有没有办法在 R 中做到这一点?

4

2 回答 2

3

这被称为图形的“连接组件”,并且存在于igraph包中。

这是文档:

http://igraph.sourceforge.net/doc/R/clusters.html

于 2012-06-06T09:00:38.503 回答
1

以为我会拼凑一个直接的解决方案来提供有用的比较(并练习我的编码 - 欢迎所有指针......)

## initial link data frame (with start and end points)
link=data.frame(st=c("A","B","A","B","E","F","H","H"),
                end=c("B","C","C","D","F","G","G","E"),
                stringsAsFactors=FALSE)

## form blank list where clusters will build up (works up 
## to n=26!)
n=nrow(link)
L=c(); for (j in seq_len(n)) {L=c(L,list(NULL))}
names(L)=LETTERS[seq_len(n)]

## for each link in turn, pull together a collection of
## everything in the same cluster as either end, and update
## all relevant entries in L
for (j in seq_len(n)) {
    clus=sort(unique(c(link$st[j],link$end[j],
                       L[[link$st[j]]],L[[link$end[j]]])))
    for (k in clus) {L[[k]]=clus}
}
print(L)

正如预期的那样,输出是:

$A
[1] "A" "B" "C" "D"

$B
[1] "A" "B" "C" "D"

$C
[1] "A" "B" "C" "D"

$D
[1] "A" "B" "C" "D"

$E
[1] "E" "F" "G" "H"

$F
[1] "E" "F" "G" "H"

$G
[1] "E" "F" "G" "H"

$H
[1] "E" "F" "G" "H"
于 2012-06-06T09:27:57.467 回答