我正在尝试使用Kou算法来识别R中使用igraph的Steiner树。
Kou的算法可以这样描述:
前两个步骤很简单:
g <- erdos.renyi.game(100, 1/10) # graph
V(g)$name <- 1:100
# Some steiner nodes
steiner.points <- sample(1:100, 5)
# Complete distance graph G'
Gi <- graph.full(5)
V(Gi)$name <- steiner.points
# Find a minimum spanning tree T' in G'
mst <- minimum.spanning.tree(Gi)
但是,我不知道如何更换T&#39;对于G中的最短路径,我知道使用get.shortest.paths
我可以从一对节点获得vpath
,但是如何在T&#39;中替换和边缘。使用G中的shortest.path
非常感谢提前
答案 0 :(得分:7)
如果我在编写算法时理解算法,我认为这可以帮助您完成第3步,但请澄清是否不是这样:
library(igraph)
set.seed(2002)
g <- erdos.renyi.game(100, 1/10) # graph
V(g)$name <- as.character(1:100)
## Some steiner nodes:
steiner.points <- sample(1:100, 5)
## Complete distance graph G'
Gi <- graph.full(5)
V(Gi)$name <- steiner.points
## Find a minimum spanning tree T' in G'
mst <- minimum.spanning.tree(Gi)
## For each edge in mst, replace with shortest path:
edge_list <- get.edgelist(mst)
Gs <- mst
for (n in 1:nrow(edge_list)) {
i <- edge_list[n,2]
j <- edge_list[n,1]
## If the edge of T' mst is shared by Gi, then remove the edge from T'
## and replace with the shortest path between the nodes of g:
if (length(E(Gi)[which(V(mst)$name==i) %--% which(V(mst)$name==j)]) == 1) {
## If edge is present then remove existing edge from the
## minimum spanning tree:
Gs <- Gs - E(Gs)[which(V(mst)$name==i) %--% which(V(mst)$name==j)]
## Next extract the sub-graph from g corresponding to the
## shortest path and union it with the mst graph:
g_sub <- induced.subgraph(g, (get.shortest.paths(g, from=V(g)[i], to=V(g)[j])$vpath[[1]]))
Gs <- graph.union(Gs, g_sub, byname=T)
}
}
par(mfrow=c(1,2))
plot(mst)
plot(Gs)
左侧最小生成树的图,替换为右侧的最短路径: