Re: [networkx-discuss] Nested Graphs

842 views
Skip to first unread message

Dan Schult

unread,
Nov 23, 2012, 11:28:14 PM11/23/12
to networkx...@googlegroups.com
> import networkx as nx
>
> sub1 = nx.Graph()
> sub1.add_node('node1')
> sub2 = nx.Graph()
> sub2.add_edge('node1')
> mainGraph = nx.Graph()
> mainGraph.add_nodes_from([sub1,sub2])
>
>
> How would I go around creating an edge between 'node1' in sub1 and
> 'node1' in sub2?

You have created three graphs. They don't know anything about the
structure of each other.
They only know about nodes and edges (and attributes) in their own
structures. So in that
sense the "nested graphs" created this way are just nodes in the
mainGraph graph.

Also, your code won't work for the sub.add_edge('node1') line...
add_edge() takes two
arguments (the nodes you want to connect with an edge).

The term "nested graphs" isn't uniquely well established. So you'll
have to build your own
form depending on what you want to do. Most likely a single graph
will work to hold all the
information with attributes identifying subsets that you keep track
of. If/when that gets too
big or slow then you can restructure with smaller graphs.

Dan

Jose Juan Tapia

unread,
Nov 24, 2012, 12:44:19 PM11/24/12
to networkx-discuss
Sorry, that was a mistake. I meant

sub2.add_node('node1')

So that I have

sub1.add_node('node1')
sub2.add_node('node1')

And I want to add an edge between node1 in sub1 and sub2.

In the end what I want is something similar to what you find in this
graphml snippet that I found in another thread (I know exporting
nested graphs into graphML isn't implemented right now. I'm just using
graphML to represent this issue).

<graph>
<node id="n0">
<graph>
<node id="n0.n0" />
</graph>
</node>
<node id="n1">
<graph>
<node id="n1.n0" />
</graph>
</node>
<edge source="n0.n0" target="n1.n0" />
</graph>


I guess I could try to represent this structure with attributes. The
point is that eventually I will need to export this information into a
data file (say GEXF ) and this format should be able to explicitly
retain all the subgraph information, so I don't know if representing
with attributes is the better option. If it is, how would I
restructure it into subgraphs. The general question being, how do I
build nested graphs in the first place.

Thank you. Yours,

Jose Juan

Dan Schult

unread,
Nov 24, 2012, 1:15:55 PM11/24/12
to networkx...@googlegroups.com
Your example suggests one way to do it.
Use node names that reflect the subgraph and the node within that graph:

g=nx.Graph()
g.add_node('n0.n0')
g.add_node('n1.n0')
g.add_edge('n0.n0','n1.n0')

Until you really specify why the nested structure is needed, it is
hard to
tell what type of nested structure is best.
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.
>

Jose Juan Tapia

unread,
Nov 24, 2012, 8:56:19 PM11/24/12
to networkx-discuss
The idea is that the nodes themselves are biomolecules, and the
subnodes represent the docking points that the molecules can use to
dock to other molecules. I'm defining a graph representation for a
language we use to define those things.

For example, in the language we would have

molecule1: A(b) #where A is a molecule name, and 'b' a docking site
within that molecule that is uses to connect to the molecule 'B'
molecule2: B(a) #where B is a molecule name, and 'a' the docking site
it used to connect to molecule 'A'

So together they form the complex
A(b!1).B(a!1) #where the syntax '!1' represents the bond between the
two, which would be the edge in the graph.


This example can be extended to complexes with multiple molecules and
multiple dockings sites. (such that in a graph as I visualize it the
molecules would be big nodes, the components should be small nodes
within that big node, and the connections should be lines between the
small nodes). Which would be the best way to represent such a
structure in networkx?

Daniel Schult

unread,
Nov 24, 2012, 10:52:53 PM11/24/12
to networkx...@googlegroups.com
OK...  that makes good sense.  

Now what do you want NetworkX to do with that data?
Are you looking to run algorithms?  fast edge lookup?  
visualization?  Flexible exploration?

If performance isn't an issue, you could store the molecules as nodes with edges
representing the bonds between molecules.  Then edge attributes could describe
the docking points on each molecule.  This could be good for a drill-down type of 
exploration.

Another method would have each docking site be a node (e.g.  A_b) with edges
connecting docking sites that create a bond.  Then the node names would include
both molecule and docking site information.  This might be better for exploration of
all possible bonds.

You could create one graph for the connections between molecules--nodes are
molecules and edges are bonds.  A second graph contains the docking site 
information as above, and separate dicts connect molecules with docking sites
and vice versa.  This might be best for exploration of relationships between molecules
and between docking site, but a little slower at moving from molecules to docking sites.

Lots of possibilities. :}
Dan



To post to this group, send email to networkx...@googlegroups.com.
To unsubscribe from this group, send email to networkx-discu...@googlegroups.com.

Jose Juan Tapia

unread,
Nov 25, 2012, 12:18:13 AM11/25/12
to networkx-discuss
The main task I have to do is visualization. (we have a separate
software that does most of the analysis stuff, but we are trying to
come up with smart ways to visualize this stuff when it goes big. This
is one of such attempts) I guess I could store the docking point as
attributes, however my visualization wise my spec still requires me to
show the docking points as small circles inside a bigger container. I
guess this doesn't explicitly require me to use nested graphs, however
docking points from a same molecule should still visually clustered
within a visually enclosing entity. I'm not sure what would be the
best way to do this using networkx.

Jose Juan Tapia

unread,
Nov 26, 2012, 11:44:29 AM11/26/12
to networkx-discuss
I guess another way of asking the same question would be:

Do networkx support nested graphs? If so, what would be the code
instructions I need to use in networkx such that when I visualize the
graph I get something like this:
http://graphml.graphdrawing.org/primer/nested.png

Aric Hagberg

unread,
Nov 26, 2012, 11:51:51 AM11/26/12
to networkx...@googlegroups.com
On Mon, Nov 26, 2012 at 9:44 AM, Jose Juan Tapia <jjt...@gmail.com> wrote:
> I guess another way of asking the same question would be:
>
> Do networkx support nested graphs? If so, what would be the code
> instructions I need to use in networkx such that when I visualize the
> graph I get something like this:
> http://graphml.graphdrawing.org/primer/nested.png

The GraphML writer in NetworkX does not support the GraphML concept of
nested graphs (or mixed graphs, hypergraphs, or ports). See
http://networkx.lanl.gov/reference/generated/networkx.readwrite.graphml.write_graphml.html#networkx.readwrite.graphml.write_graphml

Those features could certainly be added if someone is willing to write the code.

Aric

Jose Juan Tapia

unread,
Nov 26, 2012, 3:09:59 PM11/26/12
to networkx-discuss
Sorry, I sent my last reply only to Aric

It doesn't have to be in graphML, I could write my output in any of
the
other formats supported by networkx. I'm just wondering if nested
graphs are
supported themselves in networkx?


To which his reply was

>> The data structure is designed for "graphs". But like Dan suggests
>> there are probably many ways to represented some sort of nested
>> structure depending on what you want to do with it (algorithms,
>> drawing, etc).


My intended output for now is just drawing. I only want to visualize
the data in nested nodes as shown in the image I linked above. (http://
graphml.graphdrawing.org/primer/nested.png Again, I don't care about
GraphML for now, this is just an example) Is this supported?


Sorry for being so insistent, and thanks for all the help offered so
far.

On Nov 26, 11:52 am, Aric Hagberg <aric.hagb...@gmail.com> wrote:
> On Mon, Nov 26, 2012 at 9:44 AM, Jose Juan Tapia <jjta...@gmail.com> wrote:
>
> > I guess another way of asking the same question would be:
>
> > Do networkx support nested graphs? If so, what would be the code
> > instructions I need to use in networkx such that when I visualize the
> > graph I get something like this:
> >http://graphml.graphdrawing.org/primer/nested.png
>
> The GraphML writer in NetworkX does not support the GraphML concept of
> nested graphs (or mixed graphs, hypergraphs, or ports).  Seehttp://networkx.lanl.gov/reference/generated/networkx.readwrite.graph...

Dan Schult

unread,
Nov 26, 2012, 3:42:55 PM11/26/12
to networkx...@googlegroups.com
I think the easy answer is that there is no explicit support of nested graphs in NetworkX.

But there are quite a few ways to provide that support with a little bit of code. Nodes can be anything, including Graphs. In that sense, nested graphs are allowed. But our drawing routines don't treat nested graphs as such.

Your example drawing doesn't show any subgraphs connecting to other subgraphs which differs from your description. That would make it easier.

To do what you want with NetworkX you would probably need to write some routines that plot with matplotlib based on information from networkx. Matpotlib is pretty flexible: http://matplotlib.org/gallery.html

Dan

Aric Hagberg

unread,
Nov 26, 2012, 3:44:12 PM11/26/12
to networkx...@googlegroups.com
On Mon, Nov 26, 2012 at 1:09 PM, Jose Juan Tapia <jjt...@gmail.com> wrote:
> Sorry, I sent my last reply only to Aric
>
> It doesn't have to be in graphML, I could write my output in any of
> the
> other formats supported by networkx. I'm just wondering if nested
> graphs are
> supported themselves in networkx?
>
>
> To which his reply was
>
>>> The data structure is designed for "graphs". But like Dan suggests
>>> there are probably many ways to represented some sort of nested
>>> structure depending on what you want to do with it (algorithms,
>>> drawing, etc).
>
>
> My intended output for now is just drawing. I only want to visualize
> the data in nested nodes as shown in the image I linked above. (http://
> graphml.graphdrawing.org/primer/nested.png Again, I don't care about
> GraphML for now, this is just an example) Is this supported?
>
>
> Sorry for being so insistent, and thanks for all the help offered so
> far.
>

If all you want to do is draw you could use Graphviz. Example at
http://www.graphviz.org/Gallery/directed/cluster.html

PyGraphviz provides a Python interface to Graphviz that will read and
write that file.

Aric

Jose Juan Tapia

unread,
Nov 26, 2012, 5:31:15 PM11/26/12
to networkx-discuss
Thanks. Both answers will help a lot.

On Nov 26, 3:44 pm, Aric Hagberg <aric.hagb...@gmail.com> wrote:
> On Mon, Nov 26, 2012 at 1:09 PM, Jose Juan Tapia <jjta...@gmail.com> wrote:
>
>
>
>
>
>
>
>
>
> > Sorry, I sent my last reply only to Aric
>
> > It doesn't have to be in graphML, I could write my output in any of
> > the
> > other formats supported by networkx. I'm just wondering if nested
> > graphs are
> > supported themselves in networkx?
>
> > To which his reply was
>
> >>> The data structure is designed for "graphs".   But like Dan suggests
> >>> there are probably many ways to represented some sort of nested
> >>> structure depending on what you want to do with it (algorithms,
> >>> drawing, etc).
>
> > My intended output for now is just drawing. I only want to visualize
> > the data in nested nodes as shown in the image I linked above. (http://
> > graphml.graphdrawing.org/primer/nested.png Again, I don't care about
> > GraphML for now, this is just an example) Is this supported?
>
> > Sorry for being so insistent, and thanks for all the help offered so
> > far.
>
> If all you want to do is draw you could use Graphviz. Example athttp://www.graphviz.org/Gallery/directed/cluster.html
Reply all
Reply to author
Forward
0 new messages