Graph Colouring
Last updated
Last updated
Graph coloring (also called vertex coloring) is a way of coloring a graph’s vertices such that no two adjacent vertices share the same color. This post will discuss a greedy algorithm for graph coloring and minimize the total number of colors used.
For example, consider the following graph:
We can color it in many ways by using the minimum of 3 colors.
Please note that we can’t color the above graph using two colors.
Before discussing the greedy algorithm to color graphs, let’s talk about basic graph coloring terminology.
K–colorable graph:
A coloring using at most k
colors is called a (proper) k–coloring, and a graph that can be assigned a (proper) k–coloring is k–colorable.
K–chromatic graph:
The smallest number of colors needed to color a graph G
is called its chromatic number, and a graph that is k–chromatic if its chromatic number is exactly k
.
Brooks’ theorem:
Brooks’ theorem states that a connected graph can be colored with only x
colors, where x
is the maximum degree of any vertex in the graph except for complete graphs and graphs containing an odd length cycle, which requires x+1
colors.
Greedy coloring considers the vertices of the graph in sequence and assigns each vertex its first available color, i.e., vertices are considered in a specific order v1
, v2
, … vn
, and vi
and assigned the smallest available color which is not used by any of vi
’s neighbors.
Greedy coloring doesn’t always use the minimum number of colors possible to color a graph. For a graph of maximum degree x
, greedy coloring will use at most x+1
color. Greedy coloring can be arbitrarily bad; for example, the following crown graph (a complete bipartite graph), having n
vertices, can be 2–colored (refer left image), but greedy coloring resulted in n/2
colors (refer right image).
Output: The color assigned to vertex 0 is BLUE The color assigned to vertex 1 is GREEN The color assigned to vertex 2 is BLUE The color assigned to vertex 3 is RED The color assigned to vertex 4 is RED The color assigned to vertex 5 is GREEN
The time complexity of the above solution is O(V × E), where V
and E
are the total number of vertices and edges in the graph, respectively.
Applications of graph coloring:
The problem of coloring a graph arises in many practical areas such as pattern matching, designing seating plans, scheduling exam timetable, solving Sudoku puzzles, etc.