0% found this document useful (0 votes)
13 views17 pages

WINSEM2024-25 CSI2003 ETH VL2024250501994 2024-12-19 Reference-Material-III

The document discusses the Graph Colouring Problem, which involves coloring the nodes of a graph using a limited number of colors such that no two adjacent nodes share the same color. It explains the backtracking algorithm used to solve this problem, including the steps taken to color each vertex and the time complexity involved. Additionally, it briefly covers the Subset-Sum Problem, outlining a method to find subsets of integers that sum to a given target, along with its complexity analysis.
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
0% found this document useful (0 votes)
13 views17 pages

WINSEM2024-25 CSI2003 ETH VL2024250501994 2024-12-19 Reference-Material-III

The document discusses the Graph Colouring Problem, which involves coloring the nodes of a graph using a limited number of colors such that no two adjacent nodes share the same color. It explains the backtracking algorithm used to solve this problem, including the steps taken to color each vertex and the time complexity involved. Additionally, it briefly covers the Subset-Sum Problem, outlining a method to find subsets of integers that sum to a given target, along with its complexity analysis.
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/ 17

Graph Colouring Problem

• Graph colouring problem is a classical


combination problem.A graph G with n nodes
and a positive integer m are given .Using m
colours only, to colour all the nodes of graph G in
such a way that no two adjacent node have the
same colour. This problem is called the m
colouring decision problem. The integer m is
called the chromatic number of the graph.
Graph-Colour Problem
Example 1. Colour the following graph with minimum
no of distinct colours using backtracking approach.

A B C

E D
Graph-Colour Problem
• Consider the vertex A as the starting node of the
implicit tree and colour the nodes in the following
way
• Let C= Set of different colours used and S= Set of
vertices having colored .Both are initially empty
STEP 1:Colour vertex A with a colour say,Black.
C= C U {Black}={φ} U {Black}={Black}
A
S={S} U {A}={φ}U {A} = {A}
Explore A. B E
Graph-Colour Problem
Step 2. Colour vertex B.Colour B with a new colour say, Green as it is
adjacent of A and there is only one colour in C.
A
C= {Black, Green} , S={A,B}
Explore B. B E

C D E

Step 3. Consider vertex C.Colour C taking from the colour set C if


possible .Black is taken since it is not afjacent to A.
C={black,green},No new colour is considered .No change in
S={A,B,C}.Explore C. A

B E

C D E
D
Graph-Colour Problem
A
Step 4. Consider vertex D.Colour D taking
from the colour set if possible .D is adjacent B E
to both vertices B and C.Two colous are
C D E
there and they have been used for these
two vertices.Take a new colour say , D
Red to colour D. C= {black,green, red},
S={A,B,C,D}.Explore D.
However it is not possible. A
All the vertices have not been traversed .
B E
E is remained. Backtrack to B to traverse E.
C D E

D
Graph-Colour Problem
Step5.Consider vertex E.Colour E taking from these colour set
C if possible. E is adjacent to both vertices A and B.Their
colours cannot be used .But other colour Red can be A
considered .
B E
C={black,red,green} , S={A,B,C,D,E}
C D E

Thus, the given graph after


D
colouring will be
Black Green Black
A B C

Red E Red D
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 assignments of
• // 1, 2, . . . . . , m to the vertices of the graph such that adjacent
vertices are assigned distinct integers are printed. k is the index of the
next vertex to color.
•{
• repeat
• { // Generate all legal assignments for x[k].
• 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 m colors have been
• // used to color the n vertices.
• write (x [1: n]);
• else mcoloring (k+1);
• } until (false);
Algorithm NextValue (k)
• // x [1] , . . . . x [k-1] have been assigned integer values in the range [1, m] such
that adjacent vertices have distinct integers. A value for x [k] is determined in the
range [0, m].x[k] is assigned the next highest numbered color while maintaining
distinctness from the adjacent vertices of vertex k. If no such color exists, then x
[k] is 0.
•{
• repeat
•{
• x [k]: = (x [k] +1) mod (m+1) // Next highest color.
• 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 the same color. then
break;
•}
• if (j = n+1) then return; // New color found
• } until (false); // Otherwise try to find another color.
Analysis:

• Total time required by the algorithm for


n vertices is T(n)=O(mn).
• If m=n then T(n)=O(nn)
• It requires exponential time.
Subset-Sum Problem
• find a subset of a given set A = {a1, . . . , an} of n
positive integers whose sum is equal to a given
positive integer d.
• For example, for A = {1, 2, 5, 6, 8} and d = 9, there
are two solutions:{1, 2, 6} and {1, 8}.
• It is convenient to sort the set’s elements in
increasing order.
ie a1< a2 < . . . < an.
state-space tree
• The state-space tree can be constructed as a binary
tree
• The root of the tree represents the starting point
• Its left and right children represent inclusion and
exclusion of a1 in a set being sought.
• Similarly, going to the left from a node of the first
level corresponds to inclusion of a2 while going to
the right corresponds to its exclusion, and so on.
• Thus, a path from the root to a node on the ith level
of the tree indicates which of the first i numbers have
been included in the subsets represented by that
node.
• record the value of s, the sum of these
numbers, in the node
• If s is equal to d, we have a solution to the
problem
• If s is not equal to d, we can terminate the
node as nonpromising if either of the following
two inequalities holds:
s + ai+1> d (the sum s is too large),
𝑛
s + 𝑗=𝑖+1 𝑎𝑗 ≺ d
σ
A = {3, 5, 6, 7} and d = 15
0 W/o 3
With 3

0
3 W/o 5
With 5 W/o 5 With 5

5 0
8 3
W/o 6 With 6 W/o 6 With 6 W/o 6 0+13<15
With 6

14 8 9 3 11 5
14+7>15
With 7 W/o 7 9+7>15 3+7<15 11+7>15 5+7<15

15 8

solution 8<15
void subset_sum(int list[], int sum, int starting_index, int target_sum)
{
if( target_sum == sum )
{
subset_count++;
if(starting_index < list.length)
subset_sum(list, sum - list[starting_index-1],
starting_index,target_sum);
}
else
{
for( int i = starting_index; i < list.length; i++ )
{
subset_sum(list, sum + list[i], i + 1, target_sum);
}
}
}
• Complexity
• The tree generated is binary tree
• At each stage ,a node generates two child node.
• Therefore the time complexity is O(2n)

You might also like