【发布时间】:2021-09-02 13:47:35
【问题描述】:
igraph 有两个有用的函数,shortest_paths 和 all_shortest_paths。前者输出最短路径之一,另一个输出所有最短路径。但是,前者也能输出路径的边缘类型,而后者似乎不能。
如何使用all_shortest_paths 提取所有最短路径的“边缘类型”?我在下面提供了澄清代码:
library(igraph)
m <- read.table(row.names=1, header=TRUE, text=
" A B C D
A 0 1 1 0
B 0 0 0 1
C 0 0 0 1
D 0 0 0 0")
m <- as.matrix(m)
ig <- graph.adjacency(m, mode="directed")
plot(ig)
E(ig)
for (i in 1:length(E(ig))) {
if (i == 4) {
E(ig)[i]$type <- -1
} else {
E(ig)[i]$type <- 1
}
}
###This gets one of the shortest paths
test1 <- shortest_paths(ig, from = V(ig)[1], to = V(ig)[4], mode = "out", output = "epath")
###This outputs the types for the shortest path above
test1$epath[[1]]$type
###This gets all the shortest paths
test2 <- all_shortest_paths(ig, from = V(ig)[1], to = V(ig)[4], mode = "out")
###The code below doesn't work. How do I get the types for ALL the shortest paths?
test2$res[[1]]$type
【问题讨论】:
-
all_shortest_paths的底层 C 函数用于仅返回顶点序列,而不是边索引序列。 It has been recently updated to also return edge indices. 因此,对 igraph 的 C 核心进行了必要的更新。随时在 GitHub 上为 R/igraph 打开一个问题,并请求将此改进也传播到 R 接口。