在R igraph
包中,有两种函数适用于基于两种算法搜索连接 - graph.dfs
(深度优先搜索)和graph.bfs
(宽度优先搜索)。
library(igraph)
graph.dfs(g, "A", neimode = "out", dist = T)$dist
A B C D E
0 1 2 0 2
graph.bfs(g, "A", neimode = "out", dist = T)$dist
A B C D E
0 1 2 0 2
为你的情况另一个有用的功能是all_shortest_path()
,这给从顶点开始的所有路径:
all_shortest_paths(g, "A", mode = "out")
$res
$res[[1]]
+ 1/5 vertex, named:
[1] A
$res[[2]]
+ 2/5 vertices, named:
[1] A B
$res[[3]]
+ 3/5 vertices, named:
[1] A B C
$res[[4]]
+ 3/5 vertices, named:
[1] A B E
$nrgeo
[1] 1 1 1 0 1
尽管这并不完全解决您的问题,它可能会提供一些有用的提示。
应选择'C'也找回除了'A-> B-> C''D-> B-> C'?图表无法区分这一点,是吗? –
我同意@circuit咖啡 - 虽然输入侧可能知道它们是分开的路径,并且图形被绘制为表明它们彼此成直角,但这是任意的。您是否可以在输入时创建属性或标签以记住这些数据? – thelatemail
@ mathematical.coffee:你说得对,我纠正了我的问题 – hatmatrix