Ask Your Question

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

Thoughts?

edit retag close merge delete

## 1 answer

Sort by ยป oldest newest most voted

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

more

## Comments

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

( 2016-10-11 19:50:11 -0500 )edit

## Your Answer

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

Add Answer

## Stats

Asked: 2016-10-10 15:18:52 -0500

Seen: 94 times

Last updated: Oct 11 '16