gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 09:35:04 24 4
gpt4 key购买 nike

我正在尝试找到一种方法来突出显示图表上的一些最短路径。我正在尝试将 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com