# HG changeset patch
# User Nathann Cohen
# Date 1279782482 -28800
# Node ID be94a819ed703679bd9c3e2dfc89146792a93f3e
# Parent cb1ad1ee5bd452850fde04d47f9618dec4c3e997
trac #9575 -- Grundy Coloring
diff -r cb1ad1ee5bd4 -r be94a819ed70 sage/graphs/graph_coloring.py
--- a/sage/graphs/graph_coloring.py Fri Jul 16 01:54:01 2010 -0700
+++ b/sage/graphs/graph_coloring.py Thu Jul 22 15:08:02 2010 +0800
@@ -302,7 +302,7 @@
sage: vertex_coloring(g, value_only=True)
3
"""
- from sage.numerical.mip import MixedIntegerLinearProgram
+ from sage.numerical.mip import MixedIntegerLinearProgram, Sum
from sage.plot.colors import rainbow
# If k==None, tries to find an optimal coloring
@@ -419,7 +419,7 @@
p = MixedIntegerLinearProgram(maximization=True)
color = p.new_variable(dim=2)
# a vertex has exactly one color
- [p.add_constraint(sum([color[v][i] for i in xrange(k)]), min=1, max=1)
+ [p.add_constraint(Sum([color[v][i] for i in xrange(k)]), min=1, max=1)
for v in g.vertices()]
# adjacent vertices have different colors
[p.add_constraint(color[u][i] + color[v][i], max=1)
@@ -451,6 +451,160 @@
else:
return classes
+def grundy_coloring(g, k, value_only = True, solver = None, verbose = 0):
+ r"""
+ Computes the worst-case of a first-fit coloring with less than `k`
+ colors.
+
+ Definition :
+
+ A first-fit coloring is obtained by sequentially coloring the
+ vertices of a graph, assigning them the smallest color not already
+ assigned to one of its neighbors. The result is clearly a proper
+ coloring, which usually requires much more colors than an optimal
+ vertex coloring of the graph, and heavily depends on the ordering
+ of the vertices.
+
+ The number of colors required by the worst-case application of
+ this algorithm on a graph `G` is called the Grundy number, written
+ `\Gamma (G)`.
+
+ Equivalent formulation :
+
+ Equivalently, a Grundy coloring is a proper vertex coloring such
+ that any vertex colored with `i` has, for every `j*`
+ of the class
+ :class:`MixedIntegerLinearProgram `.
+
+ - ``value_only`` -- boolean (default: ``True``). When set to
+ ``True``, only the number of colors is returned. Otherwise, the
+ pair ``(nb_colors, coloring)`` is returned, where ``coloring``
+ is a dictionary associating its color (integer) to each vertex
+ of the graph.
+
+ - ``verbose`` -- integer (default: ``0``). Sets the level of
+ verbosity. Set to 0 by default, which means quiet.
+
+ ALGORITHM:
+
+ Integer Linear Program.
+
+ EXAMPLES:
+
+ The Grundy number of a `P_4` is equal to 3::
+
+ sage: from sage.graphs.graph_coloring import grundy_coloring
+ sage: g = graphs.PathGraph(4)
+ sage: grundy_coloring(g, 4)
+ 3
+
+ The Grundy number of the PetersenGraph is equal to 4::
+
+ sage: g = graphs.PetersenGraph()
+ sage: grundy_coloring(g, 5)
+ 4
+
+ It would have been sufficient to set the value of ``k`` to 4 in
+ this case, as `4 = \Delta(G)+1`.
+ """
+ from sage.numerical.mip import MixedIntegerLinearProgram
+ from sage.numerical.mip import MIPSolverException, Sum
+
+ p = MixedIntegerLinearProgram()
+
+ # List of colors
+ classes = range(k)
+
+ # b[v][i] is set to 1 if and only if v is colored with i
+ b = p.new_variable(dim=2)
+
+ # is_used[i] is set to 1 if and only if color [i] is used by some
+ # vertex
+ is_used = p.new_variable()
+
+ # Each vertex is in exactly one class
+ for v in g:
+ p.add_constraint(Sum( b[v][i] for i in classes ), max = 1, min = 1)
+
+ # Two adjacent vertices have different classes
+ for u,v in g.edges(labels = None):
+ for i in classes:
+ p.add_constraint(b[v][i] + b[u][i], max = 1)
+
+ # The following constraints ensure that if v is colored with i,
+ # then it has a neighbor colored with j for every j*