0% found this document useful (0 votes)
30 views13 pages

Backtracking

Uploaded by

sourish.js2021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views13 pages

Backtracking

Uploaded by

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

Graph Coloring using Backtracking

Backtracking

• In many real world problems, a solution can be obtained by


exhaustively searching through a large but finite number of
possibilities. Hence, the need arose for developing
systematic techniques of searching, with the hope of cutting
down the search space to possibly a much smaller space.

• Here, we present a general technique for organizing the


search known as backtracking. This algorithm design
technique can be described as an organized exhaustive
search which often avoids searching all possibilities.
The 3-Coloring Problem

• Given an undirected graph G=(V, E), it is required to color


each vertex in V with one of three colors, say 1, 2, and 3,
such that no two adjacent vertices have the same color. We
call such a coloring legal; otherwise, if two adjacent vertices
have the same color, it is illegal.
• A coloring can be represented by an n-tuple (c1, c2, …, cn)
such that ci{1, 2, 3}, 1in.
• For example, (1, 2, 2, 3, 1) denotes a coloring of a graph with
five vertices.
The 3-Coloring Problem

• There are 3n possible colorings (legal and illegal) to color a


graph with n vertices.
• The set of all possible colorings can be represented by a
complete ternary tree called the search tree. In this tree,
each path from the root to a leaf node represents one
coloring assignment.
• An incomplete coloring of a graph is partial if no two
adjacent colored vertices have the same color.
• Backtracking works by generating the underlying tree one
node at a time.
• If the path from the root to the current node corresponds
to a legal coloring, the process is terminated (unless more
than one coloring is desired).
The 3-Coloring Problem

• If the length of this path is less than n and the


corresponding coloring is partial, then one child of the
current node is generated and is marked as the current
node.
• If, on the other hand, the corresponding path is not
partial, then the current node is marked as a dead node
and a new node corresponding to another color is
generated.
• If, however, all three colors have been tried with no
success, the search backtracks to the parent node
whose color is changed, and so on.
The 3-Coloring Problem

• Example:
a

b c

d e
The 3-Coloring Problem

There are two important observations to be noted, which


generalize to all backtracking algorithms:

(1) The nodes are generated in a depth-first-search manner.

(2) There is no need to store the whole search tree; we only


need to store the path from the root to the current active
node. In fact, no physical nodes are generated at all; the
whole tree is implicit. We only need to keep track of the color
assignment.
The 3-Coloring Problem
Recursive Algorithm
Input: An undirected graph G=(V, E).
Output: A 3-coloring c[1…n] of the vertices of G, where each c[j] is 1, 2, or 3.
1. for k1 to n
2. c[k]0;
3. end for;
4. flagfalse;
5. graphcolor(1);
6. if flag then output c;
7. else output “no solution”;

graphcolor(k)
1. for color=1 to 3
2. c[k]color;
3. if c is a legal coloring then set flag true and exit;
4. else if c is partial then graphcolor(k+1);
5. end for;
The 3-Coloring Problem
Iterative Algorithm
Input: An undirected graph G=(V, E).
Output: A 3-coloring c[1…n] of the vertices of G, where each c[j] is 1, 2, or 3.
1. for k1 to n
2. c[k]0;
3. end for;
4. flagfalse;
5. k1;
6. while k1
7. while c[k]2
8. c[k]c[k]+1;
9. if c is a legal coloring then set flagtrue and exit from the two while loops;
10. else if c is partial then kk+1;
11. end while;
12. c[k]0;
13. kk-1;
14. end while;
15. if flag then output c;
16. else output “no solution”;
• 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 of the graph are
colored with same color. Here coloring of a graph means assignment of colors to
all vertices.
• Input:
1) A 2D array graph[V][V] where V is the number of vertices in graph and graph[V]
[V] is adjacency matrix representation of the graph. A value graph[i][j] is 1 if there
is a direct edge from i to j, otherwise graph[i][j] is 0.
2) An integer m which is maximum number of colors that can be used.
• Output:
An array color[V] that should have numbers from 1 to m. color[i] should represent
the color assigned to the ith vertex. The code should also return false if the graph
cannot be colored with m colors.
• #include<stdio.h>
int G[50][50],x[50]; //G:adjacency matrix,x:colors
void next_color(int k){
int i,j;
x[k]=1; //coloring vertex with color1
for(i=0;i<k;i++)

• { //checking all k-1 vertices-backtracking


if(G[i][k]!=0 && x[k]==x[i]) //if connected and has same color
x[k]=x[i]+1; //assign higher color than x[i]
}
}


• int main(){
int n,e,i,j,k,l;
printf("Enter no. of vertices : ");
scanf("%d",&n); //total vertices
printf("Enter no. of edges : ");
scanf("%d",&e); //total edges
for(i=0;i<n;i++)
for(j=0;j<n;j++)
G[i][j]=0; //assign 0 to all index of adjacency matrix

printf("Enter indexes where value is 1-->\n");


for(i=0;i<e;i++){
scanf("%d %d",&k,&l);
G[k][l]=1;
G[l][k]=1;
}
• for(i=0;i<n;i++)
next_color(i); //coloring each vertex


printf("Colors of vertices -->\n");
for(i=0;i<n;i++) //displaying color of each
vertex
printf("Vertex[%d] : %d\n",i+1,x[i]);

return 0;
}

You might also like