-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharc_consistency.cpp
executable file
·211 lines (167 loc) · 4.06 KB
/
arc_consistency.cpp
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <iostream>
#include <stdio.h>
#include <queue>
#include <utility>
#include <fstream>
using namespace std;
// Number of vertices in the graph
#define V 15
// NUmber of coloures
#define m 4
void printSolution(int color[]);
bool goal(int color[]){
for(int i=0;i<V;i++){
if(color[i]==0){
return false;
}
}
return true;
}
// returns the vertex having minimum value remains in domain
int select_unassigned_var(int color[],int domain[V][m]){
int index,rem_val=m;
int k;
k=m;
int r=0;
for (int i = 0; i < V; i++){
r=0;
if(color[i]==0){
for(int j=0;j<k;j++){
if(domain[i][j]!=0)
r++;
}
if(r<rem_val){
index=i;
}
}
}
return index;
}
bool remove_incon_val(int x,int y,int domain[V][m]){
bool removed=false;
int c,flag;
for(int i=0;i<m;i++){
c=domain[x][i];
flag=0;
for (int j = 0; j < m; j++)
if (domain[y][j]!=0&&i!=j ){
flag=1;
}
if(flag ==0){
domain[x][i]=0;
removed=true;
}
else removed=false;
}
return removed;
}
void update_domain(int c,int v,bool graph[V][V],int domain[V][m]){
for (int i = 0; i < V; i++)
if (graph[v][i] ){
domain[i][c-1]=0;
}
}
bool arc3(bool graph[V][V],int domain[V][m]){
pair<int,int> x;
queue<pair<int,int> > q;
for(int i=0;i<V;i++){
for(int j=0;j<V;j++){
if(graph[i][j]!=0)
q.push(make_pair(i,j));
}
}
while (q.empty()) {
x=q.front();
if(remove_incon_val(x.first,x.second,domain)){
for(int i=0;i<V;i++){
if(graph[x.first][i]!=0)
q.push(make_pair(x.first,i));
}
}
/* code */
}
}
bool isSafe (int v, bool graph[V][V], int color[], int c)
{
for (int i = 0; i < V; i++)
if (graph[v][i] && c == color[i])
return false;
return true;
}
bool graphColoringUtil(bool graph[V][V], int color[], int v, int domain[V][m])
{
/* base case: If all vertices are assigned a color then
return true */
if(goal(color)) // check for goal test
return true;
int var;
/* Consider this vertex v and try different colors */
for (int c = 0; c < m; c++)
{
/* Check if assignment of color c to v is fine*/
if(domain[v][c]!=0)
if (isSafe(v, graph, color, domain[v][c]))
{
color[v] = domain[v][c];
update_domain(color[v],v,graph,domain);//upate the domain of adjacent vertixes
/* recur to assign colors to rest of the vertices */
var=select_unassigned_var(color,domain);
if (graphColoringUtil(graph, color, var,domain) == true)
return true;
/* If assigning color c doesn't lead to a solution
then remove it */
color[v] = 0;
}
}
/* If no color can be assigned to this vertex then return false */
return false;
}
bool graphColoring(bool graph[V][V], int domain[V][m])
{ arc3(graph,domain);
// Initialize all color values as 0. This initialization is needed
// correct functioning of isSafe()
int *color = new int[V];
for (int i = 0; i < V; i++)
color[i] = 0;
// Call graphColoringUtil() for vertex 0
if (graphColoringUtil(graph, color, 0,domain) == false)
{
printf("Solution does not exist");
return false;
}
// Print the solution
printSolution(color);
return true;
}
/* A utility function to print solution */
void printSolution(int color[])
{
printf("Solution Exists:"
" Following are the assigned colors \n");
for (int i = 0; i < V; i++)
printf(" %d ", color[i]);
printf("\n");
}
void inti_domain(int domain[V][m]){
for(int i=0;i<V;i++){
for(int j=0;j<m;j++){
domain[i][j]=j+1;
}
}
}
int main()
{
fstream fin;
int a;
fin.open("input10.txt");
int domain[V][m];
inti_domain(domain);
bool graph[V][V];
for(int i=0;i<V;i++)
for(int j=0;j<V;j++){
fin>>a;
graph[i][j]=a;
}
graphColoring (graph,domain);
return 0;
}