Ask Your Question

Revision history [back]

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