Skip to content

Commit 02e291c

Browse files
committed
Added Graph Coloring Problem code in Backtracking folder
1 parent b5160a6 commit 02e291c

File tree

1 file changed

+160
-0
lines changed

1 file changed

+160
-0
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/* C++ implementation of Graph Coloring Problem */
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
//Function for generating a next color
6+
/*x[1],...,x[k-1] have been assigned integer values
7+
in the range [1,m] such that adjacent vertices have
8+
distinct integers*/
9+
//A value for x[k] is determined in the range [0,m]
10+
/*x[k] is assigned the next highest numbered color while
11+
maintaining distinctness from the adjacent vertices of vertex k.
12+
If no such color exists, then x[k] is 0.*/
13+
14+
void nextvalue(int x[50],int g[50][50],int k,int n,int m)
15+
{
16+
int j;
17+
while(1)
18+
{
19+
x[k] = (x[k]+1) % (m+1); //next higher color
20+
if(x[k] == 0) //all colors have been used
21+
return;
22+
23+
//checking if this color is distinct from adjacent colors
24+
for(j=1; j<=n; j++)
25+
{
26+
if(g[k][j] != 0 && (x[k] == x[j])) //if (k,j) is an edge and if adjacent vertices have the same color
27+
break;
28+
}
29+
if(j == n+1) //new color found
30+
return;
31+
}
32+
}
33+
34+
//Finding all m-colorings of a given graph
35+
/*All assignments of 1,2,...,m to the vertices of the graph such that
36+
adjacent vertices are assigned distinct integers are printed*/
37+
//k is the index of the next vertex to color
38+
//set the array x[] to zero, and then invoke mcoloring(1)
39+
void mcoloring(int x[50],int g[50][50],int k,int n,int m)
40+
{
41+
int i;
42+
do //loop for generating all legal assignments for x[k]
43+
{
44+
nextvalue(x, g, k,n ,m); //Assign to x[k] a legal color
45+
46+
if(x[k] == 0) //No new color possible
47+
return;
48+
49+
if(k == n) //Atmost m colors have been used to color the n vertices
50+
{
51+
for(i=1 ;i<=n; i++)
52+
cout<<"V"<<i<<": "<<x[i]<<"\t";
53+
cout<<"\n";
54+
}
55+
else
56+
mcoloring(x, g, k+1, n, m);
57+
58+
}while(k<n+1);
59+
}
60+
61+
//Main function
62+
int main()
63+
{
64+
int n,ne,u,v,i,j,x[50],g[50][50],m;
65+
66+
cout<<"\nEnter number of vertices: ";
67+
cin>>n; //reading number of vertices of the graph from user
68+
69+
cout<<"\nEnter number of edges: ";
70+
cin>>ne; //reading number of edges of the graph from user
71+
72+
m = n-1;
73+
cout<<"\nThe maximum possible colours that can be assigned is "<<m<<endl;
74+
75+
//The graph is represented by its boolean adjacency matrix G[1:n,1:n]
76+
for(i=0; i<=n; i++)
77+
for(j=0; j<=n; j++)
78+
g[i][j] = 0;
79+
80+
for(i=1; i<=ne; i++)
81+
{
82+
cout<<"\nEnter the first terminal vertex of edge "<<i<<": ";
83+
cin>>u;
84+
85+
cout<<"\nEnter the second terminal vertex of edge "<<i<<": ";
86+
cin>>v;
87+
88+
g[u][v] = 1;
89+
g[v][u] = 1;
90+
}
91+
92+
for(i=0; i<=n; i++)
93+
x[i] = 0;
94+
95+
cout<<endl<<"The colouring possibilities are:"<<endl;
96+
mcoloring(x ,g ,1 ,n ,m);
97+
}
98+
99+
/*
100+
Input-
101+
102+
Number of vertices= 4
103+
Number of edges= 4
104+
105+
Adjacency matrix:
106+
107+
0 1 0 1
108+
1 0 1 0
109+
0 1 0 1
110+
1 0 1 0
111+
112+
Output-
113+
114+
Enter number of vertices: 4
115+
116+
Enter number of edges: 4
117+
118+
The maximum possible colours that can be assigned is 3
119+
120+
Enter the first terminal vertex of edge 1: 1
121+
122+
Enter the second terminal vertex of edge 1: 2
123+
124+
Enter the first terminal vertex of edge 2: 2
125+
126+
Enter the second terminal vertex of edge 2: 3
127+
128+
Enter the first terminal vertex of edge 3: 3
129+
130+
Enter the second terminal vertex of edge 3: 4
131+
132+
Enter the first terminal vertex of edge 4: 1
133+
134+
Enter the second terminal vertex of edge 4: 4
135+
136+
The colouring possibilities are:
137+
V1: 1 V2: 2 V3: 1 V4: 2
138+
V1: 1 V2: 2 V3: 1 V4: 3
139+
V1: 1 V2: 2 V3: 3 V4: 2
140+
V1: 1 V2: 3 V3: 1 V4: 2
141+
V1: 1 V2: 3 V3: 1 V4: 3
142+
V1: 1 V2: 3 V3: 2 V4: 3
143+
V1: 2 V2: 1 V3: 2 V4: 1
144+
V1: 2 V2: 1 V3: 2 V4: 3
145+
V1: 2 V2: 1 V3: 3 V4: 1
146+
V1: 2 V2: 3 V3: 1 V4: 3
147+
V1: 2 V2: 3 V3: 2 V4: 1
148+
V1: 2 V2: 3 V3: 2 V4: 3
149+
V1: 3 V2: 1 V3: 2 V4: 1
150+
V1: 3 V2: 1 V3: 3 V4: 1
151+
V1: 3 V2: 1 V3: 3 V4: 2
152+
V1: 3 V2: 2 V3: 1 V4: 2
153+
V1: 3 V2: 2 V3: 3 V4: 1
154+
V1: 3 V2: 2 V3: 3 V4: 2
155+
156+
Time Complexity: O(m^V)
157+
Space Complexity: O(V)
158+
159+
where V= number of vertices and m= maximum possible colors
160+
*/

0 commit comments

Comments
 (0)