Graph Coloring Using Back Tracking - Assss

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Algorithm and Analysis design

DEBRE BIRHAN UNIVERSITY


COLLEGE OF COMPUTING
DEPARTMENT OF COMPUTER SCIENCE
COURSE TITLE : Algorithm
TITLE :graph coloring using backtracking

Prepared by :Beka Tasew

Id:1883/13
Submitted to: MR BEKELE.M

April , 2024 GC

Prepared by Beki Page 1


Algorithm and Analysis design

1.1 Introduction
Graph coloring is used to identify independent objects in a set and has applications in a wide variety of
scientific and engineering problems. Optimal coloring of graphs is an NP-complete problem. Therefore
there exist many heuristics that attempt to obtain a near-optimal number of colors. In this content we will
see graph coloring using backtracking
1.2 Graph coloring using back tracking
1.2.1 Definition of Graph coloring
A graph is a collection of dots we call vertices some of which are connected by curves we call edges. The
relative location of the dots and the shape of the curves are not relevant, we are only concerned with whether
or not a given pair of dots is connected by a curve. Initially, we forbid edges from a vertex to itself and
multiple edges between two vertices. If G is a graph, then V(G) is its set of vertices and E(G) its set of
edges. We write |G| for the number of vertices in V(G) and |G| for the number of edges in E(G).
Graph coloring is an assignment of colors (any distance marks ) to the vertices of a graph. Strictly
speaking ,a coloring a proper coloring if no two adjacent vertices have the same color .in addition
the problem of assigning a color to each vertex of a graph such that no two neighbouring vertices share the
same color, is one of the most important problems in graph theory.
Example m=3 n=4

Let the maximum color m = 3, which indicates the maximum number of colors that can be used. the output
should be Node 0 -> color 1, Node 1 -> color 2, Node 2 -> color 3, Node 3 -> color 2.
What is M-Coloring Problem?
In the M-Coloring problem, our task is to find if it is possible to assign nodes of a given graph with
m different colors, such that no two adjacent vertices of the graph are of the same colors. If a solution exists,
then display which color is assigned to each vertex. To evaluate the graph coloring problem we use
backtracking algorithm .let we see
1.2.2 Backtracking algorithm
The idea is to assign color one by one to different vertices ,starting from vertex 0.before assigning a
color ,we check for safety by considering already assign columns to the address vertices .if we do not
a find color due to a clashes then we backtrack and return false.
Backtracking algorithm used brute force approach. Brute force in computer science refers to a
straightforward approach to problem-solving, directly addressing the problem's possible solutions without
applying any strategic logic or established algorithms. This method may involve guessing all possible
combinations until the correct one is found or systematically going through each option one by one.

Prepared by Beki Page 2


Algorithm and Analysis design

1.2.3 How Backtracking work ?


Backtracking is an algorithmic technique where the goal is to get all solutions to a problem using
the brute force approach. It consists of building a set of all the solutions incrementally. Since a problem
would have constraints, the solutions that fail to satisfy them will be removed.
It uses recursive calling to find a solution set by building a solution step by step, increasing levels with
time. In order to find these solutions, a search tree named state-space tree is used. In a state-space tree,
each branch is a variable, and each level represents a solution.
Example

A backtracking algorithm uses the depth-first search method. When it starts exploring the solutions, a
bounding function is applied so that the algorithm can check if the so-far built solution satisfies the
constraints. If it does, it continues searching. If it doesn’t, the branch would be eliminated, and the
algorithm goes back to the level before.
The backtracking algorithm is applied to some specific types of problems. For instance, we can use it to
find a feasible solution to a decision problem. It was also found to be very effective for optimization
problems.
1.2.3.1 Pseudocode
Graph coloring using backtracking is an algorithmic technique to color a graph with the
minimum number of colors without violating the condition that no two adjacent vertices have the
same color. Here’s a high-level description of how the backtracking algorithm works:
Algorithm:
1.Create a recursive function that takes current index, number of vertices and output color
array.
2.If the current index is equal to number of vertices. Check if the output color configuration is
safe, i.e check if the adjacent vertices do not have same color. If the conditions are met, print
the configuration and break.
3.Assign a color to a vertex (1 to m).
4.For every assigned color recursively call the function with next index and number of vertices

5.If any recursive function returns true break the loop and returns true. The backtracking
algorithm is efficient because it tries to build a solution incrementally and abandons a solution as
soon as it determines that the solution cannot possibly be completed to a valid solution.

Prepared by Beki Page 3


Algorithm and Analysis design

Here’s a simple a C++ for graph coloring using backtracking:

GraphColor(int k){ isSafe(int k,int c) {

for(int c=1;c<=m;c++); for(int i=0;i<n;i++){

if(isSafe(k,c)) { if(G[k][i]=2 && c==x[i]) {

X[k]=c; return false;

if((k+1)<n) }

GraphColor(k+1); }

else return true;

print x[];return; } }

}}

In this pseudocode, graphColor is the representation of the graph, k is a node that we are going
to color in this level of recursion , x[k]is an array to store colors assigned to vertices. The
isSafe function checks if it’s safe to color a vertex with a given color.

Example

Prepared by Beki Page 4


Algorithm and Analysis design

1.2.3.2 Complexity Analysis

The worst-case, average-case, and best-case scenarios for graph coloring can vary depending on several
factors, including the properties of the graph (such as its size, density, and structure) and the specific
algorithm used for coloring.
1. Worst-case :
In the worst-case scenario, the graph is typically dense, with many edges, and has a large number of
vertices. The worst-case time complexity for graph coloring using backtracking is exponential, specifically
O(mv), where V is the number of vertices and m is the number of colors available. This worst-case scenario
occurs when the algorithm must explore all possible color assignments for each vertex, leading to a
combinatorial explosion in the search space.
2. Average-case :
The average-case scenario depends heavily on the characteristics of the graphs being considered and the
specific algorithm employed. For randomly generated graphs or graphs with certain structures (e.g., trees,
sparse graphs), the average-case complexity may be significantly lower than the worst-case. Algorithms
that incorporate heuristics or optimization techniques may perform better on average for a wide range of
graphs.
3. Best-case :
The best-case scenario typically occurs when the graph is trivially colorable or has very few edges. In such
cases, the graph coloring algorithm may terminate quickly, possibly in constant time or linear time
However, it's important to note that the best-case scenario is not representative of typical instances, as real-
world graphs often have complex structures and require more computational effort to color.
The space complexity of graph coloring using backtracking depends on several factors, including the size
of the graph, the number of colors available, and the specific implementation details of the backtracking
algorithm.
In general, backtracking algorithms for graph coloring typically involve maintaining data structures to
represent the current state of the coloring process, such as:
1. Adjacency list or matrix: To represent the graph structure.
2. Coloring array: To store the colors assigned to each vertex.
3. Visited array: To keep track of which vertices have been visited during the backtracking process.
4. Stack or recursion: Backtracking algorithms often use a stack (or recursion) to manage the backtracking
process and store intermediate states.
The space complexity can be analyzed based on these data structures:
1. Adjacency list or matrix: Requires O(V + E) space, where V is the number of vertices and E is the number
of edges.
2. Coloring array: Requires O(V) space to store the color assigned to each vertex.
3. Visited array: Requires O(V) space to keep track of visited vertices.

Prepared by Beki Page 5


Algorithm and Analysis design

4. Stack or recursion: Consumes additional space on the call stack, proportional to the maximum depth of
recursion or the maximum size of the backtracking stack.
Therefore, the overall space complexity of graph coloring using backtracking can be approximated as
O(V + E), where V is the number of vertices and E is the number of edges. However, the actual space usage
may vary depending on implementation details and the specific characteristics of the input graph.
Overall, while the worst-case complexity of graph coloring algorithms can be daunting, practical
implementations often benefit from various optimizations and heuristics that improve performance on
average and real-world instances
1.2.3.3 Strength and weakness of graph coloring algorithm
Graph coloring it have it own strength and weakness when we compare from other algorithm. those are
Strengths:
Versatility: Graph coloring has diverse applications across multiple domains, including scheduling,
register allocation in compilers, map coloring, and solving various optimization problems.
Intuitive Concept: The concept of graph coloring is relatively straightforward, making it accessible to
understand and implement even for beginners.
Flexibility: Graph coloring algorithms can be adapted and extended to solve different variations of the
problem, such as vertex coloring, edge coloring, and face coloring.
Scalability: While the worst-case complexity of some graph coloring algorithms can be high, practical
implementations often perform efficiently on real-world instances, especially when coupled with heuristics
and optimization techniques.

Weaknesses:
Computational Complexity: In the worst-case scenario, graph coloring algorithms can have exponential
time complexity, especially when using exact methods like backtracking. This can make them impractical
for large or dense graphs.
Dependence on Input: The efficiency of graph coloring algorithms can vary significantly depending on
the characteristics of the input graph, such as size, density, and structure. Certain graph structures may pose
challenges or require specialized algorithms for effective coloring.
Optimality Concerns: While many graph coloring algorithms produce valid colorings, they may not
always find the optimal solution (i.e., the minimum number of colors required). Determining the chromatic
number of a graph (the minimum number of colors needed for a valid coloring) is NP-hard, making it
challenging to guarantee optimality in polynomial time.
Complexity in Practice: Implementing graph coloring algorithms in real-world applications may require
dealing with additional complexities, such as handling large datasets, integrating with other algorithms or
systems, and considering practical constraints specific to the application domain.

Prepared by Beki Page 6


Algorithm and Analysis design

1.2.3.4 Feedback from the gaps of the graph coloring


From the above I would like to show my opinion in the weakness and strengthen of graph coloring
algorithm
Efficiency: Backtracking is a depth-first search approach for solving constraint satisfaction problems
like graph coloring. It’s not the most efficient for large graphs due to its exponential time complexity, but
it’s a straightforward and complete method that guarantees a solution if one exists.
Optimization: Implementing heuristics can significantly improve the performance of a backtracking
algorithm. For example, using an ordering heuristic that chooses the next vertex to color based on the
number of its remaining uncolored neighbors (often called the “degree heuristic”) can reduce the search
space.
Practicality: While backtracking provides a clear framework for graph coloring, in practice, it’s more
suitable for smaller graphs or educational purposes. For larger graphs, more sophisticated algorithms like
greedy coloring or meta heuristic approaches might be necessary

In summary, the backtracking algorithm for graph coloring is a powerful tool for teaching and
understanding the principles of recursive problem-solving and constraint satisfaction. However, for real-
world applications, especially those involving large and complex graphs, alternative methods may be more
suitable. The study of graph coloring algorithms continues to be an active area of research, with ongoing
efforts to develop more efficient and scalable solutions.

Prepared by Beki Page 7


Algorithm and Analysis design

1.3 References
https://www.naukri.com/code360/library/m-coloring-problem
https://www.geeksforgeeks.org/graph-coloring-applications

Prepared by Beki Page 8

You might also like