# Graph minors in programming

Hello

In a program I am writing, I need to know whether or not one graph is a minor of another, True or False. The function g.minor(h) returns the parts of g that are the minor h, if it exists, something like this:

{0: , 1: , 2: , 3: [3, 6], 4: , 5: }

UPDATE: I think I can handle this but I'm sure it's not the best way, so any help is still appreciated. By doing L = g.minor(h) and then testing if len(flatten(L.values())) > 0, I will get True if h is a minor of g (or so it seems).

If no such minor exists, it throws an error. To be specific, it says "Traceback", then several lines of stuff I don't get at all, and then "ValueError: This graph has no minor isomorphic to H !". I don't know how to deal with either of these situations really. Can you help me out? Again, I just want True in the first case and False in the second case for programming purposes, as in graph theory knowing if a graph has another graph as a minor is very important in many instances.

I am using Version 4.6 (since I am a Windows user), in case that matters.

Thanks for any help!

edit retag close merge delete

Sort by » oldest newest most voted 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.

more

This worked and is very simple! This is very helpful. Thanks.