Work
Work
The problem
Decide whether one can assign one of k colors to each node of a graph G = (N, E) such that adjacent
nodes are assigned different colors.
1
3
Example
4
Valid coloring
Invalid coloring
Search space
The search space is k^|N|.
Brute force
Basically the brute force algorithm consists on trying all possible combinations of colors and finding a
working one. This means going through all the possibly vast search space which is k^|N|.
Backtracking
The backtracking algorithm goes through the same search space but overall gets better performances,
due to pruning the search space with some criteria.
Backtracking can be seen as searching a tree for a particular goal. In this case the goal is: no two
adjacent nodes have the same color.
What it does is it starts with some initial assignment of a color to a node, then it goes one by one
through all the nodes and seeks for conflicts. When they are reached it changes the color of the node, in
order to eliminate them. When all conflicts for that node are solved it advances to the next one and
once more tries to solve them.
Example:
We have k = 3 and n = 4.
The algorithms starts by assigning node 1 a color, in this case for example
color red (for the sake of the example this is color 1).
1 assigned to red
2 assigned to red
Then node 2 is also assigned to red.
The method checks for connections, an edge, between node 1 and 2.
Since they are connected this implies a contradictory assignment of colors to the nodes.
So the backtracking algorithm assigns color number 2 (green) to node 2
and checks for conflicts with node 1. There are none and so we move on to node 3.
We start by coloring node 3 with red. When checking
for incompatibilities on edges with node 1, it promptly
tell us that node 1 and node 3 are connected, hence
2 assigned to green
they cannot be the same color, in this case red.
3 assigned to red
3 assigned to blue
4 assigned to red
Pseudo-code:
01 02 03 04 05 06 07 08 09 10 11 12 13 -
Complexity:
The complexity of this algorithm is relative to the specific graph and the order in which the nodes are
colored.
In the worst-case scenario, which does not occur particularly often, the algorithm is O(|E|x(k^|N|)).
So in practice, generally, the backtracking tends to behave much better than the worst-case scenario.
There are some variations of the backtracking algorithm (multilevel backtracking or reverse
backtracking, amongst others) which have been showed to be up to 66% faster than other algorithms
for this problem, such as the iterated greedy algorithm.
Another approach:
A good alternative approach to the problem would be to transform it in a SAT formulation and give to a
SAT solver to deal with.
SAT is in theory an NP-complete problem but in practice is the success story of Computer Science.
They have suffered tremendous evolution and been improved with magnificent algorithms in order to
find solutions to hard problems utterly fast.
The problem of graph coloring can be extremely easily transformed to SAT.
All we have to do is the following:
Given |N| nodes and k colors, create |N|xk variables: xij = 1 if and only if node i is assigned color j; 0
otherwise.
For each edge(u,v), require different assigned colors to u and v: 1 j k, (!xuj V !xvj)
Each node is assigned exactly once color: 1 i |N|, sum(j=1 to k) xij = 1.
Example:
1
3
Example
5 edges 5 conditions
(!x11 V!x21)and(!x12V!x22)and(!x13V!x23)and
(!x11 V!x31)and(!x12V!x32)and(!x13V!x33)and
(!x21 V!x31)and(!x22V!x32)and(!x23V!x33)and
(!x21 V!x41)and(!x22V!x42)and(!x23V!x43)and
(!x31 V!x41)and(!x32V!x42)and(!x33V!x43)and
Final Remarks
When it comes to graph coloring, the way to go is using a backtracking algorithm, from what I could
apprehend in my research.
Even so, if I had to do some work where dealing with the problem is a constant, I would rather go with
the SAT transformation technique, since SAT solvers provide great resources to deal with all kind of
problems, are constantly evolving, some even have great backtracking algorithms and on practice
provide great results.
References
For the purpose of this work the following sources were referred to:
https://fenix.tecnico.ulisboa.pt/downloadFile/848204501354765/logic-foundations.pdf
https://www.youtube.com/watch?v=Cl3A_9hokjU
http://www.slideshare.net/subhradeeptoton/backtrackin
https://secweb.cs.odu.edu/~zeil/cs361/web/website/Lectures/npprobs/pages/ar01s01s01.html
http://heuristicswiki.wikispaces.com/Graph+coloring
http://en.wikipedia.org/wiki/Graph_coloring#Exact_algorithms
http://en.wikipedia.org/wiki/Backtracking
http://www.cs.cornell.edu/courses/cs3110/2011sp/recitations/rec21-graphs/graphs.htm
http://www.cs.rit.edu/~vcss242/Lectures/03/backtracking-stu.pdf
http://faculty.kfupm.edu.sa/ics/darwish/stuff/ics353Handouts/Backtracking.pdf
Improving the Performance of Graph Coloring Algorithms through Backtracking, by Sanjukta
Bhowmick, Paul D. Hovland