graph union

123 views
Skip to first unread message

Bob

unread,
Nov 3, 2009, 6:50:16 PM11/3/09
to networkx-discuss
I tried the following code

G = nx.complete_graph(n)
H = nx.complete_graph(n)

Z = nx.union(G, H)

I got the following error
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/
site-packages/networkx-1.0rc1-py2.5.egg/networkx/operators.py", line
116, in union
networkx.exception.NetworkXError: node sets of G and H are not
disjoint. Use appropriate rename=('Gprefix','Hprefix')

but I don't understand how to fix it. If you could tell me how that'd
be great.

Thanks,
Bob

Aric Hagberg

unread,
Nov 3, 2009, 7:00:16 PM11/3/09
to networkx...@googlegroups.com
In [1]: import networkx

In [2]: g=networkx.complete_graph(4)

In [3]: h=networkx.complete_graph(4)

In [4]: gh=networkx.union(g,h,rename=('g-','h-'))

In [5]: gh.nodes()
Out[5]: ['h-0', 'h-1', 'h-2', 'h-3', 'g-3', 'g-2', 'g-1', 'g-0']

In [6]: gh=networkx.disjoint_union(g,h)

In [7]: gh.nodes()
Out[7]: [0, 1, 2, 3, 4, 5, 6, 7]

Aric

Bob

unread,
Nov 3, 2009, 7:27:25 PM11/3/09
to networkx-discuss
the problem I have is that, given a number k of complete graphs, i'd
like to put a few edges between them. But once I use union it's
difficult to detect which nodes belong to which cluster. Is it
possible to make new edges between nodes of different graphs or
something similar?

Thanks a lot

On Nov 3, 4:00 pm, Aric Hagberg <ahagb...@gmail.com> wrote:

Dan Schult

unread,
Nov 3, 2009, 11:28:51 PM11/3/09
to networkx...@googlegroups.com
In Aric's example after the union call:

> In [4]: gh=networkx.union(g,h,rename=('g-','h-'))
>
> In [5]: gh.nodes()
> Out[5]: ['h-0', 'h-1', 'h-2', 'h-3', 'g-3', 'g-2', 'g-1', 'g-0']
nodes from the graph h have names starting with h-
and nodes from graph g have names starting with g-

If you have a lot of graphs to union this way, you could
have the nodes be graph_number-node_number
e.g. 12-3 for third node of 12th graph
Is this what you are looking for?
Dan

Bob

unread,
Nov 5, 2009, 4:55:54 PM11/5/09
to networkx-discuss
it works, thanks a lot
Reply all
Reply to author
Forward
0 new messages