joining 2 graphs

3,350 views
Skip to first unread message

irinag

unread,
Jan 20, 2011, 10:11:24 PM1/20/11
to networkx-discuss
Hello,

I want to combine 2 graphs - is there a function that does this? If
yes, how does it treat nodes that appear in both graphs? How does it
draw edges between the 2? Because I need to combine them into 1
connected graph, not simply 2 components.

I'm thinking that what I want is simply an edge between the nodes that
are in common? So for the common nodes I'll have 2 instances with a
bidirectional edge. Or is there a way to combine them so that you just
add the edges from the other graph onto the node that is in common?

So if you have a-b-c and c-d-e: a-b-c-c-d-e or a-b-c-d-e.

And if it's a digraph, can I have a bidirectional edge?

If you have any other good suggestions on how to join them, please let
me know! Maybe there's a common way to do this that I'm not aware of.

Thank you very much!
Irina.

Dan Schult

unread,
Jan 21, 2011, 11:27:50 AM1/21/11
to networkx...@googlegroups.com
The included operators on graphs can be found at:
http://networkx.lanl.gov/reference/algorithms.operators.html
They include things like union() and intersection().

More flexibility is fairly straight forward too, especially if
you combine one of these routines with adding a one or two edges
yourself. Adding a bidirectional edge requires that you add
both directions--but no more difficulty than that.

The package might have exactly what your looking for, but even
if it doesn't it is very likely that you can supplement with
a very simple function that calls package functions and gets done
exactly what you want.

a-b-c and c-d-e to a-b-c-c-d-e:
H=nx.union(G1,G2,rename=('1-','2-'))
elist=[ ('1-'+str(n1),'2-'+str(n1)) for n1 in G1 if n1 in G2]
H.add_edges_from(elist)

a-b-c and c-d-e to a-b-c-d-e:
H=nx.union(G1,G2,rename=('1-','2-'))
nodes_in_both=[n for n in G1 if n in G2]
for n in nodes_in_both:
n1='1-'+str(n)
n2='2-'+str(n)
H.add_edges_from([ (n1,nbr) for nbr in H[n2] ])
H.remove_node(n2)

(These aren't tested, but hopefully show some ideas.)
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...@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.
>

Mattias Östmar

unread,
Jun 19, 2013, 4:34:27 AM6/19/13
to networkx...@googlegroups.com
Hi, 

That code works grea, thanks! However, I am trying to achieve the same for merging four graphml-files instead of two and can´t figure out how to change the code example. My programming skills are unfortunately all to lousy still. I would really appreciate any help! Here's my try so far:

Dan Schult

unread,
Jun 19, 2013, 12:49:53 PM6/19/13
to networkx...@googlegroups.com
It looks like you are trying to do what nx.compose(G,H) does. The combined nodes-set is the union of the node sets and the combined edge set is the union of the edge sets. So 1-2-3 and 3-1 become a triangle. nx.union() and nx.disjoint_union() make sure to keep the nodes separated, renaming if necessary. nx.compose() leaves the nodes the same so the networks overlay each other.

If compose works for you for 2 networks, then apply it repeatedly to get the 4 networks:
G=nx.Graph()
for H in [A,B,C,D]:
G=nx.compose(G,H)

or:
G=nx.compose(A,nx.compose(B,nx.compose(C,D)))
> --
> You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to networkx-discu...@googlegroups.com.
> To post to this group, send email to networkx...@googlegroups.com.
> Visit this group at http://groups.google.com/group/networkx-discuss.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Martin

unread,
Dec 3, 2013, 7:44:27 AM12/3/13
to networkx...@googlegroups.com
Hello,

Have to write it in Python / NetworkX. Anyone has an idea how to do that ?

Create two Erdos-Reni graphs (random graphs) “G” and “H” (different numbers of nodes & probability & color) which will be connected with some links. Then randomly draw a node from “G” and remove it with all edges connected to it. If this node is also connected with other nodes from “H”, remove them with all edges as well….......If "H" node is not connected with “G” nodes STOP      

All the best,

Martin

jyoti pandey

unread,
Dec 6, 2013, 4:53:31 AM12/6/13
to networkx...@googlegroups.com

Dan Schult

unread,
Dec 6, 2013, 2:50:07 PM12/6/13
to networkx...@googlegroups.com
You should look at the graph operators part of NetworkX:
http://networkx.github.io/documentation/latest/reference/algorithms.operators.html

Especially union and disjoint_union could be helpful--though others may be as well.
Dan
Reply all
Reply to author
Forward
0 new messages