0% found this document useful (0 votes)
196 views11 pages

L23 Backtracking Graph Coloring Problem

The document discusses solving graph coloring problems using backtracking. It explains the graph coloring problem, provides an example, and describes solving it using backtracking and recursively trying color assignments until a valid solution is found or no assignments are possible, at which point it backtracks.

Uploaded by

Shivansh Rag
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
196 views11 pages

L23 Backtracking Graph Coloring Problem

The document discusses solving graph coloring problems using backtracking. It explains the graph coloring problem, provides an example, and describes solving it using backtracking and recursively trying color assignments until a valid solution is found or no assignments are possible, at which point it backtracks.

Uploaded by

Shivansh Rag
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

DAA (Unit-5)

Solving problems using

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:

 A 2D array graph[V][V] where V is the number of vertices in graph and


graph[V][V] is an adjacency matrix representation of the graph. A value
graph[i][j] is 1 if there is a direct edge from i to j, otherwise graph[i][j] is 0.

 An integer m is the maximum number of colors that can be used.

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.

 Space Complexity: O(V).


Recursive Stack of graphColoring(…) function will require
O(V) space.

You might also like