简体   繁体   English

使用python-igraph 0.7删除相互边缘的智能方法

[英]Smart way to remove mutual edges with python-igraph 0.7

Let L be a directed graph, I have to remove the mutual edges of L. Remember that two edges are mutual if they are of the type a--->b and b---->a. 令L为有图,我必须删除L的共同边。请记住,如果两个边的类型为a ---> b和b ----> a,则它们是共同的。

For example L.is_mutual() returns list of booleans, every boolean corresponding to an edge. 例如L.is_mutual()返回布尔值列表,每个布尔值对应于一个边。 True is returned for a given edge a --> b if there exists another edge b -->a in the original graph. 如果原始图形中存在另一个边b-> a,则对于给定的边a-> b返回True。

How to use L.is_mutual() for removing mutual edges? 如何使用L.is_mutual()删除共同的边缘?

possible solution (slow): 可能的解决方案(缓慢):

 L.delete_edges([l[i] for i in range(len(l)) if mut[i]==True])

Does l contain the edge list of your graph? 是否l包含您的图形的边列表? In that case, your solution is slow because for every edge to be deleted, igraph has to look up its identifier from the endpoints (since you give the endpoints to delete_edges and not the edge index). 在那种情况下,您的解决方案很慢,因为对于每个要删除的边缘,igraph必须从端点中查找其标识符(因为您将端点赋予delete_edges而不是边缘索引)。 Since delete_edges can work with edge indices as well, it is faster to say: 由于delete_edges也可以使用边索引,所以说起来更快:

L.delete_edges(index for index, mut in enumerate(L.is_mutual()) if mut)

Another (somewhat slower but more concise) variant: 另一个(稍微慢一些但更简洁的)变体:

L.es.select(_is_mutual=True).delete()

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

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