我想在igraph中手动实现Bron-Kerbosch算法。我想用来自Wikipedia page的图表来复制这个例子。这就是我到目前为止想出的解决方案。
library(igraph)
g <- graph.formula(1-2,1-5,5-2,2-3,3-4,4-5,4-6)
P <- as.vector(V(g))
bron_kerbosch <- function(R,P,X) {
if (length(P) == 0 && length(X) == 0) {
print (R)
} else {
for (v in P) {
neis <- as.vector(neighbors(g,v))
bron_kerbosch(union(R,v),intersect(P,neis),intersect(X,neis))
P <- c(P[-which(P==v)])
X <- c(X,v)
}
}
}
> bron_kerbosch(c(),P,c())
[1] 1 2 3
[1] 2 4
[1] 3 5
[1] 4 5
[1] 5 6答案应该是:[1,2,5,2,3,3,4,4,5,4,6]我从1开始索引,而不是从0开始。知道我做错了什么吗?
发布于 2017-04-03 07:29:42
我刚刚找到了解决方案。我使用了g图的索引,而不是名称:/。所以我改了两行:
P <- V(g)$name
neis <- neighbors(g,v)$name现在这将适用于字符而不是数字。结果是:
[1] "1" "2" "5"
[1] "2" "3"
[1] "5" "4"
[1] "3" "4"
[1] "4" "6"所以顶点不是有序的(5,4)应该是(4,5),但我认为这并不重要。不过,最好把它写成整数,而不是字符。
https://stackoverflow.com/questions/43173661
复制相似问题