Kou使用igraph找到Steiner树的算法

use*_*782 13 r igraph

我正在尝试使用igraph来实现Kou算法来识别R中的Steiner树.

Kou的算法可以这样描述:

  1. 找到完整的距离图G'(G'有V'= S(steiner节点),对于VxV中的每对节点(u,v),有一条边的权重等于最小成本路径的权重这些节点p_(u,v)在G)
  2. 找到G'中的最小生成树T'
  3. 通过用T'的每个边来代替构造G的子图Gs,这是G'的边缘,具有相应的G的最短路径(它有几个最短的路径,选择任意一个).
  4. 找到Gs的最小生成树Ts(如果有几个最小的生成树,则选择任意一个)
  5. 如果需要,通过删除Ts中的边来构造一个Steiner树Th,从Th中的所有叶子都是Steiner节点.

前两个步骤很简单:

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)
Run Code Online (Sandbox Code Playgroud)

但是,我不知道如何用G'中的最短路径替换T'中的边缘.我知道get.shortest.paths我可以vpath从一对节点获取,但是我如何用shortest.pathG 中的T'替换和边缘?

提前谢谢了

For*_*ens 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)
Run Code Online (Sandbox Code Playgroud)

左侧最小生成树的图,替换为右侧的最短路径:

网络情节