简体   繁体   English

使用 igraph Python 查找顶点之间所有边的 ID

[英]Find IDs of all edges between vertexes with igraph Python

With the Python igraph library, I have a directed graph representing a road network, with distance between each vertex (coordinates) as the weight.使用 Python igraph 库,我有一个表示道路网络的有向图,每个顶点(坐标)之间的距离作为权重。 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 eg an example graph:我需要通过查询顶点 ID(例如示例图)从图中检索这些权重:

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 eg:我知道我可以获得边缘的 id,然后属性如下,但这似乎只能找到最后添加的,例如:

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.因此,在 1 和 2 之间有两条边,但 get_eid 只返回一条边——我需要知道这两条边,然后查询边属性并返回权重,以从图中选择正确的最小距离,正如 a距离加权最短路径查询。 Is there a way to do this with igraph?有没有办法用 igraph 做到这一点?

I think that you are out of luck with python.我认为你对 python 不走运。 The documentation for get_eid says get_eid的文档说

Returns the edge ID of an arbitrary edge between vertices v1 andv2返回顶点 v1 和 v2 之间任意边的边 ID

The documentation for get_eids says explicitly: get_eids的文档明确指出:

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.如果一对顶点之间有多条边,则只返回其中一条边的 ID。

Oddly, the R version of igraph does support the functionality that you want.奇怪的是,igraph 的 R 版本确实支持您想要的功能。 The function get.edge.ids has an argument multi that allows you to get multiple edges like this.函数get.edge.ids有一个参数multi ,它允许您像这样获得多个边。

In desperation, I tried adding multi=True to the python code, but it simply gave me:无奈之下,我尝试将multi=True添加到 python 代码中,但它只是给了我:

'multi' is an invalid keyword argument for this function “multi”是此函数的无效关键字参数

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.对于@G5W 的回答, get_eid函数似乎不是在两个节点之间给出“任意”边,而是在多个节点的情况下给出具有最高 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

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

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