Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

answered 2011-05-13 05:25:07 -0500

DSM gravatar image

The "Traceback" bits are showing you the code near where the uncaught exception was raised. Typing "Graph.minor??" will show you the whole code.

In Python, you can catch an exception by using a try/except pair. So one way to write a function that does what you want is:

def has_minor(G, H):
    try:
        m = G.minor(H)
        return True
    except ValueError: 
        return False


sage: has_minor(graphs.CompleteGraph(4), graphs.DurerGraph())
False
sage: has_minor(graphs.DurerGraph(), graphs.CompleteGraph(4))
True

The above calls the minor function, and tries to put that result into m. If that function succeeds, then it found a minor, and so we return True. On the other hand, if the .minor(H) call didn't succeed, then it jumps to the "except ValueError:" branch and returns False instead. The tutorial on exceptions is pretty good in explaining how the control flow works.

This isn't necessarily the most efficient way, but it should get the job done.