Graph library for Python

Rhodri James rhodri at wildebst.demon.co.uk
Tue Dec 8 20:42:53 EST 2009


On Tue, 08 Dec 2009 04:28:05 -0000, geremy condra <debatem1 at gmail.com>  
wrote:

> On Mon, Dec 7, 2009 at 6:28 PM, M.-A. Lemburg <mal at egenix.com> wrote:

>> I wasn't thinking of anything clever :-) ...
>>
>> g = Graph(
>>      [Node("a"), Node("b"), Node("c")],
>>      [Edge(Node("a"), Node("b"), "ab"),
>>       Edge(Node("a"), Node("c"), "ac"),
>>       Edge(Node("b"), Node("c"), "bc"),
>>      ])
>>
>> The main motivation here is to get lists, sets and dicts
>> play nice together.
>
> Generally, we've tried to discourage people from instantiating
> nodes and edges directly, in favor of having them controlled
> through the graph. Maybe something along the lines of:
>
> g = Graph(nodes=['a', 'b', 'c'], edges=[('a', 'b'), ('a', 'c'), ('b',  
> 'c')])
>
> ?

That works fine for simple cases like this, but starts getting unpleasant  
if you want to initialise with attributes.  Under those circumstances  
using Node and Edge explicitly is much cleaner.  The only extra I'd  
suggest is allowing is_directed as a keyword argument, so you can set the  
default for all edges if you want to.

g = Graph(
     nodes=[Node("a", colour="red"),
            Node("b", colour="white"),
            Node("c", colour="blue")],
     edges=[Edge("a", "b", "ab", weight=2),
            Edge("a", "c", "ac", is_directed=True),
            Edge("b", "c", "bc", style="dotted")],
     is_directed=True)

I could see a use for this tracking a database structure using a constant  
graph, hence all set up in one go for preference.

-- 
Rhodri James *-* Wildebeest Herder to the Masses



More information about the Python-list mailing list