Processing math: 100%
Ask Your Question
1

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

asked 8 years ago

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 vV(G), that Gv 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

Preview: (hide)

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 ( 8 years ago )

1 Answer

Sort by » oldest newest most voted
2

answered 8 years ago

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.

Preview: (hide)
link

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 ( 8 years ago )

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 ( 8 years ago )

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: 8 years ago

Seen: 647 times

Last updated: Nov 05 '16