Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

M coloring problem #2065

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions Algorithm/Backtracking/M coloring problem
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
Given an undirected graph and a number m, determine if the graph can be coloured with at most m colours such that no two adjacent vertices of the graph are colored with the same color.
Input : in input we have taken a undirected graph in which user has to enter no of vertices and edges between vertices also we have to provide a number m which is the maximum number of colors that can be used.
Output:In output the array is displayed of all the pattern in which we can color the graph .If there is no possiblity then program is simply terminated
/* Problem is solved by using backtracking . in backtracking we deal problem with searching for a set of solutions or which ask for an optimal solution that satisfiessome constraits can be solves using backtracking formulation
//Program in C++
#include<stdio.h>

int nextvalue(int k,int x[],int n,int a[][20],int m);

int mcoloring(int k,int x[],int n,int a[][20],int m)
{
int i;
do
{
nextvalue(k,x,n,a,m);
if(x[k]==0)
return 0;
if(k==n)
{
for(i=1;i<=n;i++)
printf("%d\t",x[i]);
printf("\n");
}
else
mcoloring(k+1,x,n,a,m);
}while(1);
}
// function for genertaing next color
int nextvalue(int k,int x[],int n,int a[][20],int m)
{
int j;
do
{
x[k]=(x[k]+1)%(m+1);// next higher color

if(x[k]==0)
return 0;// when all colors are used
for(j=1;j<=n;j++)
{
if((a[k][j]!=0)&&(x[k]==x[j]))// checks if the color is distinct from adjacent colors
break;
}
if(j==n+1)// that means new color found
return 0;
}while(1);
}

int main()
{
int n,ne,k,x[20],ar[20][20],i,j,a,b,m;

printf("enter no of vertices: ");
scanf("%d",&n);

printf("enter total no of edges: ");
scanf("%d",&ne);

printf("m= ");
scanf("%d",&m);

for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
ar[i][j]=0;

x[i]=0;
}


for(i=1;i<=ne;i++)
{
printf("enter first terminal: ");
scanf("%d",&a);

printf("enter second terminal: ");
scanf("%d",&b);

ar[a][b]=1;
ar[b][a]=1;
}

mcoloring(1,x,n,ar,m);
}
/*
Time Complexity: O(m^V).
There are total O(m^V) combination of colors. So time complexity is O(m^V).
Space Complexity: O(V).
It will require O(V) space.
Test case 1
enter no of vertices: 5
enter total no of edges: 5
m= 3
enter first terminal: 1
enter second terminal: 2
enter first terminal: 2
enter second terminal: 3
enter first terminal: 3
enter second terminal: 4
enter first terminal: 4
enter second terminal: 5
enter first terminal: 5
enter second terminal: 1
1 2 1 2 3
1 2 1 3 2
1 2 3 1 2
1 2 3 1 3
1 2 3 2 3
1 3 1 2 3
1 3 1 3 2
1 3 2 1 2
1 3 2 1 3
1 3 2 3 2
2 1 2 1 3
2 1 2 3 1
2 1 3 1 3
2 1 3 2 1
2 1 3 2 3
2 3 1 2 1
2 3 1 2 3
2 3 1 3 1
2 3 2 1 3
2 3 2 3 1
3 1 2 1 2
3 1 2 3 1
3 1 2 3 2
3 1 3 1 2
3 1 3 2 1
3 2 1 2 1
3 2 1 3 1
3 2 1 3 2
3 2 3 1 2
3 2 3 2 1
Test case 2
enter no of vertices: 3
enter total no of edges: 1
m= 2
enter first terminal: 1
enter second terminal: 2
1 2 1
1 2 2
2 1 1
2 1 2

*/