# Ticket #8404: trac_8404.patch

File trac_8404.patch, 6.7 KB (added by ncohen, 11 years ago)
• ## sage/graphs/graph.py

# HG changeset patch
# User Nathann Cohen <nathann.cohen@gmail.com>
# Date 1267382744 -3600
diff -r ad225fbca749 -r ffcf15176a09 sage/graphs/graph.py
 a 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