L23 Backtracking Graph Coloring Problem
L23 Backtracking Graph Coloring Problem
Backtracking
By
Dr. Ratnesh Litoriya
Medi-Caps University, Indore (M.P.)
5/6/2023
Backtracking
Backtracking is a technique used to solve
problems with a large search space, by
systematically trying and eliminating
possibilities.
It uses recursive calling to find the solution
by building a solution step by step increasing
values with time. It removes the solutions that
doesn't give rise to the solution of the
problem based on the constraints given to
solve the problem.
2
Grraph Coloring Problem
Given an undirected graph and a number
m, determine if the graph can be coloured
with at most m colours such that no two
adjacent vertices of the graph are colored
with the same color.
Here coloring of a graph means the
assignment of colors to all vertices.
Grraph Coloring Problem
Input:
Output:
An array color[V] that should have numbers from 1 to m. color[i] should
represent the color assigned to the ith vertex. The code should also return
false if the graph cannot be colored with m colors.
Grraph Coloring Problem
Example of a graph that can be coloured with 3 different
colours
Grraph Coloring Problem
Naive Approach:
Generate all possible configurations of colors.
Since each node can be coloured using any of the m
available colours, the total number of colour configurations
possible are m^V.
After generating a configuration of colour, check if the
adjacent vertices have the same colour or not.
If the conditions are met, print the combination and break
the loop.
This method is not efficient in terms of time complexity
because it finds all colors combinations rather than a single
solution.
Grraph Coloring Problem
Method 2: Backtracking.
Approach: The idea is to assign colors one by one to
different vertices, starting from the vertex 0.
Before assigning a color, check for safety by considering
already assigned colors to the adjacent vertices i.e 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.
Grraph Coloring Problem
Grraph Coloring Problem
Grraph Coloring Problem
Some important applications of graph coloring are as follows-
Map Coloring
Scheduling the tasks
Preparing Time Table
Assignment
Conflict Resolution
Sudoku
Grraph Coloring Problem
Complexity Analysis:
Time Complexity: O(m^V).
There are total O(m^V) combination of colors. So time
complexity is O(m^V). The upperbound time complexity
remains the same but the average time taken will be less.