Greedy coloring
In the study of graph coloring problems in mathematics and computer science, a greedy coloring or sequential coloring is a coloring of the vertices of a graph formed by a greedy algorithm that considers the vertices of the graph in sequence and assigns each vertex its first available color. Greedy colorings can be found in linear time, but they do not, in general, use the minimum number of colors possible.
Different choices of the sequence of vertices will typically produce different colorings of the given graph, so much of the study of greedy colorings has concerned how to find a good ordering. There always exists an ordering that produces an optimal coloring, but although such orderings can be found for many special classes of graphs, they are hard to find in general. Commonly used strategies for vertex ordering involve placing higher-degree vertices earlier than lower-degree vertices, or choosing vertices with fewer available colors in preference to vertices that are less constrained.
Variations of greedy coloring choose the colors in an online manner, without any knowledge of the structure of the uncolored part of the graph, or choose other colors than the first available in order to reduce the total number of colors. Greedy coloring algorithms have been applied to scheduling and register allocation problems, the analysis of combinatorial games, and the proofs of other mathematical results including Brooks' theorem on the relation between coloring and degree.
Other concepts in graph theory derived from greedy colorings include the Grundy number of a graph, and the well-colored graphs, graphs for which all greedy colorings use the same number of colors.
Algorithm
The greedy coloring for a given vertex ordering can be computed by an algorithm that runs in linear time. The algorithm processes the vertices in the given ordering, assigning a color to each one as it is processed. The colors may be represented by the numbers and each vertex is given the color with the smallest number that is not already used by one of its neighbors.To find the smallest available color, one may use an array to count the number of neighbors of each color, and then scan the array to find the index of its first zero.
In Python, the algorithm can be expressed as:
def first_available:
"""Return smallest non-negative integer not in the given set of colors.
"""
color = 0
while color in colors:
color += 1
return color
def greedy_coloring:
"""Find the greedy coloring of G in the given order.
The representation of G is assumed to be like https://www.python.org/doc/essays/graphs/
in allowing neighbors of a node/vertex to be iterated over by "for w in G".
The return value is a dictionary mapping vertices to their colors.
"""
coloring = dict
for node in order:
used_neighbour_colors =
coloring = first_available
return coloring
The
first_available subroutine takes time proportional to the length of its argument list, because it performs two loops, one over the list itself and one over a list of counts that has the same length. The time for the overall coloring algorithm is dominated by the calls to this subroutine. Each edge in the graph contributes to only one of these calls, the one for the endpoint of the edge that is later in the vertex ordering. Therefore, the sum of the lengths of the argument lists to first_available, and the total time for the algorithm, are proportional to the number of edges in the graph.An alternative algorithm, producing the same coloring, is to choose the sets of vertices with each color, one color at a time. In this method, each color class is chosen by scanning through the vertices in the given ordering. When this scan encounters an uncolored vertex that has no neighbor in, it adds to. In this way, becomes a maximal independent set among the vertices that were not already assigned smaller colors. The algorithm repeatedly finds color classes in this way until all vertices are colored. However, it involves making multiple scans of the graph, one scan for each color class, instead of the method outlined above which uses only a single scan.
Choice of ordering
Different orderings of the vertices of a graph may cause the greedy coloring to use different numbers of colors, ranging from the optimal number of colors to, in some cases, a number of colors that is proportional to the number of vertices in the graph. For instance, a crown graph can be a particularly bad case for greedy coloring. With the vertex ordering, a greedy coloring will use colors, one color for each pair. However, the optimal number of colors for this graph is two, one color for the vertices and another for the vertices. There also exist graphs such that with high probability a randomly chosen vertex ordering leads to a number of colors much larger than the minimum. Therefore, it is of some importance in greedy coloring to choose the vertex ordering carefully.Good orders
The vertices of any graph may always be ordered in such a way that the greedy algorithm produces an optimal coloring. For, given any optimal coloring, one may order the vertices by their colors. Then when one uses a greedy algorithm with this order, the resulting coloring is automatically optimal. However, because optimal graph coloring is NP-complete, any subproblem that would allow this problem to be solved quickly, including finding an optimal ordering for greedy coloring, is NP-hard.In interval graphs and chordal graphs, if the vertices are ordered in the reverse of a perfect elimination ordering,
then the earlier neighbors of every vertex will form a clique. This property causes the greedy coloring to produce an optimal coloring, because it never uses more colors than are required for each of these cliques. An elimination ordering can be found in linear time, when it exists.
More strongly, any perfect elimination ordering is hereditarily optimal, meaning that it is optimal both for the graph itself and for all of its induced subgraphs. The perfectly orderable graphs are defined as graphs that have a hereditarily optimal ordering. Recognizing perfectly orderable graphs is also NP-complete.
Bad orders
The number of colors produced by the greedy coloring for the worst ordering of a given graph is called its Grundy number.Just as finding a good vertex ordering for greedy coloring is difficult, so is finding a bad vertex ordering.
It is NP-complete to determine, for a given graph and number, whether there exists an ordering of the vertices of that causes the greedy algorithm to use or more colors. In particular, this means that it is difficult to find the worst ordering for.
Graphs for which order is irrelevant
The well-colored graphs are the graphs for which all vertex orderings produce the same number of colors. This number of colors, in these graphs, equals both the chromatic number and the Grundy number. They include the cographs, which are exactly the graphs in which all induced subgraphs are well-colored. However, it is co-NP-complete to determine whether a graph is well-colored.If a random graph is drawn from the Erdős–Rényi model with constant probability of including each edge, then any vertex ordering that is chosen independently of the graph edges leads to a coloring whose number of colors is close to twice the optimal value, with high probability.
It remains unknown whether there is any polynomial time method for finding significantly better colorings of these graphs.
Degeneracy
Because optimal vertex orderings are hard to find, heuristics have been used that attempt to reduce the number of colors while not guaranteeing an optimal number of colors. A commonly used ordering for greedy coloring is to choose a vertex of minimum degree, order the subgraph with removed recursively, and then place last in the ordering. The largest degree of a removed vertex that this algorithm encounters is called the degeneracy of the graph, denoted. In the context of greedy coloring, the same ordering strategy is also called the smallest last ordering. This vertex ordering, and the degeneracy, may be computed in linear time.It can be viewed as an improved version of an earlier vertex ordering method, the largest-first ordering, which sorts the vertices in descending order by their degrees.
With the degeneracy ordering, the greedy coloring will use at most colors. This is because, when colored, each vertex will have at most already-colored neighbors, so one of the first colors will be free for it to use. Greedy coloring with the degeneracy ordering can find optimal colorings for certain classes of graphs, including trees, pseudoforests, and crown graphs. define a graph to be -perfect if, for and every induced subgraph of, the chromatic number equals the degeneracy plus one. For these graphs, the greedy algorithm with the degeneracy ordering is always optimal.
Every -perfect graph must be an even-hole-free graph, because even cycles have chromatic number two and degeneracy two, not matching the equality in the definition of -perfect graphs. If a graph and its complement graph are both even-hole-free, they are both -perfect. The graphs that are both perfect graphs and -perfect graphs are exactly the chordal graphs. On even-hole-free graphs more generally, the degeneracy ordering approximates the optimal coloring to within at most twice the optimal number of colors; that is, its approximation ratio is 2. On unit disk graphs its approximation ratio is 3. The triangular prism is the smallest graph for which one of its degeneracy orderings leads to a non-optimal coloring, and the square antiprism is the smallest graph that cannot be optimally colored using any of its degeneracy orderings.
Adaptive orders
proposes a strategy, called DSatur, for vertex ordering in greedy coloring that interleaves the construction of the ordering with the coloring process.In his version of the greedy coloring algorithm, the next vertex to color at each step is chosen as the one with the largest number of distinct colors in its neighborhood. In case of ties, a vertex of maximal degree in the subgraph of uncolored vertices is chosen from the tied vertices. By keeping track of the sets of neighboring colors and their cardinalities at each step, it is possible to implement this method in linear time.
This method can find the optimal colorings for bipartite graphs, all cactus graphs, all wheel graphs, all graphs on at most six vertices, and almost every -colorable graph. Although originally claimed that this method finds optimal colorings for the Meyniel graphs, they later found a counterexample to this claim.