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.Sun, 15 Jan 2017 11:24:05 +0100How do I add an attribute to the object/class "Graph?"https://ask.sagemath.org/question/35439/how-do-i-add-an-attribute-to-the-objectclass-graph/ Hi,
I am researching methods that relate to graph minors. Currently I am looking at creating a method/function/attribute (I give the options as I do not know the best means, . . . , yet) which will allow me to test for forbidden minors.
An example is the complete graph on 6 vertices is a forbidden minor for the class of graphs that are not apex. Meaning, that given a graph G and a $v\in V(G)$, that $G-v$ is not planar. So the code that I have been working on should test if a graph matches a certain criteria. I would be able to ask SageMath in the following way
K6=graphs.CompleteGraph(6); K6.is_apex()
Of which the response would be
False
This is the code that I have been working on
def is_apex(g):
for v in g.vertex_iterator():
l = g.neighbors(v)
g.delete_vertex(v)
if g.is_planar():
return True
g.add_vertex(v)
g.add_edges([(v, y) for y in l])
return False
Now I had help on this from another member in the AskSage community, so thank you.
But now I am trying to do something like this:
class Graph():
not_apex = False
def is_apex(self):
for v in self.vertex_iterator():
l = self.neighbors(v)
g.delete_vertex(v)
if self.is_planar():
return True
self.add_vertex(v)
self.add_edges([(v, y) for y in l])
return False
Which when I enter
x.is_apex()
Of which the response that I get
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-15-e74e0334c694> in <module>()
----> 1 x.is_apex()
AttributeError: 'Graph' object has no attribute 'is_apex'
I am still a nooooooooob, so any help in understanding what I need to do would be great.
Thank you
Fri, 04 Nov 2016 22:31:21 +0100https://ask.sagemath.org/question/35439/how-do-i-add-an-attribute-to-the-objectclass-graph/Comment by slelievre for <p>Hi,</p>
<p>I am researching methods that relate to graph minors. Currently I am looking at creating a method/function/attribute (I give the options as I do not know the best means, . . . , yet) which will allow me to test for forbidden minors.</p>
<p>An example is the complete graph on 6 vertices is a forbidden minor for the class of graphs that are not apex. Meaning, that given a graph G and a $v\in V(G)$, that $G-v$ is not planar. So the code that I have been working on should test if a graph matches a certain criteria. I would be able to ask SageMath in the following way</p>
<pre><code>K6=graphs.CompleteGraph(6); K6.is_apex()
</code></pre>
<p>Of which the response would be </p>
<pre><code>False
</code></pre>
<p>This is the code that I have been working on</p>
<pre><code>def is_apex(g):
for v in g.vertex_iterator():
l = g.neighbors(v)
g.delete_vertex(v)
if g.is_planar():
return True
g.add_vertex(v)
g.add_edges([(v, y) for y in l])
return False
</code></pre>
<p>Now I had help on this from another member in the AskSage community, so thank you.</p>
<p>But now I am trying to do something like this:</p>
<pre><code>class Graph():
not_apex = False
def is_apex(self):
for v in self.vertex_iterator():
l = self.neighbors(v)
g.delete_vertex(v)
if self.is_planar():
return True
self.add_vertex(v)
self.add_edges([(v, y) for y in l])
return False
</code></pre>
<p>Which when I enter </p>
<pre><code>x.is_apex()
</code></pre>
<p>Of which the response that I get</p>
<pre><code>---------------------------------------------------------------------------
</code></pre>
<p>AttributeError Traceback (most recent call last)
<ipython-input-15-e74e0334c694> in <module>()
----> 1 x.is_apex()</p>
<p>AttributeError: 'Graph' object has no attribute 'is_apex'</p>
<p>I am still a nooooooooob, so any help in understanding what I need to do would be great.</p>
<p>Thank you</p>
https://ask.sagemath.org/question/35439/how-do-i-add-an-attribute-to-the-objectclass-graph/?comment=35469#post-id-35469Hi, could you edit your question and apply "code formatting" to the error message too?
(Just so it displays nicely.) Thanks!Mon, 07 Nov 2016 14:04:02 +0100https://ask.sagemath.org/question/35439/how-do-i-add-an-attribute-to-the-objectclass-graph/?comment=35469#post-id-35469Answer by paulmasson for <p>Hi,</p>
<p>I am researching methods that relate to graph minors. Currently I am looking at creating a method/function/attribute (I give the options as I do not know the best means, . . . , yet) which will allow me to test for forbidden minors.</p>
<p>An example is the complete graph on 6 vertices is a forbidden minor for the class of graphs that are not apex. Meaning, that given a graph G and a $v\in V(G)$, that $G-v$ is not planar. So the code that I have been working on should test if a graph matches a certain criteria. I would be able to ask SageMath in the following way</p>
<pre><code>K6=graphs.CompleteGraph(6); K6.is_apex()
</code></pre>
<p>Of which the response would be </p>
<pre><code>False
</code></pre>
<p>This is the code that I have been working on</p>
<pre><code>def is_apex(g):
for v in g.vertex_iterator():
l = g.neighbors(v)
g.delete_vertex(v)
if g.is_planar():
return True
g.add_vertex(v)
g.add_edges([(v, y) for y in l])
return False
</code></pre>
<p>Now I had help on this from another member in the AskSage community, so thank you.</p>
<p>But now I am trying to do something like this:</p>
<pre><code>class Graph():
not_apex = False
def is_apex(self):
for v in self.vertex_iterator():
l = self.neighbors(v)
g.delete_vertex(v)
if self.is_planar():
return True
self.add_vertex(v)
self.add_edges([(v, y) for y in l])
return False
</code></pre>
<p>Which when I enter </p>
<pre><code>x.is_apex()
</code></pre>
<p>Of which the response that I get</p>
<pre><code>---------------------------------------------------------------------------
</code></pre>
<p>AttributeError Traceback (most recent call last)
<ipython-input-15-e74e0334c694> in <module>()
----> 1 x.is_apex()</p>
<p>AttributeError: 'Graph' object has no attribute 'is_apex'</p>
<p>I am still a nooooooooob, so any help in understanding what I need to do would be great.</p>
<p>Thank you</p>
https://ask.sagemath.org/question/35439/how-do-i-add-an-attribute-to-the-objectclass-graph/?answer=35443#post-id-35443Looks like a Python error: class methods need to be indented under the class name.
You probably also want your new class to inherit from the base `Graph` class, so you would declare it like
class NewGraph(Graph):
and then adjust your indentation.Sat, 05 Nov 2016 04:04:05 +0100https://ask.sagemath.org/question/35439/how-do-i-add-an-attribute-to-the-objectclass-graph/?answer=35443#post-id-35443Comment by fieldofnodes for <p>Looks like a Python error: class methods need to be indented under the class name.</p>
<p>You probably also want your new class to inherit from the base <code>Graph</code> class, so you would declare it like</p>
<pre><code> class NewGraph(Graph):
</code></pre>
<p>and then adjust your indentation.</p>
https://ask.sagemath.org/question/35439/how-do-i-add-an-attribute-to-the-objectclass-graph/?comment=36259#post-id-36259I am unfamiliar with the details of the "Graph" class need to know where to go to look for more info. Help?Wed, 11 Jan 2017 22:15:44 +0100https://ask.sagemath.org/question/35439/how-do-i-add-an-attribute-to-the-objectclass-graph/?comment=36259#post-id-36259Comment by paulmasson for <p>Looks like a Python error: class methods need to be indented under the class name.</p>
<p>You probably also want your new class to inherit from the base <code>Graph</code> class, so you would declare it like</p>
<pre><code> class NewGraph(Graph):
</code></pre>
<p>and then adjust your indentation.</p>
https://ask.sagemath.org/question/35439/how-do-i-add-an-attribute-to-the-objectclass-graph/?comment=36261#post-id-36261http://doc.sagemath.org/html/en/reference/graphs/sage/graphs/graph.html
http://doc.sagemath.org/html/en/reference/graphs/sage/graphs/digraph.html
http://doc.sagemath.org/html/en/reference/graphs/sage/graphs/generic_graph.htmlThu, 12 Jan 2017 05:02:44 +0100https://ask.sagemath.org/question/35439/how-do-i-add-an-attribute-to-the-objectclass-graph/?comment=36261#post-id-36261Comment by fieldofnodes for <p>Looks like a Python error: class methods need to be indented under the class name.</p>
<p>You probably also want your new class to inherit from the base <code>Graph</code> class, so you would declare it like</p>
<pre><code> class NewGraph(Graph):
</code></pre>
<p>and then adjust your indentation.</p>
https://ask.sagemath.org/question/35439/how-do-i-add-an-attribute-to-the-objectclass-graph/?comment=36297#post-id-36297So here is my code:
import sage.graphs.generic_graph
class NewGraph(Graph):
def is_apex(self):
not_apex = False
for v in self.vertex_iterator():
l = self.neighbors(v)
g.delete_vertex(v)
if self.is_planar():
return True
self.add_vertex(v)
self.add_edges([(v, y) for y in l])
return False
This is the result:
k6=graphs.CompleteGraph(6)
sage: k6.is_apex()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-3-e18df0843618> in <module>()
----> 1 k6.is_apex()
AttributeError: 'Graph' object has no attribute 'is_apex'
What is happening?Sun, 15 Jan 2017 11:24:05 +0100https://ask.sagemath.org/question/35439/how-do-i-add-an-attribute-to-the-objectclass-graph/?comment=36297#post-id-36297