0% found this document useful (0 votes)
152 views

Work

The document discusses the graph coloring problem, which involves assigning colors to nodes of a graph such that no adjacent nodes have the same color. It describes brute force and backtracking algorithms to solve this NP-hard problem. Backtracking prunes the search space by eliminating color assignments that create conflicts. The problem can also be transformed into a SAT problem and given to efficient SAT solvers. While backtracking performs well in practice, transforming to SAT may be preferable for large instances due to advances in SAT solving.

Uploaded by

Pedro
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
152 views

Work

The document discusses the graph coloring problem, which involves assigning colors to nodes of a graph such that no adjacent nodes have the same color. It describes brute force and backtracking algorithms to solve this NP-hard problem. Backtracking prunes the search space by eliminating color assignments that create conflicts. The problem can also be transformed into a SAT problem and given to efficient SAT solvers. While backtracking performs well in practice, transforming to SAT may be preferable for large instances due to advances in SAT solving.

Uploaded by

Pedro
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Graph Coloring

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

Unfortunately it is an NP-Hard problem.


It has a huge number of crucial day-to-day applications such as map coloring, making time tables or
mobile radio frequency assignment.
For the foreseeable future the application of this problem will stand the test of time.

Search space
The search space is k^|N|.

Search Space = 3x3x3x3=81

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

Next we try the second color on node 3. It is assigned to


green.
Checking for incompatibilities, the algorithm stalls on
the fact that there is an edge between 2 and 3 so this
means 3 cannot be green and it must be assigned a
different color, in this case blue (color 3).
3 assigned to green

Now there are no incompatibilities and we move onto node 4.

3 assigned to blue

4 assigned to red

This node is assigned to red and since there are no


between 1 and 4, it is just fine.
Since node 2 and 3 have different colors from node 4
the verification returns nothing bad.
And so we have an answer to the question. This graph
can be colored with 3 colors.

Pseudo-code:
01 02 03 04 05 06 07 08 09 10 11 12 13 -

color (current_colors, graph)


evaluate the current_colors
if the coloring is complete (all nodes colored)
return current_colors
else
choose an uncolored node
generate new colorings based on that node getting every possible color
for each new coloring c
if the coloring is valid
solution = color (c, graph)
if the call did not fail
return solution
return failure

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:

Each edge(u,v) different assigned colors to u and v

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

Each node is assigned exactly one color:


(x11Vx12Vx13)and(!x11V!x12)and(!x11V!x13)and(!x12V!x13)and
(x21Vx22Vx23)and(!x21V!x22)and(!x21V!x23)and(!x22V!x23)and
(x31Vx32Vx33)and(!x31V!x32)and(!x31V!x33)and(!x32V!x33)and
(x41Vx42Vx43)and(!x41V!x42)and(!x41V!x43)and(!x42V!x43)
And we feed the SAT solver these conditions. If the problem is SAT then k-colors are enough to fill the
graph. UNSAT k-colors cannot color the graph.

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

You might also like