【问题标题】:Python igraph cant find edges in an undirected graphPython igraph 无法在无向图中找到边
【发布时间】:2019-11-23 03:23:28
【问题描述】:

我在 Python 3 中有这个简单的代码,它使用 igraph 返回我添加到无向图中的边。

from igraph import *
g = Graph()
g.add_vertices(3)
g.add_edges([(0,1), (1,2)])
# display(plot(g))
edge=g.es.find(_source=1, _target=0)

但是当我使用函数 graph.es.find() 时,它给了我一个错误,而不是返回边缘。它说“没有这样的优势”。

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-14-4182287b1986> in <module>
      4 g.add_edges([(0,1), (1,2)])
      5 # display(plot(g))
----> 6 edge=g.es.find(_source=1, _target=0)

E:\Softwares\Anaconda\lib\site-packages\igraph\__init__.py in find(self, *args, **kwds)
   3615         if es:
   3616             return es[0]
-> 3617         raise ValueError("no such edge")
   3618 
   3619     def select(self, *args, **kwds):

ValueError: no such edge

但是,如果我按照输入的顺序设置源和目标,它确实会返回边缘。在这种情况下,源 = 0,目标 = 1
我的猜测是它并不是一个真正的无向图。

我的问题是,即使我在 g.es.find() 函数中切换源节点和目标节点,我如何获得一个返回边的真正无向图,因为它应该是无向图?

【问题讨论】:

    标签: graph igraph python-3.7 undirected-graph


    【解决方案1】:

    使用它来获得优势:

    from igraph import *
    g = Graph()
    g.add_vertices(3)
    g.add_edges([(0,1), (1,2)])
    
    #get the ID
    g.get_eid(0,1)
    0
    
    #get the edge list
    g.get_edgelist()
    [(0, 1), (1, 2)]
    
    #get the first edge from the edge list
    g.get_edgelist()[0]
    (0, 1)
    

    最后,要验证它不是定向的,请使用g.is_directed()

    【讨论】:

    • 为什么会这样呢?你不认为这是违反直觉的吗?
    • 没有。文档介绍了这些方法以获得优势。你用的功能我没见过
    • 这个函数做其他工作。 g.es.find(weight_gt=5) 这将找到分配权重大于 5 的边。阅读文档。考虑接受并支持我的回答
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    • 2012-04-05
    • 2015-04-10
    • 1970-01-01
    相关资源
    最近更新 更多