【问题标题】:In python-igraph, find the number and mode of edges between two vertices在python-igraph中,查找两个顶点之间的边数和模式
【发布时间】:2022-11-14 11:47:36
【问题描述】:

在一个定向python-igraph,我可以找到两个顶点之间的路径如下:

g=ig.Graph(directed=True)
g.add_vertices(range(4))
g.add_edges([(0,1),(0,2),(1,3)])

paths=g.get_all_shortest_paths(3,2,mode='all')
paths

[[3, 1, 0, 2]]

有没有一种简单的方法来获取沿路径的边缘的模式(进或出)?

我尝试查看诱导子图,并使用“输入”和“输出”模式而不是“全部”。我可以手动走树,但我正在寻找更紧凑和pythonic的东西。

理想情况下,将有一种方法可以为上述场景返回以下内容:

[['out','out','in']]

【问题讨论】:

    标签: python igraph directed-graph


    【解决方案1】:

    像这样的东西应该可以解决问题:

    def consecutive_pairs(items):
        return zip(items, items[1:])
    
    
    def classify_edges_in_path(path, graph):
        return [
            "in" if graph.get_eid(u, v, error=False) >= 0 else "out"
            for u, v in consecutive_pairs(path)
        ]
    

    这里的诀窍是,如果 u-v 边不存在,graph.get_eid(u, v, error=False) 将返回 -1。由于路径本身存在,因此您可以知道它一定是路径中的 v-u 边。

    consecutive_pairs() 仅用于可读性;如果你愿意,你可以内联它。

    【讨论】:

      猜你喜欢
      • 2012-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-27
      • 2019-12-07
      • 1970-01-01
      • 1970-01-01
      • 2017-07-03
      相关资源
      最近更新 更多