使用 Python igraph 库,我有一个表示道路网络的有向图,其中每个顶点(坐标)之间的距离作为权重。顶点对之间很可能存在两条或更多条具有不同权重的边。我需要通过查询顶点 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]
我知道我可以获取边缘的 id,然后获取属性,如下所示,但这似乎只能找到最后添加的例如:
g.get_eid((0, 1)) # returns 0
g.get_eid((1, 2)) # returns 2
因此,1 和 2 之间有两条边,但 get_eid 仅返回一条 - 我需要知道这两条边,然后查询边属性并返回权重,以从图形中选择正确的最小距离,如下所示由距离加权最短路径查询使用。有没有办法用 igraph 做到这一点?
最佳答案
我认为你对 python 不走运。 get_eid
的文档说
Returns the edge ID of an arbitrary edge between vertices v1 andv2
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.
奇怪的是,R 版本的 igraph 确实支持您想要的功能。
函数get.edge.ids
有一个参数multi
,它允许您像这样获取多条边。
无奈之下,我尝试将 multi=True
添加到 python 代码中,但它只是给了我:
'multi' is an invalid keyword argument for this function
关于python - 使用 igraph Python 查找顶点之间所有边的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52707091/