给定一个R数据帧中无向和未加权边的列表(称为edges_df,其中数据帧的每一行包含id、start_x、end_x、start_y、end_y);我如何找到从每个节点到任何特定节点的最短路径,比如节点 3(id = 3 的节点),它在另一个数据帧(称为 nodes_df)中找到,该节点具有 id、x_value 和 y_value 作为列,穿越网络边缘时。为了方便起见,我在下面附上了用于创建数据帧和数据帧屏幕截图的 R 代码。
edges_df <- data.frame(
id=c(1,2,3,4,5,6,7,8,9,10),
start_x=c(46646.8,46646.8,46680.2,46680.2,46713.5,46713.5,46713.5,46662.2,46662.2,46662.2), end_x=c(46646.8,46680.2,46780.5,45723.5,46813.5,46813.5,46962.2,47662.2,46662.2,44879.4),
start_y= c(7392.8, 7392.8,7459.4,7659.4,7659.4,7852.3, 7182.7, 7143.9, 7243.9, 7899.5),
end_y= c(7412.8, 7536.2,7346.2,7642.5, 7642.5, 7271.3,7653.7,7392.8,7921.4,7392.8)
)
nodes_df <- data.frame(
id=c(1,2,3,4,5),
x_value= c(46728.9,46970.9,45236.9, 47852.9, 50123.9),
y_value= c(7463.89,7563.89,7417.89,7417.89,7407.89)
)
edges_df
nodes_df
例如:
library(igraph)
g <- make_ring(6)
specific <- 3
distances(g, to=specific)
## [,1]
## [1,] 2
## [2,] 1
## [3,] 0
## [4,] 1
## [5,] 2
## [6,] 3
这给出了一个矩阵,其中包含从任何节点到所选节点的所有距离。
?distances
显示相关文档。