Converting a directed graph to undirected

705 views
Skip to first unread message

Simon Knight

unread,
Jun 3, 2011, 3:35:39 AM6/3/11
to networkx...@googlegroups.com
Hello,
I am posting here as I am unsure if this is a bug or a known issue.
I am loading graphs from external file formats, which are being read
in as directed graphs.
I only wish to deal with undirected graphs, so if a link is present
from A->B or B->A I would like a link to be present in my undirected
graph.

I have found that the order matters, that networkx only looks at links
in one direction:


>>> G = nx.DiGraph()
>>> G.add_edges_from([(0, 1), (1, 2), (2, 0)])
>>> G.edges()
[(0, 1), (1, 2), (2, 0)]

However we only get two edges when converting to undirected using the
shallow copy method:
>>> nx.Graph(G).edges()
[(0, 1), (1, 2)]

The deep copy method is correct:
>>> G.to_undirected().edges()
[(0, 1), (0, 2), (1, 2)]

________
If I reverse the order of the last edge pair, from (2,0) to (0,2) all is well:

>>> G = nx.DiGraph()
>>> G.add_edges_from([(0, 1), (1, 2), (0, 2)])
>>> G.edges()
[(0, 1), (0, 2), (1, 2)]
>>> nx.Graph(G).edges()
[(0, 1), (0, 2), (1, 2)]
>>> G.to_undirected().edges()
[(0, 1), (0, 2), (1, 2)]

________

What is the best practice to handle this, given that the graphs are
coming from an external file so I am unsure which order the edges will
be specified in?

thanks
Simon

Aric Hagberg

unread,
Jun 3, 2011, 8:18:28 AM6/3/11
to networkx...@googlegroups.com

It's a bug. I opened a ticket at https://networkx.lanl.gov/trac/ticket/564

There should be a simple fix.

Aric

Simon Knight

unread,
Jun 3, 2011, 9:12:36 AM6/3/11
to networkx...@googlegroups.com
Thanks. I wasn't sure if it was something to do with handling of edge
data if link is present both directions.

Cheers
Simon

> --
> You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
> To post to this group, send email to networkx...@googlegroups.com.
> To unsubscribe from this group, send email to networkx-discu...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/networkx-discuss?hl=en.
>
>

Reply all
Reply to author
Forward
0 new messages