r - 在图表上突出显示最短路径

标签 r shortest-path igraph

我正在尝试找到一种方法来突出显示图表上的一些最短路径。 我正在尝试将 get.shortest.paths 的输出自动用于 E() 的 path= 函数,但数据结构似乎错误。见下文:

###################################################################
g <- graph.ring(10,directed=TRUE)               
plot(g)
ShortPth <- get.shortest.paths(g, 8, 2)    # List of path 8->2
ShortPth
E(g)$color <- "SkyBlue2"
E(g)$width <- 1
E(g, path=ShortPth)$color <- "red" 

### setting edges by path= is failing !!!!!!!!!!
plot(g)
################################################# ###########

任何帮助将不胜感激......

最佳答案

我想你只是漏掉了几个括号。 path 参数需要一个数字向量,但 ShortPth 是一个列表。因此,您可以通过键入 ShortPth[[1]] 来提供向量 请尝试以下操作:

E(g, path=ShortPth[[1]])$color <- "red"
plot(g)

更新:

正如 jcarlos 在评论中指出的那样,上述解决方案使用 igraph_1.0.1 引发错误:

Error in as.igraph.vs(graph, path) : (list) object cannot be coerced to type 'double'

文档说 path 参数应该是 顶点列表,以选择沿路径的边。 但是,传入列表会引发错误。 igraph_1.0.1 目前正在使用以下任何一项:

E(g, path=ShortPth$vpath[[1]])$color <- "red"
E(g, path=unlist(ShortPth$vpath))$color <- "red"
E(g, path=unlist(ShortPth[[1]]))$color <- "red"

ShortPth$vpath[[1]]  # class "igraph.vs"
# + 5/10 vertices:
# [1]  8  9 10  1  2
unlist(ShortPth$vpath)
# [1]  8  9 10  1  2

关于r - 在图表上突出显示最短路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19827139/

相关文章:

r - R 中的IGRAPH : Find the path between vertices that maximizes the product of edge attributes

algorithm - Dijkstra 源到目标的有向加权图中的最短路径

python - 从 0 到 f 的字符列表

r - 在igraph中按簇折叠图

r - 以日期作为 r 中绘制值的 float 条形图

python - 使用 RPy2 和 dplyr 时为 "Error while parsing the string"

R corrplot colorlegend 变化范围

R Package "rvest"无法安装,与 Ubuntu 终端中的情况相同

python - 在最大化边权重的同时断开图的有效方法

r - 关于igraph中的介数函数