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

标签 python igraph

使用 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/

相关文章:

python - 如何用堆叠在 numpy 数组中的一组图像制作电影?

python - plotly .ly。对多个绘图使用 slider 控件

python - 从方法中重新引发异常作为参数

python-2.7 - 保留原始权重的加权双峰二分图投影

r - 转移矩阵的马尔可夫图

python - 在sklearn中将文本列转换为数字

python - 在python中同时读取两个列表中的输入

python - igraph python 删除边框

r - 用R计算一个顶点(节点)的局部聚类系数(手工)

r - 如何根据 igraph layout_in_circle 中的度数对顶点进行排序