VOOZH about

URL: https://www.geeksforgeeks.org/dsa/graph-coloring-applications/

⇱ Introduction to Graph Coloring - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Introduction to Graph Coloring

Last Updated : 23 Jul, 2025

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

👁 Graph-Coloring-copy

The minimum number of colors needed to color a graph is called its chromatic number. For example, the following can be colored a minimum of 2 colors.

👁 chromatic-number-of-cycle-graph-example
Example of Chromatic Number

The problem of finding a chromatic number of a given graph is NP-complete.

Graph coloring problem is both, a decision problem as well as an optimization problem.

  • A decision problem is stated as, "With given M colors and graph G, whether a such color scheme is possible or not?".
  • The optimization problem is stated as, "Given M colors and graph G, find the minimum number of colors required for graph coloring."

Assign colors one by one to different vertices, starting from vertex 0. Before assigning a color, check if the adjacent vertices have the same color or not. If there is any color assignment that does not violate the conditions, mark the color assignment as part of the solution. If no assignment of color is possible then backtrack and return false.

Follow the given steps to solve the problem:

  • Create a recursive function that takes the graph, current index, number of vertices, and color array.
  • If the current index is equal to the number of vertices. Print the color configuration in the color array.
  • Assign a color to a vertex from the range (1 to m).
    • For every assigned color, check if the configuration is safe, (i.e. check if the adjacent vertices do not have the same color) and recursively call the function with the next index and number of vertices else return false
    • If any recursive function returns true then break the loop and return true
    • If no recursive function returns true then return false

Below is the implementation of the above approach:


Output
Solution Exists: Following are the assigned colors
 1 2 3 2 
  • Design a timetable.
  • Sudoku.
  • Register allocation in the compiler.
  • Map coloring.
  • Mobile radio frequency assignment.

Related Articles:

Comment
Article Tags: