Subgraph for node

148 views
Skip to first unread message

Casey

unread,
Sep 13, 2010, 7:18:22 PM9/13/10
to pygraphviz-discuss
Is there a way to query the subgraph (or subgraph name) for a node?
For example, given the following:

>>> from pygraphviz import AGraph
>>> G = AGraph(directed=True, strict=False, compound=True)
>>> S = G.add_subgraph(name='cluster_a')
>>> S.add_node('a')
>>> G
digraph {
graph [compound=True];
subgraph cluster_a {
a;
}
}
>>> len(G.subgraphs())
1

Now, I'd like to use a method to find out which immediate subgraph
node 'a' is in. Something like:

>>> G.subgraph_containing('a')
'cluster_a'

OR

>>> S = G.subgraph_containing('a')
>>> S
digraph {
graph [compound=True];
subgraph cluster_a {
a;
}
}
>>> len(G.subgraphs())
1

Is there something like this?

Thanks,
Casey

Aric Hagberg

unread,
Sep 13, 2010, 8:36:43 PM9/13/10
to pygraphvi...@googlegroups.com

I just browsed through the libcgraph graphviz code and I don't see
that there is any obvious API for that. It might be possible to
inspect the graphviz node data structure somehow to get that.

At any rate there is no PyGraphviz function to do that.

You could consider creating a Python dictionary that maps nodes to a
list of subgraphs.
When loading a graph from a file you'd need to recursively traverse
all of the subgraphs to build that though...

Aric

Casey Deccio

unread,
Sep 14, 2010, 3:46:26 PM9/14/10
to pygraphvi...@googlegroups.com
On Mon, Sep 13, 2010 at 5:36 PM, Aric Hagberg <ahag...@gmail.com> wrote:
> You could consider creating a Python dictionary that maps nodes to a
> list of subgraphs.
> When loading a graph from a file you'd need to recursively traverse
> all of the subgraphs to build that though...
>

That should probably work well enough for what I need. For what I'm
doing, I don't even need to import from a file, so it should be
relatively simple.

Casey

Reply all
Reply to author
Forward
0 new messages