【问题标题】:Is there an igraph function in Python for returning the indexes of shortest_path resultsPython中是否有用于返回shortest_path结果索引的igraph函数
【发布时间】:2022-01-14 18:36:26
【问题描述】:

我有一个无向图,我知道它对于一组特定的点(例如 105 和 149)有一条有效的最短路径。

我不确定如何解释结果以获取 shortest_paths 函数遍历的节点的索引。

如果从 105 开始的路径经过:117、123、29、35、56、78 并在 149 终止。如何检索索引数组 [117、123、29、35、56、78]?

我的代码是这样的:

path = G.shortest_paths(source=[G.vs[105]], target=[G.vs[149]], mode='all')

我的path 对象返回一个带有[[6]] 之类的数组。

【问题讨论】:

  • 您可以在 TowardsDataScience tutorial 上阅读有关如何使用最短路径的更多信息
  • 谢谢,也许只是我,但我误解了shortest_pathsget_shortest_pathsget_all_shortest_paths之间的区别
  • 在 igraph 的某些未来版本中,shortest_paths 可能会重命名为更直观的名称,例如 distances,从而消除这种混淆。总是欢迎这样的反馈,我鼓励您在igraph.discourse.group 上发布您学习 igraph 的经验
  • contribute to the project有很多方法,提供这样的反馈是一种方法。
  • 我认为我们应该从networkx 包中学到很多东西,因为它的文档非常好。他们以非常相似的方式实现了最短路径:nx.shortest_path(G, source=0, target=4).

标签: python igraph shortest-path


【解决方案1】:

看起来igraph.shortest_paths 不是为这种用途而设计的。尝试 igraph.get_all_shortest_paths 改为:

import igraph as ig
g = ig.Graph(n=5, edges=[(0,1), (1,4), (2,3), (2,4), (2,5) , (3,5), (4,6), (6, 0)], directed=False)
path = g.get_all_shortest_paths(0, to=2)
>>> path
[[0, 1, 4, 2], [0, 6, 4, 2]]
>>> [p[1:-1] for p in path]
[[1, 4], [6, 4]]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-09
    • 2019-08-18
    • 2011-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多