Chromatic Number of a Graph | Graph Colouring (original) (raw)

Last Updated : 23 Jul, 2025

Graph coloring is a fundamental concept in graph theory, and the chromatic number is a key parameter that quantifies the coloring properties of a graph. Let's go into the introductory aspects of the chromatic number.

**Graph coloring refers to the problem of **coloring vertices of a graph in such a way that **no two adjacent vertices have the **same **color. This is also called the **vertex coloring problem. If coloring is done using at most **m colors, it is called **m-coloring.

Table of Content

What is Chromatic Number?

The **chromatic number of a graph **G, denoted as **χ(G), is the **minimum number of **colors required to **color the **vertices of a graph **G in such a way that no two adjacent vertices share the same color. Formally, it is the smallest positive integer **k for which there exists a proper vertex coloring with k colors.

**Vertex Coloring:

Chromatic Number of **Cyclic Graph:

A graph is known as a cycle graph if it contains ‘n’ edges and ‘*n’ vertices (*n >= 3), which form a cycle of length ‘**n**’. The chromatic number in a cycle graph depends upon the **parity of cycle length:

Case 1 (**Odd length cycle): **χ(G) = 3.

Case 2(**Even length cycle): **χ(G) = 2.

Chromatic Number of Complete Graph:

The chromatic number of a complete graph is equal to the **number of vertices in the graph.

Chromatic Number of Bipartite Graph:

Non-empty bipartite graphs have a **chromatic number of **2.

since the two parts are each independent sets and can be colored with a single color. Conversely, if a graph can be **2-colored, it is **bipartite, since all edges connect vertices of different colors

Chromatic Number of Star Graph:

The chromatic number of a **star graph is 2.

Chromatic Number of Wheel Graph with more than 3 Vertices

The **chromatic number of a **wheel graph is 3 if the number of **vertices is even, and **4 if the number of vertices is odd.

Chromatic Number of Planar Graph:

A Planar Graph is a graph that can be drawn in a plane such that none of its edges cross each other.

χ(Planar Graph) <= 4

**χ(Trees) = 2.

Properites of Chromatic Number:

Here are some properties of the chromatic number:

Importance of Chromatic Number in Graph Theory:

Algorithm to Find Chromatic Numbers:

There are several algorithms to find the chromatic number of a graph, each with its own strengths and weaknesses in terms of complexity:

**1. Finding Chromatic Numbers using Greedy Algorithm:

Implementation:

C++ `

#include #include #include #include

using namespace std;

// Function to find the chromatic number using the greedy // coloring algorithm int greedyColoring(const vector<vector >& graph) { int n = graph.size(); vector colors(n, -1);

for (int v = 0; v < n; ++v) {
    unordered_set<int> usedColors;

    // Check neighbors and mark their colors as used
    for (int neighbor : graph[v]) {
        if (colors[neighbor] != -1) {
            usedColors.insert(colors[neighbor]);
        }
    }

    // Find the smallest available color
    for (int color = 1;; ++color) {
        if (usedColors.find(color)
            == usedColors.end()) {
            colors[v] = color;
            break;
        }
    }
}

// Find the maximum color used (chromatic number)
int chromaticNumber
    = *max_element(colors.begin(), colors.end()) + 1;
return chromaticNumber;

}

int main() { // Sample graph represented as an adjacency list vector<vector > graph = { { 1, 2, 3 }, { 0, 2 }, { 0, 1, 3 }, { 0, 2 } };

// Find and output the chromatic number
int chromaticNumber = greedyColoring(graph);
cout << "Chromatic Number: " << chromaticNumber << endl;

return 0;

}

Java

// Java Implementation

import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set;

public class GreedyColoring {

// Function to find the chromatic number using the greedy
// coloring algorithm
public static int greedyColoring(List<List<Integer>> graph) {
    int n = graph.size();
    int[] colors = new int[n];
    for (int i = 0; i < n; i++) {
        colors[i] = -1;
    }

    for (int v = 0; v < n; v++) {
        Set<Integer> usedColors = new HashSet<>();

        // Check neighbors and mark their colors as used
        for (int neighbor : graph.get(v)) {
            if (colors[neighbor] != -1) {
                usedColors.add(colors[neighbor]);
            }
        }

        // Find the smallest available color
        int color;
        for (color = 1; ; color++) {
            if (!usedColors.contains(color)) {
                colors[v] = color;
                break;
            }
        }
    }

    // Find the maximum color used (chromatic number)
    int chromaticNumber = 0;
    for (int color : colors) {
        chromaticNumber = Math.max(chromaticNumber, color);
    }
    return chromaticNumber + 1;
}

public static void main(String[] args) {
    // Sample graph represented as an adjacency list
    List<List<Integer>> graph = new ArrayList<>();
    graph.add(new ArrayList<>(List.of(1, 2, 3)));
    graph.add(new ArrayList<>(List.of(0, 2)));
    graph.add(new ArrayList<>(List.of(0, 1, 3)));
    graph.add(new ArrayList<>(List.of(0, 2)));

    // Find and output the chromatic number
    int chromaticNumber = greedyColoring(graph);
    System.out.println("Chromatic Number: " + chromaticNumber);
}

}

// This code is contributed by Tapesh(tapeshdu420)

Python

def greedy_coloring(graph): n = len(graph) colors = [-1] * n

for v in range(n):
    used_colors = set()

    # Check neighbors and mark their colors as used
    for neighbor in graph[v]:
        if colors[neighbor] != -1:
            used_colors.add(colors[neighbor])

    # Find the smallest available color
    color = 1
    while True:
        if color not in used_colors:
            colors[v] = color
            break
        color += 1

# Find the maximum color used (chromatic number)
chromatic_number = max(colors) + 1
return chromatic_number

Sample graph represented as an adjacency list

graph = [[1, 2, 3], [0, 2], [0, 1, 3], [0, 2]]

Find and output the chromatic number

chromatic_number = greedy_coloring(graph) print("Chromatic Number:", chromatic_number)

C#

using System; using System.Collections.Generic; using System.Linq;

public class GreedyColoring { // Function to find the chromatic number using the greedy // coloring algorithm public static int FindChromaticNumber(List<List> graph) { int n = graph.Count; int[] colors = new int[n]; for (int i = 0; i < n; i++) { colors[i] = -1; }

    for (int v = 0; v < n; v++)
    {
        HashSet<int> usedColors = new HashSet<int>();

        // Check neighbors and mark their colors as used
        foreach (int neighbor in graph[v])
        {
            if (colors[neighbor] != -1)
            {
                usedColors.Add(colors[neighbor]);
            }
        }

        // Find the smallest available color
        int color;
        for (color = 1; ; color++)
        {
            if (!usedColors.Contains(color))
            {
                colors[v] = color;
                break;
            }
        }
    }

    // Find the maximum color used (chromatic number)
    int chromaticNumber = colors.Max();
    return chromaticNumber + 1;
}

public static void Main(string[] args)
{
    // Sample graph represented as an adjacency list
    List<List<int>> graph = new List<List<int>>
    {
        new List<int> { 1, 2, 3 },
        new List<int> { 0, 2 },
        new List<int> { 0, 1, 3 },
        new List<int> { 0, 2 }
    };

    // Find and output the chromatic number
    int chromaticNumber = FindChromaticNumber(graph);
    Console.WriteLine("Chromatic Number: " + chromaticNumber);
}

}

JavaScript

// Function to find the chromatic number // using the greedy coloring algorithm function greedyColoring(graph) { const n = graph.length; const colors = new Array(n).fill(-1);

for (let v = 0; v < n; ++v) {
    const usedColors = new Set();

    // Check neighbors and mark 
    // their colors as used
    for (const neighbor of graph[v]) {
        if (colors[neighbor] !== -1) {
            usedColors.add(colors[neighbor]);
        }
    }

    // Find the smallest available
    // color
    let color = 1;
    while (true) {
        if (!usedColors.has(color)) {
            colors[v] = color;
            break;
        }
        color++;
    }
}

// Find the maximum color used
// (chromatic number)
const chromaticNumber = Math.max(...colors) + 1;
return chromaticNumber;

}

// Sample graph represented as an // adjacency list const graph = [ [1, 2, 3], [0, 2], [0, 1, 3], [0, 2] ];

// Find and output the chromatic number const chromaticNumber = greedyColoring(graph); console.log("Chromatic Number: " + chromaticNumber);

`

Output

Chromatic Number: 4

**2. Finding Chromatic Numbers using Backtracking Algorithm:

Implementation:

C++ `

#include #include #include

using namespace std;

// Function to check if it's safe to color a vertex with a // given color bool isSafe(int v, const vector<vector >& graph, const vector& color, int c) { for (int neighbor : graph[v]) { if (color[neighbor] == c) { return false; // If any adjacent vertex has the // same color, it's not safe } } return true; }

// Backtracking function to find a valid coloring bool graphColoringUtil(int v, const vector<vector >& graph, vector& color, int m) { if (v == graph.size()) { return true; // All vertices are colored, a solution // is found }

for (int c = 1; c <= m; ++c) {
    if (isSafe(v, graph, color, c)) {
        color[v] = c;

        // Recur for the next vertices
        if (graphColoringUtil(v + 1, graph, color, m)) {
            return true;
        }

        // Backtrack
        color[v] = 0;
    }
}

return false; // No solution found for this coloring

}

// Main function to find chromatic number int graphColoring(const vector<vector >& graph, int m) { int n = graph.size(); vector color(n, 0);

if (!graphColoringUtil(0, graph, color, m)) {
    cout << "No feasible solution exists";
    return 0;
}

// Print the solution
cout << "Vertex colors: ";
for (int c : color) {
    cout << c << " ";
}
cout << endl;

// Count unique colors to determine chromatic number
unordered_set<int> uniqueColors(color.begin(),
                                color.end());
return uniqueColors.size();

}

int main() { // Sample graph represented as an adjacency list vector<vector > graph = { { 1, 2, 3 }, { 0, 2 }, { 0, 1, 3 }, { 0, 2 } };

// Set the maximum number of colors
int maxColors = 3;

// Find and output the chromatic number
int chromaticNumber = graphColoring(graph, maxColors);
cout << "Chromatic Number: " << chromaticNumber << endl;

return 0;

}

Java

import java.util.*;

public class GraphColoring { // Function to check if it's safe to color a vertex with a given color static boolean isSafe(int v, List<List> graph, int[] color, int c) { for (int neighbor : graph.get(v)) { if (color[neighbor] == c) { return false; // If any adjacent vertex has the same color, it's not safe } } return true; }

// Backtracking function to find a valid coloring
static boolean graphColoringUtil(int v, List<List<Integer>> graph, int[] color, int m) {
    if (v == graph.size()) {
        return true; // All vertices are colored, a solution is found
    }

    for (int c = 1; c <= m; ++c) {
        if (isSafe(v, graph, color, c)) {
            color[v] = c;

            // Recur for the next vertices
            if (graphColoringUtil(v + 1, graph, color, m)) {
                return true;
            }

            // Backtrack
            color[v] = 0;
        }
    }

    return false; // No solution found for this coloring
}

// Main function to find chromatic number
static int graphColoring(List<List<Integer>> graph, int m) {
    int n = graph.size();
    int[] color = new int[n];

    if (!graphColoringUtil(0, graph, color, m)) {
        System.out.println("No feasible solution exists");
        return 0;
    }

    // Print the solution
    StringBuilder result = new StringBuilder("Vertex colors: ");
    for (int c : color) {
        result.append(c).append(" ");
    }
    System.out.println(result);

    // Count unique colors to determine chromatic number
    Set<Integer> uniqueColors = new HashSet<>();
    for (int c : color) {
        uniqueColors.add(c);
    }
    return uniqueColors.size();
}

// Main function
public static void main(String[] args) {
    // Sample graph represented as an adjacency list
    List<List<Integer>> graph = new ArrayList<>();
    graph.add(Arrays.asList(1, 2, 3));
    graph.add(Arrays.asList(0, 2));
    graph.add(Arrays.asList(0, 1, 3));
    graph.add(Arrays.asList(0, 2));

    // Set the maximum number of colors
    int maxColors = 3;

    // Find and output the chromatic number
    int chromaticNumber = graphColoring(graph, maxColors);
    System.out.println("Chromatic Number: " + chromaticNumber);
}

}

Python

from typing import List, Set

def is_safe(v, graph, color, c): for neighbor in graph[v]: if color[neighbor] == c: return False # If any adjacent vertex has the same color, it's not safe return True

def graph_coloring_util(v, graph, color, m): if v == len(graph): return True # All vertices are colored, a solution is found

for c in range(1, m + 1):
    if is_safe(v, graph, color, c):
        color[v] = c

        # Recur for the next vertices
        if graph_coloring_util(v + 1, graph, color, m):
            return True

        # Backtrack
        color[v] = 0

return False  # No solution found for this coloring

def graph_coloring(graph, m): n = len(graph) color = [0] * n

if not graph_coloring_util(0, graph, color, m):
    print("No feasible solution exists")
    return 0

# Print the solution
print("Vertex colors:", end=" ")
for c in color:
    print(c, end=" ")
print()

# Count unique colors to determine chromatic number
unique_colors = set(color)
return len(unique_colors)

if name == "main": # Sample graph represented as an adjacency list graph = [[1, 2, 3], [0, 2], [0, 1, 3], [0, 2]]

# Set the maximum number of colors
max_colors = 3

# Find and output the chromatic number
chromatic_number = graph_coloring(graph, max_colors)
print("Chromatic Number:", chromatic_number)

C#

using System; using System.Collections.Generic;

class Program { // Function to check if it's safe to color a vertex with // a given color static bool IsSafe(int v, List<List > graph, List color, int c) { foreach(int neighbor in graph[v]) { if (color[neighbor] == c) { return false; // If any adjacent vertex has // the same color, it's not // safe } } return true; }

// Backtracking function to find a valid coloring
static bool GraphColoringUtil(int v,
                              List<List<int> > graph,
                              List<int> color, int m)
{
    if (v == graph.Count) {
        return true; // All vertices are colored, a
                     // solution is found
    }

    for (int c = 1; c <= m; ++c) {
        if (IsSafe(v, graph, color, c)) {
            color[v] = c;

            // Recur for the next vertices
            if (GraphColoringUtil(v + 1, graph, color,
                                  m)) {
                return true;
            }

            // Backtrack
            color[v] = 0;
        }
    }

    return false; // No solution found for this coloring
}

// Main function to find chromatic number
static int GraphColoring(List<List<int> > graph, int m)
{
    int n = graph.Count;
    List<int> color = new List<int>(new int[n]);

    if (!GraphColoringUtil(0, graph, color, m)) {
        Console.WriteLine(
            "No feasible solution exists");
        return 0;
    }

    // Print the solution
    Console.Write("Vertex colors: ");
    foreach(int c in color) { Console.Write(c + " "); }
    Console.WriteLine();

    // Count unique colors to determine chromatic number
    HashSet<int> uniqueColors = new HashSet<int>(color);
    return uniqueColors.Count;
}

static void Main(string[] args)
{
    // Sample graph represented as an adjacency list
    List<List<int> > graph = new List<List<int> >{
        new List<int>{ 1, 2, 3 }, new List<int>{ 0, 2 },
        new List<int>{ 0, 1, 3 }, new List<int>{ 0, 2 }
    };

    // Set the maximum number of colors
    int maxColors = 3;

    // Find and output the chromatic number
    int chromaticNumber
        = GraphColoring(graph, maxColors);
    Console.WriteLine("Chromatic Number: "
                      + chromaticNumber);
}

}

JavaScript

// Function to check if it's safe to color a vertex with a given color function isSafe(v, graph, color, c) { for (let neighbor of graph[v]) { if (color[neighbor] === c) { return false; // If any adjacent vertex has the same color, it's not safe } } return true; }

// Backtracking function to find a valid coloring function graphColoringUtil(v, graph, color, m) { if (v === graph.length) { return true; // All vertices are colored, a solution is found }

for (let c = 1; c <= m; ++c) {
    if (isSafe(v, graph, color, c)) {
        color[v] = c;

        // Recur for the next vertices
        if (graphColoringUtil(v + 1, graph, color, m)) {
            return true;
        }

        // Backtrack
        color[v] = 0;
    }
}

return false; // No solution found for this coloring

}

// Main function to find chromatic number function graphColoring(graph, m) { const n = graph.length; const color = new Array(n).fill(0);

if (!graphColoringUtil(0, graph, color, m)) {
    console.log("No feasible solution exists");
    return 0;
}

// Print the solution
let result = "Vertex colors: ";
for (let c of color) {
    result += c + " ";
}
console.log(result);

// Count unique colors to determine chromatic number
const uniqueColors = new Set(color);
return uniqueColors.size;

}

// Main function function main() { // Sample graph represented as an adjacency list const graph = [[1, 2, 3], [0, 2], [0, 1, 3], [0, 2]];

// Set the maximum number of colors
const maxColors = 3;

// Find and output the chromatic number
const chromaticNumber = graphColoring(graph, maxColors);
console.log("Chromatic Number: " + chromaticNumber);

}

// Call the main function main();

`

Output

Vertex colors: 1 2 3 2 Chromatic Number: 3

**3. Finding Chromatic Numbers using Heuristic Algorithms:

**4. Finding Chromatic Numbers using Exact Algorithms:

Choosing the right algorithm for finding chromatic number depends on the specific graph:

Relation between chromatic number and chromatic polynomial:

The **Chromatic Number (χ) and **Chromatic Polynomial (P(G, λ)) of a graph are two fascinating concepts in graph theory, each revealing different aspects of a graph's colorability. While they seem unrelated at first glance, they share a deep and intriguing connection.

**Chromatic Number :

**Chromatic Polynomial****:**

Relation/Connection between Chromatic number and Chromatic polynomial

**Analogy:

Imagine the **Chromatic Number as the "**minimum number of paint buckets" you need to **color a graph. The **Chromatic Polynomial, on the other hand, is like a "**painting inventory" that tells you how many different ways you can paint the graph with different numbers of buckets available.

Introduction to Graph Coloring

M-Coloring Problem

Graph Coloring Using Greedy Algorithm

Edge Coloring of a Graph

Coloring a Cycle Graph

Chromatic Polynomial