# HG changeset patch
# User Nathann Cohen
# Date 1267382744 -3600
# Node ID ffcf15176a09ea5e4fd49f067921ad8bb85d6647
# Parent ad225fbca74921d05b49b616c18820365bf389af
trac 8404 : Minors...
diff -r ad225fbca749 -r ffcf15176a09 sage/graphs/graph.py
--- a/sage/graphs/graph.py Fri Mar 05 12:28:04 2010 +0100
+++ b/sage/graphs/graph.py Sun Feb 28 19:45:44 2010 +0100
@@ -1897,6 +1897,166 @@
return repr
+ def minor(self, H, **kwds):
+ r"""
+ Returns the vertices of a minor isomorphic to `H` in the current graph.
+
+ We say that a graph `G` has a `H`-minor (or that it has
+ a graph isomorphic to `H` as a minor), if for all `h\in H`,
+ there exist disjoint sets `S_h \subseteq V(G)` such that
+ once the vertices of each `S_h` have been merged to create
+ a new graph `G'`, this new graph contains `H` as a subgraph.
+
+ For more information of minor theory, see
+ http://en.wikipedia.org/wiki/Minor_(graph_theory)
+
+ INPUT:
+
+ - ``H`` -- The minor to find for in the current graph
+
+ - ``**kwds`` -- arguments to be passed down to the ``solve``
+ function of ``MixedIntegerLinearProgram``. See the documentation
+ of ``MixedIntegerLinearProgram.solve`` for more informations.
+
+ OUTPUT:
+
+ A dictionary associating to each vertex of `H` the set of vertices
+ in the current graph representing it.
+
+ ALGORITHM:
+
+ Mixed Integer Linear Programming
+
+ COMPLEXITY:
+
+ Theoretically, when `H` is fixed, testing for the existence of
+ a `H`-minor is polynomial. The known algorithms are highly
+ exponential in `H`, though.
+
+ NOTE:
+
+ This function can be expected to be *very* slow, especially
+ where the minor does not exist.
+
+ EXAMPLE:
+
+ Trying to find a minor isomorphic to `K_4` in
+ the `4\times 4` grid ::
+
+ sage: g = graphs.GridGraph([4,4])
+ sage: h = graphs.CompleteGraph(4)
+ sage: L = g.minor(h) # optional - requires GLPK, CPLEX or CBC
+ sage: gg = g.subgraph(flatten(L.values(), max_level = 1)) # optional - requires GLPK, CPLEX or CBC
+ sage: _ = [gg.merge_vertices(l) for l in L.values() if len(l)>1] # optional - requires GLPK, CPLEX or CBC
+ sage: gg.is_isomorphic(h) # optional - requires GLPK, CPLEX or CBC
+ True
+
+ We can also try to prove this way that the Petersen graph
+ is not planar, as it has a `K_5` minor ::
+
+ sage: g = graphs.PetersenGraph()
+ sage: K5_minor = g.minor(graphs.CompleteGraph(5)) # optional long - requires GLPK, CPLEX or CBC
+
+ And even a `K_{3,3}` minor ::
+
+ sage: K33_minor = g.minor(graphs.CompleteBipartiteGraph(3,3)) # optional long - requires GLPK, CPLEX or CBC
+
+ (It is much faster to use the linear-time test of
+ planarity in this situation, though)
+
+ As there is no cycle in a tree, looking for a `K_3` minor is useless.
+ This function will raise an exception in this case::
+
+ sage: g = graphs.RandomGNP(20,.5)
+ sage: g = g.subgraph(edges = g.min_spanning_tree())
+ sage: g.is_tree()
+ True
+ sage: L = g.minor(graphs.CompleteGraph(3)) # optional - requires GLPK, CPLEX or CBC
+ Traceback (most recent call last):
+ ...
+ ValueError: This graph has no minor isomorphic to H !
+ """
+
+ from sage.numerical.mip import MixedIntegerLinearProgram, MIPSolverException
+ p = MixedIntegerLinearProgram()
+
+ # sorts an edge
+ S = lambda (x,y) : (x,y) if x