Hi, I’m using python igraph to write an algorithm (exact algorithm not relevant), and part of it calls for reversing the direction of all edges in a directed graph.

I can’t see a simple way to do this. I would guess that it’s not an uncommon thing people might want to do, so perhaps there is a simple way of doing it which I have just missed. I’ve searched the documentation and the forum for “transpose”, “reverse edges” and just “reverse”, but nothing seems relevant.

At the moment, I’m getting the weighted adjacency matrix, transposing it, and creating a new graph from said transpose, and copying over all the vertex attributes. Since weight is the only edge attribute I’m dealing with, this works fine, but I suspect there’s a more efficient and neater way of doing it.

Does anyone know if there’s a simple built-in function for doing it, or alternatively a more efficient way of doing it without a self-contained function?

The reason is that (almost?) any operation that takes edge directions into account has a mode parameter that can be set to either OUT or IN, with IN indicating that the graph should be interpreted in the reverse direction.

If you feel that you still need this feature, please do comment on this feature request and explain why using the mode parameter is insufficient or inconvenient for you. Input from users will be helpful in arriving to a decision about whether (and how) to implement this.

Using the adjacency matrix uses a full matrix representation, unless you specifically use get_adjacency_sparse. Using the sparse representation will obviously be more efficient. Alternatively, you could also use the edge list directly, as follows

el = G.get_edgelist()
el_t = (tuple(reversed(e)) for e in el)
G_t = ig.Graph(el_t, directed=True)

You are constructing a completely new graph in this way. So, there simply aren’t attributes (yet). But, the edge order is identical, so you can easily copy them over: e.g G_t.es['weight'] = G.es['weight'].

Regardless, it would be convenient if this was directly implemented.