Ask Your Question
1

How do I add an attribute to the object/class "Graph?"

asked 2016-11-04 22:31:21 +0100

fieldofnodes gravatar image

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

edit retag flag offensive close merge delete

Comments

Hi, could you edit your question and apply "code formatting" to the error message too? (Just so it displays nicely.) Thanks!

slelievre gravatar imageslelievre ( 2016-11-07 14:04:02 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2016-11-05 04:04:05 +0100

Looks 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.

edit flag offensive delete link more

Comments

I am unfamiliar with the details of the "Graph" class need to know where to go to look for more info. Help?

fieldofnodes gravatar imagefieldofnodes ( 2017-01-11 22:15:44 +0100 )edit

So 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?

fieldofnodes gravatar imagefieldofnodes ( 2017-01-15 11:24:05 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-11-04 22:31:21 +0100

Seen: 589 times

Last updated: Nov 05 '16