Get this book -> Problems on Array: For Interviews and Competitive Programming

Reading time: 25 minutes

In graph theory, **graph coloring** is a special case of graph labeling ; it is an assignment of labels traditionally called "colors" to elements of a graph subject to certain constraints. **In its simplest form , it is a way of coloring the vertices of a graph such that no two adjacent vertices share the same color; this is called a vertex coloring**. Similarly, an **edge coloring** assigns a color to each edge so that no two adjacent edges share the same color, and a **face coloring** of a planar graph assigns a color to each face or region so that no two faces that share a boundary have the same color.

**Chromatic Number**: The smallest number of colors needed to color a graph G is called its chromatic number. For example, the following can be colored minimum 3 colors.

**Vertex coloring** is the starting point of the subject, and other coloring problems can be transformed into a vertex version. For example, an edge coloring of a graph is just a vertex coloring of its line graph, and a face coloring of a plane graph is just a vertex coloring of its dual. However, non-vertex coloring problems are often stated and studied as is. That is partly for perspective, and partly because some problems are best studied in non-vertex form, as for instance is edge coloring .

## Origin

The convention of using colors originates from coloring the countries of a map, where each face is literally colored. This was generalized to coloring the faces of a graph embedded in the plane. By planar duality it became coloring the vertices, and in this form it generalizes to all graphs. In mathematical and computer representations, it is typical to use the first few positive or non negative integers as the "colors". In general, one can use any finite set as the "color set". The nature of the coloring problem depends on the number of colors but not on what they are.

### Definition and terminology

**Vertex coloring**

When used without any qualification, a **coloring** of a graph is almost always a proper vertex coloring, namely a labeling of the graphâ€™s vertices with colors such that no two vertices sharing the same **edge** have the same color. Since a vertex with a **loop** (i.e. a connection directly back to itself) could never be properly colored, it is understood that graphs in this context are loopless.

The terminology of using colors for vertex labels goes back to map coloring. Labels like red and blue are only used when the number of colors is small, and normally it is understood that the labels are drawn from the integers {1, 2, 3, ...}.

A coloring using at most k colors is called a (proper) **k-coloring**. The smallest number of colors needed to color a graph G is called its **chromatic number**, and is often denoted Ï‡(G). Sometimes Î³(G) is used, since Ï‡(G) is also used to denote the Euler characteristic of a graph. A graph that can be assigned a (proper) k-coloring is k-colorable, and it is k-chromatic if its chromatic number is exactly k.

**Chromatic polynomial**

The chromatic polynomial counts the number of ways a graph can be colored using no more than a given number of colors. For example, using three colors, the graph in the adjacent image can be colored in 12 ways. With only two colors, it cannot be colored at all. With four colors, it can be colored in 24 + 4â‹…12 = 72 ways: using all four colors, there are 4! = 24 valid colorings (every assignment of four colors to any 4-vertex graph is a proper coloring); and for every choice of three of the four colors, there are 12 valid 3-colorings.

**Edge coloring**

An **edge coloring** of a graph is a proper coloring of the edges, meaning an assignment of colors to edges so that no vertex is incident to two edges of the same color. An edge coloring with k colors is called a **k-edge-coloring** and is equivalent to the problem of partitioning the edge set into k matchings. The smallest number of colors needed for an edge coloring of a graph G is the chromatic index, or edge chromatic number, Ï‡â€²(G).

**Total coloring**

Total coloring is a type of coloring on the vertices and edges of a graph. When used without any qualification, a total coloring is always assumed to be proper in the sense that no adjacent vertices, no adjacent edges, and no edge and its end-vertices are assigned the same color. The total chromatic number Ï‡â€³(G) of a graph G is the fewest colors needed in any total coloring of G.

### Algorithms

**Polynomial time**

Determining if a graph can be colored with 2 colors is equivalent to determining whether or not the graph is bipartite, and thus computable in linear time using breadth-first search or depth-first search. More generally, the chromatic number and a corresponding coloring of perfect graphs can be computed in polynomial time using semidefinite programming. Closed formulas for chromatic polynomial are known for many classes of graphs, such as forests, chordal graphs, cycles, wheels, and ladders, so these can be evaluated in polynomial time.

If the graph is planar and has low branch-width (or is nonplanar but with a known branch decomposition), then it can be solved in polynomial time using dynamic programming. In general, the time required is polynomial in the graph size, but exponential in the branch-width.

**Exact algorithms**

**Brute-force search** for a k-coloring considers each of the $k^n$ assignments of k colors to n vertices and checks for each if it is legal. To compute the chromatic number and the chromatic polynomial, this procedure is used for every k=1,2,....n-1, impractical for all but the smallest input graphs.

Using dynamic programming and a bound on the number of maximal independent sets, k-colorability can be decided in time and space $O(2.445^{n})$ $O(2.445^{n})$.Using the principle of **inclusionâ€“exclusion** and **Yatesâ€™s algorithm** for the fast zeta transform, k-colorability can be decided in time $O(2^{n}n)$ for any k. Faster algorithms are known for 3- and 4-colorability, which can be decided in time $O(1.3289^{n})$ and $O(1.7272^{n})$ , respectively.

**Contraction**

The **contraction** $G/uv$ of a graph G is the graph obtained by identifying the vertices u and v, and removing any edges between them. The remaining edges originally incident to u or v are now incident to their identification. This operation plays a major role in the analysis of graph coloring.

The chromatic number satisfies the recurrence relation:

where u and v are adjacent vertices, and $G-uv$ is the graph with the edge uv removed. $P(G-uv,k)$ represents the number of possible proper colorings of the graph, where the vertices may have the same or different colors. Then the proper colorings arise from two different graphs. To explain, if the vertices u and v have different colors, then we might as well consider a graph where u and v are adjacent. If u and v have the same colors, we might as well consider a graph where u and v are contracted. Tutteâ€™s curiosity about which other graph properties satisfied this recurrence led him to discover a bivariate generalization of the chromatic polynomial, the **Tutte polynomial**.

These expressions give rise to a recursive procedure called the $deletionâ€“contraction algorithm$, which forms the basis of many algorithms for graph coloring.

**Greedy coloring**

The greedy algorithm considers the vertices in a specific order $v_{1}$,â€¦,$v_{n}$ and assigns to $v_{i}$ the smallest available color not used by $v_{1}$,â€¦,$v_{i-1}$ , adding a fresh color if needed. The quality of the resulting coloring depends on the chosen ordering. There exists an ordering that leads to a greedy coloring with the optimal number of Ï‡(G) colors. On the other hand, greedy colorings can be arbitrarily bad; for example, the crown graph on n vertices can be 2-colored, but has an ordering that leads to a greedy coloring with $n/2$ colors.

For chordal graphs, and for special cases of chordal graphs such as interval graphs and indifference graphs, the greedy coloring algorithm can be used to find optimal colorings in polynomial time, by choosing the vertex ordering to be the reverse of a perfect elimination ordering for the graph. The perfectly orderable graphs generalize this property, but it is NP-hard to find a perfect ordering of these graphs.

If the vertices are ordered according to their degrees, the resulting greedy coloring uses at most **ma$x_i$ min{d($x_i$)+1,i}** colors, at most one more than the graphâ€™s maximum degree. This heuristic is sometimes called the **Welshâ€“Powell** algorithm. Another heuristic due to BrÃ©laz establishes the ordering dynamically while the algorithm proceeds, choosing next the vertex adjacent to the largest number of different colors. Many other graph coloring heuristics are similarly based on greedy coloring for a specific static or dynamic strategy of ordering the vertices, these algorithms are sometimes called **sequential coloring** algorithms.

The maximum (worst) number of colors that can be obtained by the greedy algorithm, by using a vertex ordering chosen to maximize this number, is called the **Grundy number** of a graph.

**Parallel and distributed algorithms**

In the field of distributed algorithms, graph coloring is closely related to the problem of symmetry breaking. The current state-of-the-art randomized algorithms are faster for sufficiently large maximum degree Î” than deterministic algorithms. The fastest randomized algorithms employ the multi-trials technique by Schneider et al.

In a symmetric graph, a deterministic distributed algorithm cannot find a proper vertex coloring. Some auxiliary information is needed in order to break symmetry. A standard assumption is that initially each node has a unique identifier, for example, from the set {1, 2, ..., n}. Put otherwise, we assume that we are given an n-coloring. The challenge is to reduce the number of colors from n to, e.g., Î” + 1. The more colors are employed, e.g. O(Î”) instead of Î” + 1, the fewer communication rounds are required.

A straightforward distributed version of the greedy algorithm for (Î” + 1)-coloring requires Î˜(n) communication rounds in the worst case âˆ’ information may need to be propagated from one side of the network to another side.

**Decentralized algorithms**

Decentralized algorithms are ones where no message passing is allowed (in contrast to distributed algorithms where local message passing takes places), and efficient decentralized algorithms exist that will color a graph if a proper coloring exists. These assume that a vertex is able to sense whether any of its neighbors are using the same color as the vertex i.e., whether a local conflict exists. This is a mild assumption in many applications e.g. in wireless channel allocation it is usually reasonable to assume that a station will be able to detect whether other interfering transmitters are using the same channel (e.g. by measuring the SINR). This sensing information is sufficient to allow algorithms based on learning automata to find a proper graph coloring with probability one

**Computational complexity**

Graph coloring is computationally hard. It is NP-complete to decide if a given graph admits a k-coloring for a given k except for the cases k âˆˆ {0,1,2} . In particular, it is NP-hard to compute the chromatic number. The 3-coloring problem remains NP-complete even on 4-regular planar graphs. However, for every k > 3, a k-coloring of a planar graph exists by the four color theorem, and it is possible to find such a coloring in polynomial time.

The best known approximation algorithm computes a coloring of size at most within a factor O(n(log log n)2(log n)âˆ’3) of the chromatic number. For all Îµ > 0, approximating the chromatic number within n1âˆ’Îµ is NP-hard.

It is also NP-hard to color a 3-colorable graph with 4 colors and a k-colorable graph with k(log k ) / 25 colors for sufficiently large constant k.

example - Blum's Algorithm , Wigdersonâ€™s Algorithm .

**Complexity chart of various coloring algorithms**

## Further reading

First, get an overview of different approaches of the Graph Coloring problem:

Get an overview of Graph Coloring algorithmsLearn about a greedy approach for Graph Coloring

Understand Welsh Powell algorithm for Graph Coloring

Checking if a graph is bipartite using Graph Coloring and Breadth First Search

Learn about a Widgerson Algorithm for Graph Coloring