[NetworkX-discuss] Duplicate Nodes?

1,832 views
Skip to first unread message

Chris S

unread,
Jun 1, 2006, 7:15:03 PM6/1/06
to networkx...@lists.sourceforge.net
Is it possible to represent an abstract syntax tree with networkx, in
the sense that you can have two or more distinct nodes sharing a
hashable value? For instance, consider a=1 and b=1. I want to have two
nodes with the value '=', each with links to 'a', 'b' and '1' nodes
respectively, not a single '=' node with all the other nodes linked to
it.

Regards,
Chris


_______________________________________________
NetworkX-discuss mailing list
NetworkX...@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/networkx-discuss

Aric Hagberg

unread,
Jun 2, 2006, 12:08:28 AM6/2/06
to Chris S, networkx...@lists.sourceforge.net
Chris,

The ids that are given to nodes must be unique. But if you want
you can always have a symbol table to assign labels to the ids.
For example:

>>> import networkx as NX
>>> import pylab as P
>>> G=NX.Graph()
>>> label={}
>>> label[1]="a"
>>> label[2]="="
>>> label[3]="1"
>>> G.add_edge(1,2)
>>> G.add_edge(3,2)
>>> label[4]="b"
>>> label[5]="="
>>> label[6]="1"
>>> G.add_edge(4,5)
>>> G.add_edge(6,5)
>>> print [label[n] for n in G]
>>> for (u,v) in G.edges():
>>> print (label[u],label[v])
>>> NX.draw(G,labels=label)
>>> P.show()

Aric

On Thu, Jun 01, 2006 at 07:15:03PM -0400, Chris S wrote:
> Is it possible to represent an abstract syntax tree with networkx, in
> the sense that you can have two or more distinct nodes sharing a
> hashable value? For instance, consider a=1 and b=1. I want to have two
> nodes with the value '=', each with links to 'a', 'b' and '1' nodes
> respectively, not a single '=' node with all the other nodes linked to
> it.

_______________________________________________

Chris S

unread,
Jun 2, 2006, 10:11:55 AM6/2/06
to networkx...@lists.sourceforge.net
On 6/2/06, Aric Hagberg <hag...@lanl.gov> wrote:
> Chris,
>
> The ids that are given to nodes must be unique. But if you want
> you can always have a symbol table to assign labels to the ids.
> For example:
>
> >>> import networkx as NX
> >>> import pylab as P
> >>> G=NX.Graph()
> >>> label={}
> >>> label[1]="a"
> >>> label[2]="="
> >>> label[3]="1"
> >>> G.add_edge(1,2)
> >>> G.add_edge(3,2)
> >>> label[4]="b"
> >>> label[5]="="
> >>> label[6]="1"
> >>> G.add_edge(4,5)
> >>> G.add_edge(6,5)
> >>> print [label[n] for n in G]
> >>> for (u,v) in G.edges():
> >>> print (label[u],label[v])
> >>> NX.draw(G,labels=label)
> >>> P.show()

Thanks, that might help, although I'm looking for something that will
still let me easily query nodes based on function. If every '=' node
has a unique value, I won't be able to quickly find all links to '='
nodes. However, I think I might be able to emulate this functionality
by using an XDiGraph and representing the unique id on the link. So
add_edge('a', '=', 123) would have the meaning of a link from node 'a'
to node '=' with id 123.

Reply all
Reply to author
Forward
0 new messages