-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.c
407 lines (364 loc) · 7.51 KB
/
helpers.c
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include <stdio.h>
#include <stdlib.h>
//#include <stdint.h>
#include <stdbool.h>
#include "helpers.h"
int cmpN1(const void * a, const void * b)
{
struct _Nodo al = *(Nodo)a;
struct _Nodo bl = *(Nodo)b;
if (al.estado0 < bl.estado0)
{
return 1;
}
if (al.estado0 > bl.estado0)
{
return -1;
}else{
return 1;
}
}
int cmpN2(const void * a, const void * b)
{
struct _Nodo al = *(Nodo)a;
struct _Nodo bl = *(Nodo)b;
if (al.estado2 < bl.estado2)
{
return -1;
}
if (al.estado2 > bl.estado2)
{
return 1;
}else{
if(al.estado0 < bl.estado0)
{
return -1;
}
if(al.estado0 > bl.estado0)
{
return 1;
}
else
{
return 0;
}
}
}
int cmpuestado0 (const void * a, const void * b)
{
Nodo al =*((Nodo*)a);
Nodo bl =*((Nodo*)b);
if (bl->estado0 > al->estado0)
{
return -1;
}
if (bl->estado0 < al->estado0)
{
return 1;
}else{
return 0;
}
}
bool VerificadorWP(Grafo G)
{
u32 NumVer = NumeroDeVertices(G);
for(u32 i=0; i<NumVer; i++)
{
for(u32 j=i; j<NumVer; j++)
{
if(Grado(i,G) < Grado(j,G))
{
return false;
}
}
}
return true;
}
/*----------------------------------OrdBC--------------------------------------
* Se empleo array de struct _Nodo *, llamado Nodo, de la manera,
* ∀i / 0 <= i < NumeroDeVertices(G):
* Nodo[i].estado0 = Color del vértice i del grafo G.
* Nodo[i].estado1 = Posición natural del vértice i del grafo G.
* Nodo[i].estado2 = La cantidad de repeticiones del color del vértice i del
* grafo G.
* Se empleo NodoBC, llamado ColArray, de la manera:
* ColArray.length = cantidad de colores
* ColArray.array = array de u32, la posición i determina las repeticiones del
* color i en el grafo G.
*/
Nodo OrdBC(Grafo G)
{
u32 NumVer = NumeroDeVertices(G);
Nodo Nodo = calloc(NumVer, sizeof(struct _Nodo));
if(Nodo == NULL)
{
return NULL;
}
NodoBC ColArray;
ColArray.length = 0;
ColArray.array = NULL;
u32 ColTem;
// Ordeno de forma natural, guardo datos de los vertices, calculo el maximo
// color y las repeticiones de cada color
for(u32 i=0; i<NumVer; i++)
{
FijarOrden(i,G,i);
Nodo[i].estado0 = Color(i,G);
Nodo[i].estado1 = i;
if(ColArray.length <= Color(i,G))
{
ColTem = ColArray.length;
ColArray.length = Color(i,G) + 1;
ColArray.array = realloc(ColArray.array,ColArray.length*sizeof(u32));
if(ColArray.array == NULL)
{
free(ColArray.array);
ColArray.array = NULL;
return NULL;
}
// Inicializamos las celdas en 0
for(u32 i=ColTem; i<ColArray.length;i++)
{
ColArray.array[i] = 0;
}
}
// Incrementamos las repeticiones del color
ColArray.array[Color(i,G)] += 1;
}
// Guardamos las veces que se repite el color del vertice
for(u32 i=0; i<NumVer; i++)
{
Nodo[i].estado2 = ColArray.array[Nodo[i].estado0];
}
free(ColArray.array);
return Nodo;
}
/*----------------------------------BiDFS--------------------------------------
* Mediante OrdenVecino() obetenmos la posición del vecino para poder hacer
* recurción sobre BiDFS.
*/
bool BiDFS(u32 index, Grafo G)
{
bool ret = true;
u32 color = 0;
u32 disp0 = 0;
u32 disp1 = 0;
if(Color(index,G) == 4294967296-1)
{
// Buscamos el color disponible para el vertice en la posición index
for(u32 k=0; k<Grado(index,G);k++)
{
if(ColorVecino(k,index,G) == 0 && disp0 == 0)
{
disp0 = 1;
}
if(ColorVecino(k,index,G) == 1 && disp1 == 0)
{
disp1 = 1;
}
if (disp1 == 1 && disp0 == 1)
{
return false;
}else if(disp0 == 0){
color = 0;
}else if(disp1 == 0){
color = 1;
}
}
FijarColor(color,index,G);
// Hago recurción sobre los vecinos llamando BiDFS
for(u32 k=0; k<Grado(index,G); k++)
{
u32 posc = OrdenVecino(k,index,G);
ret = BiDFS(posc,G);
if(!ret)
{
return false;
}
}
}
return ret;
}
//estado0: nombrev, estado1: si esta visitado 1, si no 0
Nodo2 * inicializar_tabla(u32 NumVert, Grafo G)
{
Nodo2 * hash = malloc(sizeof(Nodo) * NumVert);
if (hash != NULL)
{
for (unsigned int i = 0; i < NumVert; i++)
{
hash[i] = malloc(sizeof(struct _Nodo2 ));
if (hash[i] != NULL) {
hash[i]->estado0 = ErrorGrafo;
hash[i]->estado1 = 0;
}
}
}
for (u32 j = 0; j < NumVert; j++)
{
u32 index = Nombre(j,G) % NumVert;
//en la posicion index en hash esta vacia
if(hash[index]->estado0 == ErrorGrafo)
{
hash[index]->estado0 = Nombre(j,G);
hash[index]->estado2 = j;
}else
//tenemos que buscar una celda vacia
{
u32 tope = index;
index++;
while(index != tope)
{
if(index == NumVert) {
index = 0;
}else if(hash[index]->estado0 == ErrorGrafo)
{
hash[index]->estado0 = Nombre(j,G);
hash[index]->estado2 = j;
break;
}else
{
index++;
}
}
}
}
return hash;
}
void liberar_tabla(Nodo2 * hash,u32 NumVert)
{
for (unsigned int i = 0; i < NumVert; i++) {
free(hash[i]);
}
free(hash);
}
bool vertice_fue_visitado( u32 nombrev, Nodo2 * hash,u32 NumVert)
{
bool result = false;
u32 index = nombrev % NumVert;
if (hash[index]->estado0 == nombrev)
{
result = hash[index]->estado1;
}
else
{
u32 tope = index;
index++;
while (tope != index)
{
if (index == NumVert) {
index = 0;
}
if (hash[index]->estado0 == nombrev)
{
result = hash[index]->estado1;
break;
}
else
{
index++;
}
}
}
return result;
}
u32 obtener_orden_interno( u32 nombrev, Nodo2 * hash,u32 NumVert)
{
u32 result = 0;
u32 index = nombrev % NumVert;
if (hash[index]->estado0 == nombrev)
{
result = hash[index]->estado2;
}
else
{
u32 tope = index;
index++;
while (tope != index)
{
if (index == NumVert) {
index = 0;
}
if (hash[index]->estado0 == nombrev)
{
result = hash[index]->estado2;
break;
}
else
{
index++;
}
}
}
return result;
}
void visitar_vertice (u32 nombrev, Nodo2 * visitados, u32 NumVert)
{
u32 index = nombrev % NumVert;
if (visitados[index]->estado0 == nombrev)
{
visitados[index]->estado1 = 1;
}
else
{
u32 tope = index;
index++;
while (tope != index)
{
if (index == NumVert) {
index = 0;
}
if (visitados[index]->estado0 == nombrev)
{
visitados[index]->estado1 = 1;
break;
}
else
{
index++;
}
}
}
}
void visitar_vecinos(unsigned int i, Nodo2 * visitados, u32 NumVert,Grafo G)
{
u32 numVecinos = Grado(i,G);
for (unsigned int j = 0; j < numVecinos; j++) {
u32 nombrevecino = NombreVecino(j,i,G);
if (!vertice_fue_visitado(nombrevecino,visitados,NumVert))
{
u32 ordeninterno = obtener_orden_interno(nombrevecino,visitados,NumVert);
visitar_vertice(nombrevecino,visitados,NumVert);
visitar_vecinos(ordeninterno,visitados,NumVert,G);
}
}
}
void crear_cjto_color(u32 index,u32 * conjunto,u32 conjunto_length,Grafo G)
{
for (u32 j = 0; j < (Grado(index,G)) ; j++)//para cada vecino j de index
{
u32 colorvecino = ColorVecino(j,index,G);
/*si el color del vecino j es mayor que los manejados actualmente
(solo puede ser el ErrorGrafo) => no fue coloreado
si es menor, entonces tiene un color de los usados actualmente*/
if(colorvecino < conjunto_length)
{
conjunto[colorvecino] = 1;
}
}
}
u32 buscar_min_color(u32 * conjunto, u32 conjunto_length)
{
u32 index = 0;
while (index < conjunto_length)
{
if(conjunto[index] == 0)
{
return index;
}else
{
index++;
}
}
return index;
}