Combine multiple graphs into one?

1,689 views
Skip to first unread message

Jeff Hoogland

unread,
Oct 9, 2012, 3:28:01 PM10/9/12
to networkx...@googlegroups.com
If I have multiple graph instances, how can I combine them all into one disjoint graph in network x?

~Jeff

Moritz Emanuel Beber

unread,
Oct 10, 2012, 6:07:45 AM10/10/12
to networkx...@googlegroups.com
Hi Jeff,
That depends a little bit on your node type. Basically, you can create a
new, empty graph instance and then add the edges from all other graphs,
however, they will only be disjoint if the nodes in your graphs are
different. For example, if your nodes are given by integers and in your
graphs the nodes range from 1 to 400, you can simply add a fixed value
to each node so that none two integers are contained within each graph.

Hope that helps,
Moritz

Dan Schult

unread,
Oct 10, 2012, 7:35:05 AM10/10/12
to networkx...@googlegroups.com
Did you try: nx.disjoint_union_all() ?
http://networkx.lanl.gov/reference/generated/
networkx.algorithms.operators.all.disjoint_union_all.html

See also:
disjoint_union
http://networkx.lanl.gov/reference/generated/
networkx.algorithms.operators.binary.disjoint_union.html

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

Jeff Hoogland

unread,
Oct 10, 2012, 9:57:51 AM10/10/12
to networkx...@googlegroups.com
Union does pretty much exactly what I am looking for. Just one question though, disjoint_union says:

"The nodes of G are relabeled 0 to len(G)-1, and the nodes of H are relabeled len(G) to len(G)+len(H)-1"

Is there any way to have this function preserve the names of my nodes in G and H since I know none of them have duplicated name values?

Dan Schult

unread,
Oct 10, 2012, 10:38:25 AM10/10/12
to networkx...@googlegroups.com
If they are distinct names anyway, you can use nx.union() or nx.union_all()
I think the *union_all functions are in the latest version but may not be in previous versions.
Dan
> --
> You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/networkx-discuss/-/AoUe_hF8ykoJ.
> To post to this group, send email to networkx...@googlegroups.com.
> To unsubscribe from this group, send email to networkx-discu...@googlegroups.com.

Jeff Hoogland

unread,
Oct 10, 2012, 12:17:23 PM10/10/12
to networkx...@googlegroups.com
Thanks again Dan - nx.union() is what I want.
Reply all
Reply to author
Forward
0 new messages