简体   繁体   English

如何在R中使用igraph根据某些边缘属性找到两个节点之间的最短路径?

[英]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.

相关问题 igraph基于edge属性的最短路径 - Shortest Paths based on edge attribute with igraph 使用igraph / R查找所有最短路径 - Find All Shortest Paths using igraph/R 如何基于R中igraph中的节点属性值在节点之间生成边缘? - How do I generate an edge between nodes based on the node attribute values in igraph in R? 如何在R中使用igraph根据其长度选择图的某些路径 - How to select certain paths of a graph based on their length, using igraph in R R / igraph删除满足特定要求的特定节点之间的边缘? - R / igraph Removing the edge between specific nodes that fulfill a certain requirement? 使用 R / igraph,有没有办法在考虑到唯一节点属性的计数的情况下找到节点之间的最短路径? - Using R / igraph, is there a way to find a shortest path between nodes taking the count of unique node attributes into account? 如何从igraph R连接最短路径 - How to concatenate shortest paths from igraph R 在igraph(R)中执行shortest.paths时如何获取边缘属性的列表 - How to get the a list of the edge attributes when performing shortest.paths in igraph (R) R和igraph:基于入射在边缘的其他节点的属性的子图节点 - R and igraph: subgraph nodes based on attributes of other nodes that are incident on edge R中的igraph:get.shortest.paths错误,但shortsts.paths错误 - igraph in R: error with get.shortest.paths but not with shortest.paths
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM