WINSEM2024-25 CSI2003 ETH VL2024250501994 2024-12-19 Reference-Material-III
WINSEM2024-25 CSI2003 ETH VL2024250501994 2024-12-19 Reference-Material-III
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
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
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:
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)