-
Notifications
You must be signed in to change notification settings - Fork 617
/
Copy pathM coloring problem
145 lines (129 loc) · 3.68 KB
/
M coloring problem
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
*/