[英]How to find shortest paths between two nodes based on certain edge attribute using igraph in R?
I have made a directed graph in Rstudio using igraph package. 我已经使用igraph包在Rstudio中制作了有向图。 For each edge I have set an edge attribute called edge_ids.
我为每个边缘设置了一个名为edge_ids的边缘属性。 Multiple edges are there with the same edge_id.
多个边具有相同的edge_id。 Now I want to find if there exists a path between 2 nodes with a particular edge_id?
现在,我想查找两个具有特定edge_id的节点之间是否存在路径?
So basically, suppose I have a graph of trips covered by 10 vehicles and each vehicle has its own vehicle_id. 因此,基本上,假设我有一张图表,其中包含10辆车,每辆车都有自己的vehicle_id。 Now if I want to find the shortest distance between 2 nodes in this graph, then it will consider all 10 vehicles, but what I want is that it should consider only that vehicle whose vehicle_id will be given by me.
现在,如果我想在该图中找到2个节点之间的最短距离,则它将考虑所有10辆车,但是我想要的是,它应仅考虑其车辆信息将由我给定的那辆车。
shortest.paths(total_network, 'ttn85jv', 'ttn9rjy')
E(total_network)[edge_id=="0358511023767613_132.csv"]
First line will give me shortest path between node ttn85jv and ttn9rjy. 第一行将为我提供节点ttn85jv和ttn9rjy之间的最短路径。 Second line will give me edge sequence vector having edge id=0358511023767613_132.csv.
第二行将为我提供边缘ID为= 0358511023767613_132.csv的边缘序列向量。
If edge id=0358511023767613_132.csv has created a path in the (total_network) graph, then I want to find if nodes ttn85jv and ttn9rjy are coming in between the path or not. 如果边缘id = 0355851023767613_132.csv在(total_network)图中创建了一条路径,那么我想查找节点ttn85jv和ttn9rjy是否在该路径之间。
If both the nodes exist in the path, then what is the path length between these two nodes for that id(0358511023767613_132.csv) 如果路径中同时存在两个节点,那么对于该id,这两个节点之间的路径长度是多少(0358511023767613_132.csv)
I have got an answer for this. 我有一个答案。 But it is not an optimal way, also it is taking very long time to execute because first it is making subgraph of the given graph(total_network).
但这不是最佳方法,而且执行时间也很长,因为首先要制作给定图的子图(total_network)。
s1 <- subgraph.edges(total_network, E(total_network)[edge_id=="0358511023767613_132.csv"])
shortest.paths(s1,'ttn9rjy','ttn85jv')
You already provide a functioning solution to your question, but Tthere are faster ways than subgraph.edges()
for checking edges by edge-id. 你已经提供一个有效的解决你的问题,但Tthere 比更快的方式
subgraph.edges()
检查由边缘-ID的边缘。
Try, for example to define n <- make_empty_graph(n = length(V(total_network)))
as an empty network and then find distances with this: 例如,尝试将
n <- make_empty_graph(n = length(V(total_network)))
为一个空网络,然后使用以下方法查找距离:
#s1 <- subgraph.edges(total_network, E(total_network)[edge_id=="0358511023767613_132.csv"])
s1 <- n %>% add_edges( as.vector(ends(total_network, E(total_network)[edge_id=="0358511023767613_132.csv"])) )
My version creates the subgraph much quicker. 我的版本可以更快地创建子图。 On my 2017 Mac, the speed advantages seem to be about 2x, but depend on network size.
在我的2017 Mac上,速度优势似乎约为2倍,但取决于网络大小。 Maybe the advantages are different for you size of network?
也许优势对于您的网络规模是不同的?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.