Graph.copy() and deepcopy

1,799 views
Skip to first unread message

nernst

unread,
Nov 10, 2009, 11:45:42 AM11/10/09
to networkx-discuss
Hi, I found the docs a little perplexing on the subject of graph
copying.

The docs say:
Graph.copy()
"This makes a complete copy of the graph including all of the node or
edge attributes."

In the source the following is done:
return deepcopy(self)

Seems like it is doing what I wanted, but this didn't in fact return a
deepcopy. I have a Digraph with user objects on the edges and user
objects as nodes, so I ended up with copy by reference.

When I followed the forum advice, and did copy.deepcopy(G), I got the
desired result.

Perhaps the docs could reflect this fact. In particular the words
"complete copy" are confusing, it should probably reflect shallow or
deep, as desired.

Aric Hagberg

unread,
Nov 10, 2009, 12:21:28 PM11/10/09
to networkx...@googlegroups.com
On Tue, Nov 10, 2009 at 9:45 AM, nernst <neil....@gmail.com> wrote:
>
> Hi, I found the docs a little perplexing on the subject of graph
> copying.
>
> The docs say:
> Graph.copy()
> "This makes a complete copy of the graph including all of the node or
> edge attributes."
>
> In the source the following is done:
> return deepcopy(self)
>
> Seems like it is doing what I wanted, but this didn't in fact return a
> deepcopy. I have a Digraph with user objects on the edges and user
> objects as nodes, so I ended up with copy by reference.
> When I followed the forum advice, and did copy.deepcopy(G), I got the
> desired result.

The intent is for Graph.copy() to make a deepcopy and as you see we
call that explicitly. I don't understand why you got something
different - but maybe I am missing something about deepcopy?
Can you provide a simple example?

>
> Perhaps the docs could reflect this fact. In particular the words
> "complete copy" are confusing, it should probably reflect shallow or
> deep, as desired.

I'll add another note to the documentation.

Aric

Dan Schult

unread,
Nov 10, 2009, 12:26:51 PM11/10/09
to networkx...@googlegroups.com
Something is strange there....
If the source code uses deepcopy and you use deepcopy they
should return the same thing. Can you give a short
example that shows the shallow copy? The docs
should probably use the word "deep" instead of
"complete", but if it is actually shallow, that
is a bug. Is it possible that you are running a
different version than the source code you are
looking at? The shallow/deep choice for copy()
has changed over the versions.
Dan


This code seems to work:

class node(dict):
def __hash__(self):
return len(self)

class edge(dict):
pass

>>> G=nx.Graph()
>>> n1=node(size=1)
>>> n2=node(size=2)
>>> G.add_edge(n1,n2,edge(weight=4))
>>> H=G.copy()
>>> H[n1][n2]['color']='red'
>>> for n in H: n[2]=3
...
>>> H.adj
{{2: 3, 'size': 2}: {{2: 3, 'size': 3}: {'color': 'red', 'weight': 4}},
{2: 3, 'size': 3}: {{2: 3, 'size': 2}: {'color': 'red', 'weight': 4}}}

>>> G.adj
{{'size': 2}: {{'size': 3}: {'weight': 4}},
{'size': 3}: {{'size': 2}: {'weight': 4}}}
Reply all
Reply to author
Forward
0 new messages