简体   繁体   English

igraph:基于无向图中源/目标的选择边中的意外行为

[英]igraph: unexpected behavior in select edge based on source/target in an undirected graph

I am using igraph in python. 我在python中使用igraph。 I want to find an edge in an undirected graph using source and target vertex. 我想使用源顶点和目标顶点在无向图中找到一条边。 I am using the following code: 我正在使用以下代码:

g = Graph() # or g = Graph(directed=True)
g.add_vertices(4)
g.add_edges([(0,1),(1,2),(2,3)])
print len(g.es.select(_source=0, _target=1))
print len(g.es.select(_source=1, _target=0))

In case of directed graph, it works as I expected and output is 1 and 0. But in case of undirected graph, I expected that both print statements will return 1, but instead it is same as previous case. 在有向图的情况下,它可以按我的预期工作,并且输出为1和0。但是在无向图的情况下,我希望两个print语句都将返回1,但与以前的情况相同。

What is the correct way of finding an edge between source and target (that works both for directed and undirected graph)? 在源和目标之间找到一条边的正确方法是什么(对有向图和无向图都适用)?

The canonical way to get an edge from source and target vertices is to use the get_eid method. 从源顶点和目标顶点获取边的规范方法是使用get_eid方法。

get_eid(v1, v2, directed=True, error=True)

Directed is ignored for undirected graphs. 对于无向图,将忽略有向图。 The error parameter controls whether an exception is raised is the edge does not exists. 错误参数控制是否在不存在边缘的情况下引发异常。 If error=False it returns -1. 如果error=False则返回-1。

g = igraph.Graph(directed=False)
g.add_vertices(4)
g.add_edges([(0,1),(1,2),(2,3)])
print 'Edge id: {0}'.format(g.get_eid(1,0))
print 'Edge id: {0}'.format(g.get_eid(0,1))

Output: 输出:

Edge id: 0
Edge id: 0

The full documentation is here. 完整的文档在这里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM