Any elegant way to construct the complete $k$-partite graph in Python?
anand jeyahar
anand.ibmgsi at gmail.com
Mon Nov 23 19:25:46 EST 2009
I am not sure what you mean by complete $k$-
partite graph....
There is the python-graph package(http://code.google.com/p/python-graph/)
you might wanna check out.
It does return a complete graph.. may be u can tweak it??
==============================================
Anand J
http://sites.google.com/a/cbcs.ac.in/students/anand
==============================================
The man who is really serious,
with the urge to find out what truth is,
has no style at all. He lives only in what is.
~Bruce Lee
Love is a trade with no accounting policies.
~Aang Jie
On Tue, Nov 24, 2009 at 05:35, Paul Miller <
paul.w.miller.please.dont.spam.me at wmich.edu> wrote:
> I was wondering if there were any neat tools (like for instance,
> something from itertools) that would help me write the following function
> more elegantly. The return value should, of course, be the complete $k$-
> partite graph $K_{n_1, n_2, \dots, n_k}$:
>
> def completeGraph (*ns):
> '''
> Returns the complete graph $K_{n_1, n_2, \dots, n_k}$ when passed
> the sequence \code {n_1, n_2, \dots, n_k}.
> '''
> if len (ns) == 1:
> return completeGraph ( * ([1] * ns[0]) )
> n = sum (ns)
> vertices = range (n)
> partition_indices = [sum (ns[:i]) for i in range (len (ns))]
> partite_sets = [vertices[partition_indices[i]:partition_indices[i+1]]
> \
> for i in range (len (partition_indices) - 1)]
> partite_sets.append (vertices[partition_indices [-1]:] )
>
> edges = []
> for i in range (len (partite_sets)):
> for j in range (i + 1, len (partite_sets)):
> edges.extend ([ (u, v) for u in partite_sets [i] for v in \
> partite_sets [j] ])
>
> return graph.Graph (vertices = vertices, edges = edges)
>
> Many thanks!
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091124/30e125be/attachment-0001.html>
More information about the Python-list
mailing list