0% found this document useful (0 votes)
14 views4 pages

Graph Coloring

The document presents a C program to solve the Graph Colouring Problem using a backtracking algorithm. It details the algorithm's functions, time complexity of O(m^V × V), and space complexity of O(V). The program attempts to assign colors to graph vertices while ensuring that adjacent vertices do not share the same color.

Uploaded by

ankurmondal039
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)
14 views4 pages

Graph Coloring

The document presents a C program to solve the Graph Colouring Problem using a backtracking algorithm. It details the algorithm's functions, time complexity of O(m^V × V), and space complexity of O(V). The program attempts to assign colors to graph vertices while ensuring that adjacent vertices do not share the same color.

Uploaded by

ankurmondal039
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/ 4

Experiment No – 19

Write a C program to solve Graph Colouring Problem


Solution: -
19.1 Algorithm
function isSafe(v, graph, color, c):
for i from 0 to V-1:
if graph[v][i] == 1 and color[i] == c:
return false
return true

function solve(v, graph, color, m):


if v == V:
return true

for c from 1 to m:
if isSafe(v, graph, color, c):
color[v] = c
if solve(v + 1, graph, color, m):
return true
color[v] = 0 // Backtrack

return false

function graphColoring(graph, m):


color = array of size V initialized to 0
if solve(0, graph, color, m) == false:
print "Solution does not exist"
else:
print "Assigned Colors:", color
19.2 Algorithmic paradigm: - Backtracking
19.3 Complexity analysis: -
Time complexity
The time complexity of the graph coloring algorithm using backtracking primarily depends
on how many color assignments the algorithm attempts in the worst case.
Let’s say:
• V is the number of vertices in the graph.
• m is the number of available colors.
At each vertex, the algorithm tries all m possible colors. It does this recursively for all V
vertices. So, in the worst case (without any pruning), the total number of color combinations
the algorithm may explore is m^V. Therefore, the worst-case time complexity is O(m^V).
However, not all of these m^V combinations are fully explored. The function isSafe() checks
whether assigning a color to a vertex violates the coloring rule (i.e., adjacent vertices must
not have the same color). If it detects a violation, the current path is pruned, and the function
backtracks. This pruning reduces the actual number of recursive calls in many practical cases,
but it doesn’t change the worst-case bound.
The isSafe() function runs in O(V) time for each call since it checks all adjacent vertices of a
given vertex. Since it's called in each recursive step for up to m colors, the overhead of safety
checking contributes an additional multiplicative factor. So, the overall worst-case time
complexity can be expressed more accurately as O(m^V × V).

Space complexity
In terms of space complexity, the algorithm uses an array of size V to store color
assignments, so the space complexity is O(V). There is also recursive stack space used during
backtracking, which can go up to depth V, hence additional O(V) space. So, total space
complexity is O(V).
19.4 Source code
Output

You might also like