igraph to_undirected() method analogy in NetworkX

548 views Asked by At

In igraph I found two different methods for converting graph to undirected graph:
The first is to_undirected which simply 'Converts a directed graph to undirected.' and the second is as_undirected which invokes to_undirected on the copy.

In NetworkX I found only single method to_undirected which simply create undirected deep copy of the graph. The problem is that I really can't found method similar to the first one in igraph. Is there any solution to transform graph without creating copy using NetworkX?

1

There are 1 answers

0
jme On

As you state, networkx.to_undirected creates a deep copy of the graph. This means that it copies the edge, node, and graph attribute dictionaries. To create an undirected shallow copy of a digraph g, one can write:

g = networkx.Graph(g)

This is still a copy, but a shallow one in which the attribute dictionaries are not deep copied.

It does not appear that a method exists to convert a DiGraph to a Graph in-place. Doing as much would mean changing the type of the object -- it's likely cleaner to simply do a shallow copy, and not that much more expensive.