100% found this document useful (1 vote)
173 views8 pages

Graph Coloring & Hamiltonian Cycles

Graph coloring is assigning colors to vertices of a graph such that no two adjacent vertices have the same color. The minimum number of colors needed is called the chromatic number. Map coloring can be modeled as graph coloring by representing regions as vertices and borders as edges. The Four Color Theorem states that any planar graph can be colored with four or fewer colors. Graph coloring algorithms use backtracking to systematically try all possible colorings.

Uploaded by

aman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
173 views8 pages

Graph Coloring & Hamiltonian Cycles

Graph coloring is assigning colors to vertices of a graph such that no two adjacent vertices have the same color. The minimum number of colors needed is called the chromatic number. Map coloring can be modeled as graph coloring by representing regions as vertices and borders as edges. The Four Color Theorem states that any planar graph can be colored with four or fewer colors. Graph coloring algorithms use backtracking to systematically try all possible colorings.

Uploaded by

aman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

3/2/2017

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

Proper 6-coloring Optimal 4-coloring

A Graph for which there is no solution to the 2-Coloring Problem

1
3/2/2017

State Space Tree Coloring Planar Graphs


 A tree of All possibilities • Definition:
 Each possible color is tried for vertex vi at level i such that no two A graph is said to be planar iff it can be drawn in a plane in such
adjacent vertices are of the same color a way that no two edges cross each other.

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.

Map Coloring Map Coloring


Suppose removed all borders but still wanted to see all
Consider a Fictional Continent. the countries. 1 color insufficient ?

2
3/2/2017

Map Coloring Map Coloring


So add another color. So add another color.
Try to fill in every country with one of the two colors. Try to fill in every country with one of the two colors.

Map Coloring Map Coloring


So add another color. So add another color.
Try to fill in every country with one of the two colors. Try to fill in every country with one of the two colors.

3
3/2/2017

Map Coloring Map Coloring

PROBLEM: Two adjacent countries forced to have So add another color:


same color. Border unseen.

Map Coloring Map Coloring

With 4 colors, could do it.


Insufficient. Need 4 colors because of this country.

4
3/2/2017

Map Coloring Map Coloring to Graph Coloring


• Color a map such that two regions with a common border are
Map (top) and its Planar Graph representation (bottom)
assigned different colors.

• Each map can be represented by a graph:


– Each region of the map is represented by a vertex
– Edges connect two vertices if the regions represented by these
vertices are adjacent.
• The resulting graph is called the dual graph of the map.

5
3/2/2017

Map Coloring Graph Coloring Algorithm


MainAlgo
{ Create Adjacency Matrix G[1:n, 1:n] of a given graph G
Set x[1:n] = 0
mColoring(1)
Now the map can be Return
colored as shown here: }
mColoring(k) // k – Index of the next vertex to color
{
Repeat
{ NextValue(k)
if(x[k] = 0) then // No New color possible
return
Four Colors are chosen as:- if(k = n) then // All vertices colored
Red, Green, Blue and Yellow write ‘x[1:n]’
else
mColoring(k+1)
} Until(false)
}

Graph Coloring Algorithm Hamiltonian Cycles


NextValue(k) // Generating a next color
{  Let G = {V, E} be a connected graph with n vertices.
Repeat
{
x[k] = (x[k] + 1) mod (m+1) // next higher color  Hamiltonian Path:
if(x[k] = 0) then // All colors used
 A path which visits every vertex in a graph G exactly once.
return
for j = 1 to n do
{ // check for adjacent node color
if(G[k, j] != 0 and x[k] = x[j])
 Hamiltonian Circuit/Cycle:
Break  Also known as a Round-Trip Path.
}
if(j = n+1) then // new color found  It was proposed by William Hamilton
Return
} Until(false)  It is a cycle which visits every vertex exactly once, except for the first
} vertex, which is also visited at the end of the cycle.

Complexity: O(nmn)

6
3/2/2017

Hamiltonian Cycles

 In other words, more formally,


 If a Hamiltonian Cycle begins at some vertex v1 Є G and the vertices
of G are visited in the order v1, v2, …., vn+1, then the edges (vi, vi+1) are
in E, 1 ≤ i ≤ n and vi are distinct except for v1 and vn+1 which are equal.

1 2 3 4 1 2 3

8 7 6 5 5 4

Fig-1: G1 contains Hamiltonian Fig-2: G2 contains no


Cycle: 1, 2, 8, 7, 6, 5, 4, 3, 1 Hamiltonian Cycle

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)
}

Hamiltonian(k) // Finds the Hamiltonian cycles of graph G


{
Repeat
{ NextValue(k)
if(x[k] = 0) then
Return
if(k = n) then
write ‘x[1:n]’
else
Hamiltonian(k+1)
} Until(false)
}

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)
}

You might also like