0

I am using NetowrkX and OsmnX Python packages to analyze a road network. Once I obtained the simplified graph, I would like to evaluate the performance of the network through the shortest path calculation. In order to obtain a specific index, I need the length of the shortest paths that include selected edges that are shortest paths through the motorway edges.

First I listed the motorway edges which I am interested in (G5 is the corresponding graph of my network):

MW_edges=[(u,v,k,d) for u,v,k,d in G5.edges(keys=True, data=True) if  d['highway']=='motorway']

I used the following function to evaluate the shortest paths in the network for all pairs of nodes:

paths=dict(nx.all_pairs_dijkstra_path(G5, cutoff=None, weight='length'))

How can obtain/extract the demanded shortest paths including motorway edges?

  • I have a similar requirement and will consider either a) split route to include a node or group of nodes that are motorway - ie instead of source->target get routes (source->motorway_node(s), motorway_node(s)->target) - depends on being able to identify appropriate motorway node(s) for each source-> target pair. Or, create custom weight where motorway edges have a lower weight than non-motorway edges (maybe speedlimit/length) so shortest_path source->target would prefer these edges. Then evaluate the true cost by walking the route edge by edge, taking length. Did you get anywhere with this? – user1676300 Apr 10 '20 at 07:26

1 Answers1

0

I do not have access to your graph or a toy example that resembles it, and plus I do not get what it is that you want exactly. Mainly:

  • is the graph weighted or not? The fact that you use dijkstra makes me think that it is, but then the fact that you are working with a road network makes me think that it is not.

  • what is your objective? Do you want, given two nodes (let's say source and target), to get the nodes that make up the shortest path between them? Or do you want all the shortest paths for all node pairs? There are different functions that are optimized for doing each of these things.

I made a toy example where given two nodes, the algorithm returns the nodes that make up the shortest path between them. Finding the edges is then trivial (just pick the nodes in pairs consecutively from the returned list):

import networkx as nx
import matplotlib.pyplot as plt

nodes = [i for i in range(10)]
edges = [(i, i+1) for i in range(len(nodes)-1)] + [(nodes[0], nodes[-1])] + [(nodes[4], nodes[8])]
G = nx.Graph()
G.add_nodes_from(nodes)
G.add_edges_from(edges)

source, target = 4, 9
print("Shortest path between nodes {} and {} = {}"
      .format(source, target, nx.bidirectional_shortest_path(G, source, destination)))

nx.draw_networkx(G)

I left the drawing line to help visualize things better. I also suggest you take a look at the docs here to better understand which algorithm is better for your use case.
If I misunderstood things, please correct me!

neko
  • 379
  • 1
  • 5
  • Thank you for your answer! 1.As you assumed the graph is weighted because of the attribute "length" assigned to each edge. 2. The aim is to get the shortest paths, among all the shortest paths for each pair of nodes, passing through the motorway edges (edges with the attribute "motorway). This is why I calculated all the shortest paths in the graph considering each pair of nodes, and from that I want to get just the shortest paths including motorway edges. Do you think is it possible? – Silvia Ientile Nov 14 '19 at 14:22
  • I have to think a bit for the path thing, but if only the edges with the attribute `motorway` matter, why don't you switch the order of things? First find all the edges with the `motorway` attribute, build a graph only with those nodes/edges, and then study the shortest paths on those. This will at least save you time in the path computation. – neko Nov 14 '19 at 14:32
  • If I consider a graph just contains the motorway edges, I will not take into account all the connections between the places(nodes in the overall graph). In fact, the connections between two nodes often combine motorway and other types of road (primary, secondary, etc.). This means that I am not considering all the places in the interested real-network – Silvia Ientile Nov 14 '19 at 15:51