Ask Your Question
1

How do I write function to test if a graph is apex?

asked 2016-10-10 22:18:52 +0100

fieldofnodes gravatar image

I am working on topological graph theory problems and using SageMath. I want to create a function that give a boolean True or False answer, so I can use this answer for further use. My current function that I use:

def is_apex(a):
for v in a.vertex_iterator():
    l=a.neighbors(v)
    if a.is_planar(a.delete_vertex(v)):
        print("Deleting vertex ",v," makes a planar graph")
        a.add_vertex(v)
        a.add_edges([(v, y) for y in l])
    else:
        a.add_vertex(v)
        a.add_edges([(v, y) for y in l])
        print("Deleting vertex ",v," does not make a planar graph")

I am a noob when it comes to programming, and any help would be awesome.

I want a True returned if the graph is apex and a False value to return for not apex.

Thoughts?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2016-10-11 20:46:10 +0100

tmonteil gravatar image

First, here are some hints about your code:

  • a.delete_vertex(v) defines an action of the graph a (it modifies a itself), it does not return anything, so it is useless to compose such non-function with a.is_planar()
  • the block

    a.add_vertex(v)
    a.add_edges([(v, y) for y in l])
    

    is repeated twice, so it should be put outside the if/else statement.

  • you want to define a function, so it should return something, in the present case either True or False depending on if a good v was found or not. So you have to return True when you find a good v, and wait until the end and return False if no v was convenient.

Here is a possible rewrite of your function, i tried to modify it at least as possible:

def is_apex(a):
    for v in a.vertex_iterator():
        l = a.neighbors(v)
        a.delete_vertex(v)
        if a.is_planar():
            print("Deleting vertex ",v," makes a planar graph")
            return True
        else:
            print("Deleting vertex ",v," does not make a planar graph")
        a.add_vertex(v)
        a.add_edges([(v, y) for y in l])
    return False
edit flag offensive delete link more

Comments

Great mate. This is very much in the direction that I am heading. I really appreciate the comment.

fieldofnodes gravatar imagefieldofnodes ( 2016-10-12 02:50:11 +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-10-10 22:18:52 +0100

Seen: 793 times

Last updated: Oct 11 '16