On 3 Dec 2009, at 08:39, Sudarshan Iyengar wrote:
> I would want to sort the nodes in such a way that G.nodes() displays
> the sorted order. My work involves removal of certain vertices and
> checking the damage created to the graph and every-time i remove a
> vertex G.nodes() gets haphazardly permuted.
>
> Is there any way out?
>
> Sudarshan
I'm using networkx for processing *ordered* trees (I know networkx was
not designed with trees in mind, but I like having the added-value of
a well-designed library instead of implementing and maintaining my own
tree class). You can keep the insertion order of the nodes by
replacing the dictionaries used for storing the edges by *ordered*
dictionaries. For the Digraph class, this involves self.pred and
self.succ/self.adj (no need to change self.nodes and self.graph). I
subclass Digraph as an OrderedDiGraph class, and override the
__init__, add_edge, add_edges_from, add_node, and add_nodes_from
methods, making sure that whenever a new dict for edges is created it
is an ordered dict. I imagine you can do the same thing for the other
graph classes.
You can find a pure Python recipe for an ordered dict by Raymond
Hettinger at
http://code.activestate.com/recipes/576693/ OrderedDict
will be included in the standard library of Python 2.7 (
http://docs.python.org/dev/library/collections.html#ordereddict-objects
). There are also faster alternatives like the ordereddict extension
module by Anton van der Neut (
http://www.xs4all.nl/~anthon/Python/ordereddict/
).
Erwin