manifold
manifold

Reputation: 233

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.

For example L.is_mutual() returns list of booleans, every boolean corresponding to an edge. True is returned for a given edge a --> b if there exists another edge b -->a in the original graph.

How to use L.is_mutual() for removing mutual edges?

possible solution (slow):

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

Upvotes: 0

Views: 314

Answers (1)

Tamás
Tamás

Reputation: 48051

Does l contain the edge list of your graph? 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). Since delete_edges can work with edge indices as well, it is faster to say:

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()

Upvotes: 1

Related Questions