在不知道距离的情况下聚类r中的网络节点

时间:2012-06-06 08:58:57

标签: r networking

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

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

我希望它们像ABCD和EFGH一样聚集。 有没有办法在R ??

中做到这一点

2 个答案:

答案 0 :(得分:2)

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

这是doc:

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

答案 1 :(得分:0)

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

## 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"