Graph Coloring
Graph Coloring
Graph Coloring
Pratishtha Pandey
3rd C.S.E
1
Coloring Graphs
This handout:
• Coloring maps and graphs
• Chromatic number
• Problem Definition
• Our Algorithms
• Applications of graph coloring
2
Coloring Graphs
Definition:A graph has been colored if a color has
been assigned to each vertex in such a way that
adjacent vertices have different colors.
3
Preliminaries
A Vertex
An Edge
4
Preliminaries
Adjacent vertices
5
Preliminaries
Adjacent vertices
6
Preliminaries
Adjacent vertices
B B and C are
adjacent to A
7
The Graph Coloring Problem
A Valid Coloring
Conflict
An Invalid Coloring
11
Coloring Graphs Naïvely
Question: Can an n-vertex graph be colored with k
colors? (This question is equivalent to the graph
coloring problem.)
Naïve algorithm: try all possible ways of assigning k
colors to the n vertices
If a valid coloring is found then answer yes. Otherwise,
answer no
Time: there are kn possible colorings to check
Example: Can a 30-vertex graph be colored with 3 colors?
330 possible colorings
106 colorings per second
6 MILLION YEARS !!!
12
Graph Coloring
A
As an example:
The vertices are B F
enumerated in order A-F
The colors are given in
order: R, G, B C E
13
Graph Coloring
B F
C E
14
Graph Coloring
B F
C E
15
Graph Coloring
B F
C E
16
Graph Coloring
B F
C E
17
Graph Coloring
B F
C E
18
Graph Coloring
B F
C E
19
Graph Coloring
B F
C E
20
Graph Coloring
B F
C E
21
Graph Coloring
B F
C E
22
Graph Coloring
B F
C E
23
A
Graph Coloring
B F
C E
24
Complexity of Graph Coloring
Complexity: Algorithm for finding the
optimal solution requires exponential time.
25
Coloring Planar Graphs
Definition: A graph is planar if it can be
drawn in a plane without edge-crossings.
29
Graph Theory for the Four Color Conjecture
30
Graph Theory for the Four Color Conjecture
31
Any Planar Map Is Four-Colorable
Planar graph - a graph drawn in a
plane without any of its edges
crossing or intersecting
Each vertex (A,B,C,D,E) represents a
region in a graph
Each edge represents regions that
share a boundary
32
An Application of Graph Coloring
Class Scheduling
Chem
Math Cannot be offered
at the same time
Art • Math – Bio
• Math – Chem
• Bio – Music
Music • Bio – Econ
Bio
• Music – Chem
• Music – Econ
• Chem – Art
• Art – Econ
Econ
33
An Application of Graph Coloring
Class Scheduling
Chem
Math Cannot be offered
at the same time
Art • Math – Bio
• Math – Chem
• Bio – Music
Music • Bio – Econ
Bio
• Music – Chem
• Music – Econ
• Chem – Art
• Art – Econ
Econ
34
An Application of Graph Coloring
Class Scheduling
Chem
Math Cannot be offered
at the same time
Art • Math – Bio
• Math – Chem
• Bio – Music
Music • Bio – Econ
Bio
• Music – Chem
• Music – Econ
• Chem – Art
• Art – Econ
Econ
35
An Application of Graph Coloring
Class Scheduling
Chem
Math Cannot be offered
at the same time
Art • Math – Bio
• Math – Chem
• Bio – Music
Music • Bio – Econ
Bio
• Music – Chem
• Music – Econ
• Chem – Art
• Art – Econ
Econ
36
An Application of Graph Coloring
Class Scheduling
Chem
Math
Art
Music
Bio
Goal : minimize the
number of periods (colors)
used
Econ
37
An Application of Graph Coloring
Class Scheduling
Chem
Math
Schedule
Art
• Period 1
• Period 2
Music
Bio
• Period 3
Econ
38
Optimization Problems
39
Weighted Graph Coloring
By state space tree
40
m-Coloring of a Graph
We represent graph by adjacency matrix
G[1:n,1:n),Where
G[i,j]=1 if (i,j) is an edge of G.
Otherwise, G[i,j]=0
The colors are represented by 1,2….,m.
and solutions are given by the n-tuple(x1,
…,xn),where xi is the color of node i.
Array x[]=0 firstly;
41
Algorithm mColoring(k)
// This algorithm was formed using the recursive backtracking
schema.The graph is represented by its boolean adjacency
matrix G[1:n,1:n].
// All asignment of 1,2,….,m to the vertices are assigned distinct
integers are printed. k is the index of the next vertex to color.
{
Repeat
{ NextValue(k); //Assign to x[k] a legal color.
if (x[k]=0) then return; // No new color possible
if(k=n) then // At most color have been used to
color
write (x[1:n]);
else mColoring(k+1);
} until (false);
}
42
Algorithm NextValue(k)
{
repeat
{
x[k]:=(x[k]+1) mod(m+1); //Next highest colour.
if (x[k]=0) then return; // All colors have been used.
for( j:=1 to n) do
{ // check if this color is distinct from adjacent colors.
if ((G[k,j]=!0) and (x[k]=x[j]))
// If (k,j) is and edge and if adj. vertices have same color.
then break;
}
if (j=n+1) then return;
} until (false); //otherwise find another color.
}
43
Time Bounded
The upper bound on the computing time of mColoring can
be derived at by noticing that the number of internal nodes
n-
in the state space tree is ∑i=0 mi .
At each1internal node, O(mn) time is spent by NextValue
to determine the children corresponding to legal colorings.
Hence the total time is bounded by
∑
n-
i=0 mi+1
n= ∑
n
i=1 mi
n = n(mn+1
-2)/(m-1)
1
=O(nmn)
44
4-Node Graph
1 2
3
4
45
X1 = 1 3
2
2 3 1 2
X 2= 1 3
1 2 1 2 3 1 3
X 3= 1 3 2 3 2
2 3 2 2 3 3 1 3 1 3 1 3 1 1 2 2 1 2
X 4=
46
Other Applications of Graph Coloring
CPU Registers Football games
48
Thank You
49