Graph Coloring | Set 1 (Introduction and Applications)

Graph coloring problem is to assign colors to certain elements of a graph subject to certain constraints.

Vertex coloring is the most common graph coloring problem. The problem is, given m colors, find a way of coloring the vertices of a graph such that no two adjacent vertices are colored using same color. The other graph coloring problems like Edge Coloring (No vertex is incident to two edges of same color) and Face Coloring (Geographical Map Coloring) can be transformed into vertex coloring.

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.


The problem to find chromatic number of a given graph is NP Complete.

Applications of Graph Coloring:

The graph coloring problem has huge number of applications.

1) Making Schedule or Time Table: Suppose we want to make am exam schedule for a university. We have list different subjects and students enrolled in every subject. Many subjects would have common students (of same batch, some backlog students, etc). How do we schedule the exam so that no two exams with a common student are scheduled at same time? How many minimum time slots are needed to schedule all exams? This problem can be represented as a graph where every vertex is a subject and an edge between two vertices mean there is a common student. So this is a graph coloring problem where minimum number of time slots is equal to the chromatic number of the graph.

2) Mobile Radio Frequency Assignment: When frequencies are assigned to towers, frequencies assigned to all towers at the same location must be different. How to assign frequencies with this constraint? What is the minimum number of frequencies needed? This problem is also an instance of graph coloring problem where every tower represents a vertex and an edge between two towers represents that they are in range of each other.

3) Suduku: Suduku is also a variation of Graph coloring problem where every cell represents a vertex. There is an edge between two vertices if they are in same row or same column or same block.

4) Register Allocation: In compiler optimization, register allocation is the process of assigning a large number of target program variables onto a small number of CPU registers. This problem is also a graph coloring problem.

5) Bipartite Graphs: We can check if a graph is Bipartite or not by colowing the graph using two colors. If a given graph is 2-colorable, then it is Bipartite, otherwise not. See this for more details.

6) Map Coloring: Geographical maps of countries or states where no two adjacent cities cannot be assigned same color. Four colors are sufficient to color any map (See Four Color Theorem)

There can be many more applications: For example the below reference video lecture has a case study at 1:18.
Akamai runs a network of thousands of servers and the servers are used to distribute content on Internet. They install a new software or update existing softwares pretty much every week. The update cannot be deployed on every server at the same time, because the server may have to be taken down for the install. Also, the update should not be done one at a time, because it will take a lot of time. There are sets of servers that cannot be taken down together, because they have certain critical functions. This is a typical scheduling application of graph coloring problem. It turned out that 8 colors were good enough to color the graph of 75000 nodes. So they could install updates in 8 passes.

We will soon be discussing different ways to solve the graph coloring problem.

Lec 6 | MIT 6.042J Mathematics for Computer Science, Fall 2010 | Video Lecture

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Company Wise Coding Practice    Topic Wise Coding Practice

Writing code in comment? Please use, generate link and share the link here.

  • Rajat

    Here is my solution, plz suggest if you find out any possibilities of optimization :

    typedef struct _graph{
    int V;
    list *adj;

    void initGraph(
    graph & g,
    int nVer
    g.V = nVer;
    g.adj = new list[nVer];

    void deinitGraph(
    graph & g
    delete[] g.adj;

    void addEdge(
    graph & g,
    int first,
    int second
    g.adj[first].push_back( second );
    g.adj[second].push_back( first );

    int getColor(
    graph & g,
    int child,
    int *color
    int tempCol = color[child] + 1;
    int find = 0;
    while( !find )
    find = 1;
    list::iterator lt;
    for( lt = g.adj[child].begin(); lt != g.adj[child].end(); ++lt )
    if( color[*lt] == tempCol )
    find = 0;
    return tempCol;

    void findMinColor(
    graph g
    int *color = new int[g.V]();
    int *visited = new int[g.V]();
    int tCol = 0;
    stack bfs;
    bfs.push( 0 );
    color[0] = 1;
    while( !bfs.empty() )
    int v =;
    visited[v] = 1;
    list::iterator lt;
    for( lt = g.adj[v].begin(); lt != g.adj[v].end(); ++lt )
    if( !visited[*lt] )
    bfs.push( *lt );
    if( color[*lt] == 0 )
    if( color[v] == 1 )
    color[*lt] = color[v] + 1;
    color[*lt] = color[v] – 1;
    else if( color[*lt] == color[v] )
    color[*lt] = getColor( g, *lt, color );
    if( tCol < color[*lt] )
    tCol = color[*lt];
    for( int i = 0; i < g.V; i++ )
    cout << i << " color : " << color[i] << endl;
    cout << "Total Color : " << tCol << endl;
    delete[] visited;
    delete[] color;

    int main()
    graph g;

    initGraph( g, 3 );

    addEdge( g, 0, 1 );
    addEdge( g, 1, 2 );
    addEdge( g, 2, 0 );

    findMinColor( g );

    deinitGraph( g );


  • its_dark

    awesome !

  • bornHacker