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

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.

1 answer

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: