From 775d0ca49236e732c9e010733dda4cfaef07230d Mon Sep 17 00:00:00 2001 From: Sidhi Jain <64956959+Sidhijain@users.noreply.github.com> Date: Sun, 30 May 2021 18:20:44 +0530 Subject: [PATCH 1/2] M coloring problem --- Algorithm/Backtracking/M coloring problem | 88 +++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 Algorithm/Backtracking/M coloring problem diff --git a/Algorithm/Backtracking/M coloring problem b/Algorithm/Backtracking/M coloring problem new file mode 100644 index 000000000..0e3874056 --- /dev/null +++ b/Algorithm/Backtracking/M coloring problem @@ -0,0 +1,88 @@ +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 +//Program in C++ +#include + +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); +} + +int nextvalue(int k,int x[],int n,int a[][20],int m) +{ + int j; + do + { + x[k]=(x[k]+1)%(m+1); + + if(x[k]==0) + return 0; + for(j=1;j<=n;j++) + { + if((a[k][j]!=0)&&(x[k]==x[j])) + break; + } + if(j==n+1) + 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. +*/ From 17e5f060ffa31009feddd1f53e4033f008479d96 Mon Sep 17 00:00:00 2001 From: Sidhi Jain <64956959+Sidhijain@users.noreply.github.com> Date: Sun, 30 May 2021 21:59:41 +0530 Subject: [PATCH 2/2] Update M coloring problem --- Algorithm/Backtracking/M coloring problem | 67 +++++++++++++++++++++-- 1 file changed, 62 insertions(+), 5 deletions(-) diff --git a/Algorithm/Backtracking/M coloring problem b/Algorithm/Backtracking/M coloring problem index 0e3874056..ac9e4688a 100644 --- a/Algorithm/Backtracking/M coloring problem +++ b/Algorithm/Backtracking/M coloring problem @@ -1,6 +1,7 @@ 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 @@ -24,22 +25,22 @@ int mcoloring(int k,int x[],int n,int a[][20],int m) 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); + x[k]=(x[k]+1)%(m+1);// next higher color if(x[k]==0) - return 0; + return 0;// when all colors are used for(j=1;j<=n;j++) { - if((a[k][j]!=0)&&(x[k]==x[j])) + if((a[k][j]!=0)&&(x[k]==x[j]))// checks if the color is distinct from adjacent colors break; } - if(j==n+1) + if(j==n+1)// that means new color found return 0; }while(1); } @@ -85,4 +86,60 @@ 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 + */ +