简体   繁体   English

R:计算两个顶点之间的单个最短路径

[英]R: calculating single shortest path between two vertices

Currently, I am working on a project that involves NYC Taxi data, in which I am given where a person is picked up and dropped off in a network. 目前,我正在从事一个涉及NYC Taxi数据的项目,在该项目中,我得到在网络中上落人员的位置。

I am working with an ESRI shapefile , which I can load into R as an igraph object with the shp2graph package; 我正在使用ESRI shapefile ,可以使用shp2graph包将其作为igraph对象加载到R中; I need to utilize Dijkstra's algorithm (or a similar shortest-path algorithm) to find the single shortest path between two given vertices. 我需要利用Dijkstra的算法(或类似的最短路径算法)来找到两个给定顶点之间的最短路径。 I thought that the get.shortest.paths() method of the igraph package would be my solution, but to my surprise, this calculates all shortest paths from a vertex to all others in a network. 我以为igraph包的get.shortest.paths()方法将是我的解决方案,但令我惊讶的是,它计算从顶点到网络中所有其他顶点的所有最短路径。

To me, this seems like overkill, because I need only one single path between two specified nodes. 在我看来,这似乎有些过分,因为我只需要两个指定节点之间的一条路径即可。 I did some poking around online and in the igraph documentation, but all I can find are methods surrounding calculating many shortest paths from a given vertex to all others. 我在网上和igraph文档中进行了一些探索,但是我所能找到的是围绕计算从给定顶点到所有其他顶点的最短路径的方法。

Due to how computationally expensive it would be to calculate every single shortest path from a vertex, and then just select one from the behemoth of a list, I'm looking for a way to utilize Dijkstra's algorithm between two specified vertices in a graph. 由于从顶点计算每个最短路径,然后仅从列表的庞然大物中选择一个,这在计算上将是非常昂贵的,所以我正在寻找一种在图中两个指定顶点之间利用Dijkstra算法的方法。 Is there a way to do this in the igraph package, or if not, is there a good way to do this with a different package in R? 是否可以在igraph程序包中执行此操作,如果没有,是否可以使用R中的其他程序包执行此操作?

EDIT: In the end, I am hoping to look for a function that will take in the graph object and the ID of two vertices I wish to find the shortest path between, then return a list of paths/edges (or IDs) along that shortest path. 编辑:最后,我希望找到一个函数,该函数将接受图形对象和两个顶点的ID,我希望找到它们之间的最短路径,然后返回沿其的路径/边沿(或ID)列表最短路径。 This would help me to inspect each individual street along the shortest path between the two vertices. 这将帮助我检查沿两个顶点之间最短路径的每条街道。

EDIT: As an example of how I am currently using the function: path <- get.shortest.paths(NYCgraph, from=32, mode="out") . 编辑:作为当前我如何使用该函数的示例: path <- get.shortest.paths(NYCgraph, from=32, mode="out") Something I would hope to find is path <- shortestPathFunction(NYCgraph, from=32, to=37) to arbitrary calculate a shortest path between vertex ID 32 and vertex ID 37 (two random street intersections in the network). 我希望找到的是path <- shortestPathFunction(NYCgraph, from=32, to=37)可以任意计算顶点ID 32和顶点ID 37(网络中的两个随机街道交叉点)之间的最短路径。

I found my issue, which occurred before I called get.shortest.paths(). 我发现了问题,发生在我调用get.shortest.paths()之前。 For those who are curious on how to read in an ESRI shapefile, and find a single shortest path between two points (which was my dilemma): 对于那些对如何读取ESRI shapefile感到好奇并在两点之间找到一条最短路径的人(这是我的难题):

myShapefile <- readOGR(dsn=".", layer="MyShapefileName") # i.e. "MyShapefileName.shp"
shpData <- readshpnw(myShapefile, ELComputed=TRUE)
igraphShpObject <- nel2igraph(shpData[[2]], shpData[[3]], weight=shpData[[4]])
testPath <- get.shortest.paths(igraphShpObject, from=42, to=52) # arbitrary nodes
testPath[1] # print the node IDs to the console

Furthermore, if one was interested in getting the ID of the edge connecting two nodes (perhaps from nodes in the testPath): 此外,如果有兴趣获取连接两个节点(可能来自testPath中的节点)的边缘的ID:

get.edge.ids(igraphShpObject, c(42,45) # arbitrary nodes 42 and 45

This indexing is the same as the indexing in shpData; 此索引与shpData中的索引相同; for example, if you want to get the length of edge ID x, as found in get.edge.ids() , you may type shpData[[4]][x] . 例如,如果要获取边缘ID x的长度(如get.edge.ids() ,则可以键入shpData[[4]][x]

I hope these tidbits may be helpful to somebody in the future encountering the same problems! 我希望这些花絮对将来遇到相同问题的人有所帮助! This method utilizes the shp2graph , rgdal , and igraph packages in R. 此方法利用R中的shp2graphrgdaligraph软件包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM