Any elegant way to construct the complete $k$-partite graph in Python?

Paul Miller paul.w.miller.please.dont.spam.me at wmich.edu
Tue Nov 24 01:23:53 EST 2009


On Mon, 23 Nov 2009 19:57:05 -0800, Richard Thomas wrote:

> Not sure exactly how you're representing graphs, this seems like the
> simplest way of listing the edges.
> 
> def complete_partite(*sizes):
>     total = sum(sizes)
>     nodes, edges = range(total), []
>     for group in xrange(len(sizes)):
>         low = sum(sizes[:group-1])
>         high = sum(sizes[:group])
>         edges.extend((i, j) for i in xrange(low, high)
>                             for j in xrange(high, total))
>     return nodes, edges

Thanks!  I think this is what I was looking for (unless the collective 
wisdom of c.l.py can come up with something *even more* elegant). :-)



More information about the Python-list mailing list