ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 15 Sep 2011 16:28:43 +0200Determining Complete Multipartite Graphs (Specifically Tripartite)https://ask.sagemath.org/question/8319/determining-complete-multipartite-graphs-specifically-tripartite/I have been reading the Sage References, and it does not seem that complete multipartite graphs are defined in Sage yet. I have tried to critique the following two lines of code to see if it would work for complete multipartite graphs.
def is_complete_multipartite(p):
return p.is_multipartite() and p.num_edges() == mul(map(len, g.multipartite_sets()))
dd = filter(is_complete_multipartite, d)
I get a "graph attribute needed" error. I just wanted to verify that this is the case. Thanks again for the help!Wed, 14 Sep 2011 17:41:08 +0200https://ask.sagemath.org/question/8319/determining-complete-multipartite-graphs-specifically-tripartite/Answer by Mike Hansen for <p>I have been reading the Sage References, and it does not seem that complete multipartite graphs are defined in Sage yet. I have tried to critique the following two lines of code to see if it would work for complete multipartite graphs. </p>
<pre><code>def is_complete_multipartite(p):
return p.is_multipartite() and p.num_edges() == mul(map(len, g.multipartite_sets()))
dd = filter(is_complete_multipartite, d)
</code></pre>
<p>I get a "graph attribute needed" error. I just wanted to verify that this is the case. Thanks again for the help!</p>
https://ask.sagemath.org/question/8319/determining-complete-multipartite-graphs-specifically-tripartite/?answer=12649#post-id-12649You're getting that error because there is no `is_multipartite` method on graphs in Sage, nor is there a `multipartite_sets` method. You can use the `coloring` method to get what you need since a tripartite graph is 3-colourable. Thus, you could do something like the following: (To simplify things, I'll just consider the `m > 1` case.)
def is_complete_multipartite(m):
assert m > 1
def is_complete_m_partite(g):
coloring = g.coloring()
if coloring is None: #empty graph
return False
return len(coloring) == m and g.num_edges() == mul(map(len, coloring))
return is_complete_m_partite
Then, you'd use it like
dd = filter(is_complete_multipartite(3), d)
to test for complete tripartiteness.Wed, 14 Sep 2011 19:16:41 +0200https://ask.sagemath.org/question/8319/determining-complete-multipartite-graphs-specifically-tripartite/?answer=12649#post-id-12649Comment by acacost1 for <p>You're getting that error because there is no <code>is_multipartite</code> method on graphs in Sage, nor is there a <code>multipartite_sets</code> method. You can use the <code>coloring</code> method to get what you need since a tripartite graph is 3-colourable. Thus, you could do something like the following: (To simplify things, I'll just consider the <code>m > 1</code> case.)</p>
<pre><code>def is_complete_multipartite(m):
assert m > 1
def is_complete_m_partite(g):
coloring = g.coloring()
if coloring is None: #empty graph
return False
return len(coloring) == m and g.num_edges() == mul(map(len, coloring))
return is_complete_m_partite
</code></pre>
<p>Then, you'd use it like</p>
<pre><code>dd = filter(is_complete_multipartite(3), d)
</code></pre>
<p>to test for complete tripartiteness.</p>
https://ask.sagemath.org/question/8319/determining-complete-multipartite-graphs-specifically-tripartite/?comment=21253#post-id-21253Thanks! I'll give it try.Thu, 15 Sep 2011 16:28:43 +0200https://ask.sagemath.org/question/8319/determining-complete-multipartite-graphs-specifically-tripartite/?comment=21253#post-id-21253