0% found this document useful (0 votes)
5 views3 pages

DAA Lab WEEK - 8

The document outlines an experiment focused on graph coloring using backtracking to ensure no two adjacent vertices share the same color. It describes the algorithm and provides a C program that implements the graph coloring technique, including input for the number of vertices and edges. The output demonstrates the chromatic number and total solutions for a sample graph with four vertices.

Uploaded by

Devabn Nirmal
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)
5 views3 pages

DAA Lab WEEK - 8

The document outlines an experiment focused on graph coloring using backtracking to ensure no two adjacent vertices share the same color. It describes the algorithm and provides a C program that implements the graph coloring technique, including input for the number of vertices and edges. The output demonstrates the chromatic number and total solutions for a sample graph with four vertices.

Uploaded by

Devabn Nirmal
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/ 3

Experiment 8

Aim: Write a program to colour the nodes in the graph such that no two adjacent
can have the same colour using backtracking.

Description: Graph colouring is the procedure of assignment of colours to each


vertex of a graph G such that no adjacent vertices get same colour.
The objective is to minimize the number of colours while colouring a graph.
The smallest number of colours required to colour a graph G is called its chromatic
number of that graph.
The steps required to colour a graph G with n number of vertices are as
follows:
 Arrange the vertices of the graph in some order.
 Choose the first vertex and colour it with the first colour.
 Choose the next vertex and colour it with the lowest numbered colour
that has not been coloured on any vertices adjacent to it. If all the
adjacent vertices are coloured with this colour, assign a new colour to it.
Repeat this step until all the vertices are coloured.
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
int m,n;
int c=0;
int sol=0;
int g[10][10];
int x[10];
void nextvalue(int k);
void graphcoloring(int k);
void main()
{
int i,j,t;
printf("\n enter the number of vertices in a graph: " );
scanf("%d", &n);
printf("\n enter graph edges\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&g[i][j]);
}
}
printf("\n All possible solutions are\n");
for(m=1;m<=n;m++)
{
if(c==1)
break;
graphcoloring(1);
}
printf("\n chromatic number is %d",m-1);
printf("\n total number of solutions is %d",sol);
getch();
}
void graphcoloring(int k)
{
int i;
while(1)
{
nextvalue(k);
if(x[k]==0)
return;
if(k==n)
{
c=1;
for(i=1;i<=n;i++)
printf("%d ",x[i]);
sol++;
printf("\n");
}
else
graphcoloring(k+1);
}
}
void nextvalue(int k)
{
int j;
while(1)
{
x[k]=(x[k]+1)%(m+1);
if(x[k]==0)
return;
for(j=1;j<=n;j++)
{
if(g[k][j]==1&&x[k]==x[j])
break;
}
if(j==(n+1))
return;
}
}

Output:
enter the number of vertices in a graph: 4
enter graph edges
0111
1011
0101
1010
All possible solutions are
1212
2121
chromatic number is 2
total number of solutions is 2

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy