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

Aph Coloring

Graph coloring is the problem of assigning colors to the vertices of a graph such that no two adjacent vertices share the same color. This problem has many applications such as scheduling exams in a university, assigning radio frequencies to cell towers, and chemical storage. The graph coloring problem is NP-complete, meaning there is no known efficient algorithm to find the minimum number of colors needed. However, greedy algorithms can provide approximate solutions by iteratively coloring vertices with the lowest available color.

Uploaded by

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

Aph Coloring

Graph coloring is the problem of assigning colors to the vertices of a graph such that no two adjacent vertices share the same color. This problem has many applications such as scheduling exams in a university, assigning radio frequencies to cell towers, and chemical storage. The graph coloring problem is NP-complete, meaning there is no known efficient algorithm to find the minimum number of colors needed. However, greedy algorithms can provide approximate solutions by iteratively coloring vertices with the lowest available color.

Uploaded by

VANSHITA GUPTA
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Graph coloring

What is Coloring?
• Graph Coloring is an assignment of colors (or any distinct marks) to
the vertices of a graph. Strictly speaking, a coloring is a proper
coloring if no two adjacent vertices have the same color.

f : V (G )  S
Graph Coloring
• Special case of labeling graph elements subject to
certain constraints.
– Traditionally, "colors" are used as labels.
• Several forms
– Vertex coloring. Color vertices of a graph such that
no two adjacent vertices share the same color.
– Edge coloring. Color edges such that no two adjacent
edges share the same color.
– Face coloring of a planar graph. Assign color to each
face or region so that no two faces that share a
boundary have the same color.
Vertex Coloring
• A vertex coloring is an assignment of labels or colors
to each vertex of a graph such that no edge connects
two identically colored vertices
Edge Coloring
• Similar to vertex coloring, except edges are color.
• Adjacent edges have different colors.
Solution TO Problem
• The other graph coloring problems like Edge Coloring (No vertex is
incident to two edges of same color) and Face Coloring (Geographical
Map Coloring) can be transformed into vertex coloring.
Contd…

• The simplest form is vertex coloring.


• Formally,
If C be the set of colors, then find a function
f : V→ C
– such that if there is an edge (vw), then f(v) ≠
f(w), and
– C is of minimum cardinality.
Definitions

• k-coloring. Coloring using at most k colors.


• Chromatic number of a graph (χ(G)). The smallest
number of colors needed to color a graph G.
• k-colorable. A graph that can be assigned a k-
coloring is k-colorable.
• Note: k-coloring partitions the vertex set into k
independent sets.
• no efficient algorithm available for coloring a graph with minimum
number of colors as the problem is a known NP Complete problem.
• There are approximate algorithms to solve the problem though.
• Greedy Algorithm to assign colors. It doesn’t guarantee to use
minimum colors, but it guarantees an upper bound on the number
of colors.
• The basic algorithm never uses more than d+1 colors where d is the
maximum degree of a vertex in the given graph.
The problem to find chromatic number of a given graph is NP Complete.
Greedy Approach
Greedy approach
1. Color a vertex with color 1.
2. Pick an uncolored vertex v. Color it with the lowest-numbered color
that has not been used on any previously-colored vertices adjacent to
v. (If all previously-used colors appear on vertices adjacent to v, this
means that we must introduce a new color and number it.)
3. Repeat the previous step until all vertices are colored
Example

v1 v2 v3 v4

v5 v6 v7 v8
Example v1 v2 v3 v4

v5 v6 v7 v8
Order of vertices
v1 v2 v3 v4 v5 v6 v7 v8
c1 c1 c2 c1 c2 c2 c3 c4
Number of colors required ≤ Maximum degree + 1
≤ max{deg(v1), deg(v2), deg(v3), deg(v4), deg(v5),
deg(v6), deg(v7), deg(v8)} + 1
≤ max{3, 3, 2, 2, 2, 4, 5, 3} + 1
≤5+1
≤ 6.
Color using greedy algorithm(C1,C2,C3,C4)
Greedy Approach
1. Color first vertex with first color.
2. Do following for remaining V-1 vertices.
a) Consider the currently picked vertex and color it with the
lowest numbered color that has not been used on any previously
colored vertices adjacent to it. If all previously used colors
appear on vertices adjacent to v, assign a new color to it.
Sequential Coloring – Greedy Approach
• sequentialColoringAlgorithm(graph = (V,E))
1. Put vertices in a certain order {v1,v2,…,vn}.
2. Put colors in a certain order {c1,c2,…,ck}.
3. for i = 1 to n
4. j = the smallest index of color that does
not appear in any neighbor of vi.
5. color(vi) = j.
If every node in G has degree ≤ d, then the
algorithm uses at most d + 1 colors for G.
Welsh–Powell algorithm
• If vertices are ordered according to their degrees
(decreasing), then

The number of colors needed to color the graph is


at most
max min(i, deg(vi ) 1)
i
Number of colors for graph at slide 42.
• Number of colors required ≤ max min(i, deg(vi ) 1)
i
• If vertices are arranged as {v1,v2,v3,v4,v5,v6,v7,v8}
≤ max{min(1,4), min(2,4), min(3,3), min(4,3),
min(5,3), min(6,5), min(7,6), min(8,4)}
≤ max{1,2,3,3,3,5,6,4}
≤ 6.
• Arrangement as per degrees {v7,v6,v1,v2,v8,v3,v4,v5}
≤ max{min(1,6), min(2,5), min(3,4), min(4,4),
min(5,4), min(6,3), min(7,3), min(8,3)}
≤ max{1,2,3,4,4,3,3,3}
≤ 4.
Example

v1 v2 v3 v4

v5 v6 v7 v8

v7 v6 v1 v2 v8 v3 v4 v5
c1 c2 c3 c1 c3 c2 c3 c2
m-Coloring Problem
• Given an undirected graph and a number m, determine if
the graph can be colored with at most m colors such that
no two adjacent vertices have the same color.
• Input:
– An adjacency matrix, graph[V][V] where V is the
number of vertices in the graph.
– An integer m which is maximum number of colors that
can be used.
• Output:
– An array color[V] containing colors assigned to all the
vertices in the range 1 to m. False will be returned, if
the graph cannot be colored with m colors.
Following is an example of graph that can be colored with 3
different colors
• Naive Algorithm
Generate all possible configurations of
colors and print a configuration that
satisfies the given constraints.
while there are untried configurations
{ generate the next configuration if
no adjacent vertices are colored with
same color
{ print this configuration; } }

There will be V^m configurations of colors.


.
• Backtracking Algorithm
The idea is to assign colors one by one to different vertices,
starting from the vertex 0. Before assigning a color, we check for
safety by considering already assigned colors to the adjacent
vertices. If we find a color assignment which is safe, we mark the
color assignment as part of solution. If we do not a find color
due to clashes then we backtrack and return false
Backtracking
1. If all colors are assigned,
2. Print vertex assigned colors.
3. Else
4. Trying all possible colors, assign a color to the
vertex.
5. If color assignment is possible, recursively assign
colors to next vertices.
6. If color assignment is not possible, de-assign
color, return False.
Example – Check for 3-colorability
v2 v3 v5 v6

7 c2 c1 c2
6 c3 c1 c2 c3 v7 v1 v0 v4
5 c2 c1 c2
0 1 2 3 4 5 6 7
4 c3 c1 c2 c3 0 0 1 1 0 1 1 1 0
3 c1 1 1 0 1 0 1 0 1 0
2 1 1 0 0 0 0 0 1
2 c3 c1 c2 c3 3 0 0 0 0 1 1 0 1
1 c2 c1 c2 4 1 1 0 1 0 0 0 0
5 1 0 0 1 0 0 0 0
0 c1 6 1 1 0 0 0 0 0 0
7 0 0 1 1 0 0 0 0
Applications
• Scheduling.
• Frequency Assignment.
• Register Allocation.
• Bipartite Graphs.
• Map Coloring, etc. v2 v3 v5 v6

v7 v1 v0 v4
• The graph coloring problem has huge number of applications.
• 1) Making Schedule or Time Table: Suppose we want to make am exam schedule for a
university. We have list different subjects and students enrolled in every subject. Many
subjects would have common students (of same batch, some backlog students, etc). How
do we schedule the exam so that no two exams with a common student are scheduled at
same time? How many minimum time slots are needed to schedule all exams? This
problem can be represented as a graph where every vertex is a subject and an edge
between two vertices mean there is a common student. So this is a graph coloring
problem where minimum number of time slots is equal to the chromatic number of the
graph.
• 2) Mobile Radio Frequency Assignment: When frequencies are assigned to towers,
frequencies assigned to all towers at the same location must be different. How to assign
frequencies with this constraint? What is the minimum number of frequencies needed?
This problem is also an instance of graph coloring problem where every tower represents
a vertex and an edge between two towers represents that they are in range of each
other.
• Suppose that you are responsible for scheduling times for lectures in
a university. You want to make sure that any two lectures with a
common student occur at different times to avoid a conflict. We could
put the various lectures on a chart and mark with an “X” any pair that
has students in common:

We can code each time with a color, for example 11:00-12:00 might be given the color green, and those lectures that
meet at this time will be colored green. The no-conflict rule then means that we need to color the vertices of our
graph in such a way that no two adjacent vertices (representing courses which conflict with each other) have the
same color
Example. Minimizing exam period in school. How to
schedule exams in minimum parallel sessions, where no
two concurrent exams have a common student?
Define 𝐺 𝑉, 𝐸 , where 𝑣 ∈ 𝑉 corresponds to course, and
𝑒 𝑢, 𝑣 ∈ 𝐸 iff courses 𝑢 and 𝑣 have a common student.
An independent set of vertices implies a parallel exam
session. χ 𝐺 is the smallest number of parallel sessions.
Example. Chemical storage. Store 𝑛 different chemicals.
The interaction between some pairs is explosive.
What is the smallest required number of compartments
in the storage? χ 𝐺 .

March 2014 Graph Coloring 39


• 3) Sudoku: Sudoku is also a variation of Graph coloring problem where
every cell represents a vertex. There is an edge between two vertices if
they are in same row or same column or same block.
• 4) Register Allocation: In compiler optimization, register allocation is the
process of assigning a large number of target program variables onto a
small number of CPU registers. This problem is also a graph coloring
problem.
• 5) Bipartite Graphs: We can check if a graph is Bipartite or not by coloring
the graph using two colors. If a given graph is 2-colorable, then it is
Bipartite, otherwise not. See this for more details.
• 6) Map Coloring: Geographical maps of countries or states where no two
adjacent cities cannot be assigned same color. Four colors are sufficient to
color any map (See Four Color Theorem)
Different orderings may yield smaller upper bounds.
Finding the best ordering is hard. Is there an ordering
yielding χ 𝐺 ? It can be shown that such exists.
Example. Register allocation and interval graphs.
Consider the registers used by a compiler, each has start
and end time. What is the smallest number of physical
registers that can be used?
Assign the symbols 𝑎, 𝑏, 𝑐, … to the registers in the
code, and draw their usage time intervals.
𝑏 𝑓
𝑎 𝑑 𝑒
𝑐 𝑔
Proposition. If 𝐺 is an interval graph then 𝜒 𝐺 = ω 𝐺 .
March 2014 Graph Coloring 41

You might also like