2
votes

With the Python igraph library, I have a directed graph representing a road network, with distance between each vertex (coordinates) as the weight. It is quite possible that there can be two or more edges between a vertex pair, with different weights. I need to retrieve these weights from the graph by querying the vertex IDs e.g. an example graph:

import igraph as ig

g = ig.Graph(directed=True)

g.add_vertices(4)

edges = [(0, 1), (1, 2), (1, 2), (2, 3)]
g.add_edges(edges)

g.es[:]["dist"] = [1, 2, 3, 4]

I know I can get the id of an edge, and then the attributes as follows, but this only seems to find the last added e.g:

g.get_eid((0, 1)) # returns 0
g.get_eid((1, 2)) # returns 2

So, there are two edges between 1 and 2, but only one is returned by get_eid - I need to know both to then query the edge attributes and return the weightings, to select the correct minimum distances from the graph, as were used by a distance-weighted shortest path query. Is there a way to do this with igraph?

2

2 Answers

1
votes

I think that you are out of luck with python. The documentation for get_eid says

Returns the edge ID of an arbitrary edge between vertices v1 andv2

The documentation for get_eids says explicitly:

The method does not consider multiple edges; if there are multiple edges between a pair of vertices, only the ID of one of the edges is returned.

Oddly, the R version of igraph does support the functionality that you want. The function get.edge.ids has an argument multi that allows you to get multiple edges like this.

In desperation, I tried adding multi=True to the python code, but it simply gave me:

'multi' is an invalid keyword argument for this function

0
votes

further to @G5W's answer, the get_eid function seems to be giving not an "arbitrary" edge between two nodes but, in case of multiple nodes, the one with the highest id. consider:

g = ig.Graph()
g.add_vertices(['A', 'B', 'C'])
g.add_edges([('A', 'B'), ('A', 'B'), ('A', 'B'), ('B', 'C'), ('B', 'C')])
g.get_eid(0, 1) #always returns 2
g.get_eid(1, 2) #always returns 4