Graph Coloring & Hamiltonian Cycles
Graph Coloring & Hamiltonian Cycles
Graph Coloring
• Graph Coloring is an assignment of colors (or any distinct marks) to the
vertices of a graph.
• A graph is said to be colored if a color has been assigned to each vertex in
such a way that adjacent vertices have different colors.
• Its also known as optimization decision problem
CAP538
Algorithm Design & Analysis
• The chromatic number m of a graph G is the smallest number of colors
with which it can be colored.
Graph Coloring & Hamiltonian Cycles
In the above example, the chromatic number is 4.
Examples Examples
1
3/2/2017
R G B a b
G B
d c
R
Red - 1
B
R G Green - 2
Blue - 3 • Four Color Problem:
R
– For every planar graph, the chromatic number is ≤ 4.
B
G
– The mathematicians, Appel and Haken proved that 4 colors are sufficient to
• A portion of the pruned state space tree produced using backtracking to do a 3-coloring color any graph with the help of computers.
of the graph.
• First solution is found at the shaded node. Each non-promising node is marked with a X.
2
3/2/2017
3
3/2/2017
4
3/2/2017
5
3/2/2017
Complexity: O(nmn)
6
3/2/2017
Hamiltonian Cycles
1 2 3 4 1 2 3
8 7 6 5 5 4
Hamiltonian Algorithm
MainAlgo
{
Create Adjacency Matrix G[1:n, 1:n] of a given graph G
Set x[1] = 1
Set x[2:n] = 0
Hamiltonian(2)
}
7
3/2/2017
Hamiltonian Algorithm
NextValue(K) // Generating a next vertex
{
Repeat
{
x[k] = (x[k] + 1) mod (n+1) // next vertex
if(x[k] = 0) then
Return
if(G[x[k-1], x[[k]] != 0) then
{
for j=1 to k-1 do
{ if(x[j] = x[k]) then // if current vertex not same
Break
}
if(j = k) then // vertex is distinct
{ if(k < n or (k = n and G[x[n], x[1]] != 0)) then
Return
}
}
} Until(false)
}