-
Notifications
You must be signed in to change notification settings - Fork 0
/
APIG23.c
286 lines (245 loc) · 8.7 KB
/
APIG23.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
#include "APIG23.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "EstructuraGrafo23.h"
//////////////////////////////////////////////////////////////////////////////////////////
///////////////FUNCIONES ESTATICAS PARA MEJORAR LA MODULARIZACION DEL CODIGO//////////////
/*Crea un nuevo grafo con la cantidad de vertices
y la cantidad de lados
*/
/* static print_graph(Grafo G) {
printf("Vertices: %u\n", G->cantVertices);
printf("Lados: %u\n", G->cantLados);
printf("Mayor grado: %u\n", G->mayor_grado);
for (u32 i = 0; i < G->cantVertices; i++) {
printf("Vertice %u: ", G->listVertices[i].nombre);
printf("Grado %u: ", G->listVertices[i].grado);
for (u32 j = 0; j < G->listVertices[i].grado; j++) {
printf("%u ", G->listVertices[i].indiceVecinos[j]);
}
printf("\n");
}
} */
static Grafo initGrafo(u32 n, u32 m) {
Grafo newGrafo = calloc(1, sizeof(GrafoSt));
if (newGrafo == NULL) {
printf("Error no se pudo pedir la memoria para la estructura grafo");
return NULL;
}
// guardaste memoria para M*2 tuplas
newGrafo->listLados = (Tupla *)calloc((2 * m), sizeof(Tupla));
if (newGrafo->listLados == NULL) {
printf("Error no se pudo pedir la memoria para la lista de lados");
free(newGrafo);
return NULL;
}
// guaradste memoria para una lista con N vertices
// estos vertices fijate van a vivr en esos espacios de memoria
newGrafo->listVertices = calloc(n, sizeof(vertice));
if (newGrafo->listVertices == NULL) {
printf("Error no se pudo pedir la memoria para la lista de vertices");
free(newGrafo->listLados);
free(newGrafo);
return NULL;
}
newGrafo->cantVertices = n;
newGrafo->cantLados = m;
newGrafo->mayorGrado = 0;
return newGrafo;
}
static void cargar_lado(Tupla *listaLados, int i, u32 primero, u32 segundo) {
listaLados[i].x = primero;
listaLados[i].y = segundo;
listaLados[i + 1].x = segundo;
listaLados[i + 1].y = primero;
}
static int cmpTuples(const void *a, const void *b) {
// ordena de menor a mayor primero respecto de
// la primera componente (x) y luego de la segunda (y)
Tupla *tuplaA = (Tupla *)a;
Tupla *tuplaB = (Tupla *)b;
if (tuplaA->x < tuplaB->x) {
return -1;
} else if (tuplaA->x > tuplaB->x) {
return 1;
} else {
if (tuplaA->y < tuplaB->y) {
return -1;
} else if (tuplaA->y > tuplaB->y) {
return 1;
} else {
return 0;
}
}
}
static int binarySearch(Grafo G, u32 target_name) {
u32 left = 0;
u32 right = G->cantVertices - 1;
u32 pivot;
u32 vecino;
while (left <= right) {
pivot = (left + right) / 2;
vecino = G->listVertices[pivot].nombre;
if (vecino == target_name) {
return pivot;
} else if (vecino < target_name) {
left = pivot + 1;
} else {
right = pivot - 1;
}
}
return -1;
}
static void cargarVecinos(Grafo G) {
u32 indice;
u32 maxGrade = 0;
// printf("Cargando vecinos.\n");
for (u32 i = 0; i < G->cantVertices; i++) {
if (G->listVertices[i].grado > maxGrade) {
maxGrade = G->listVertices[i].grado;
}
for (u32 j = 0; j < G->listVertices[i].grado; j++) {
// printf("Cargando vecino %u del vertice %u\n", j, G->listVertices[i].nombre);
indice = binarySearch(G, G->listVertices[i].indiceVecinos[j]);
if (indice != -1) {
// printf("Nombre del vertice: %u ", G->listVertices[i].nombre);
// printf("Nombre del vecino antes: %u", G->listVertices[i].indiceVecinos[j]);
//// printf(" GRADO %u", G->listVertices[i].grado);
// printf("\nNombre del vecino buscado con indice obtenido: %u\n\n", G->listVertices[indice].nombre);
G->listVertices[i].indiceVecinos[j] = indice;
}
}
}
G->mayorGrado = maxGrade;
// printf("Vecinos Cargados :).\n");
}
static void cargarVertices(Grafo G) {
u32 lastCharged = 0;
u32 grado = 0;
u32 indiceVert = 0;
u32 **generalVecinos = calloc(G->cantVertices, sizeof(u32 *));
// cargo el primer vertice y su indice
G->listVertices[indiceVert].nombre = G->listLados[indiceVert].x;
lastCharged = G->listVertices[indiceVert].nombre;
G->listVertices[indiceVert].indice = indiceVert;
for (u32 i = 0; i < G->cantLados * 2; i++) {
if (G->listLados[i].x == lastCharged) {
grado++;
G->listVertices[indiceVert].grado = grado;
} else {
indiceVert++;
grado = 1;
lastCharged = G->listLados[i].x;
G->listVertices[indiceVert].nombre = lastCharged;
G->listVertices[indiceVert].indice = indiceVert;
G->listVertices[indiceVert].grado = grado;
}
generalVecinos[indiceVert] = realloc(generalVecinos[indiceVert], grado * sizeof(u32));
// printf("%u \n", generalVecinos[indiceVert]);
generalVecinos[indiceVert][grado - 1] = G->listLados[i].y;
}
// update the vertex arrays with the neighbor lists
for (u32 i = 0; i < G->cantVertices; i++) {
G->listVertices[i].indiceVecinos = calloc(G->listVertices[i].grado, sizeof(u32));
// printf("list_vecinos de %u\n", G->listVertices[i].nombre);
// printf("[");
for (u32 j = 0; j < G->listVertices[i].grado; j++) {
G->listVertices[i].indiceVecinos[j] = generalVecinos[i][j];
// printf("%u,", G->listVertices[i].indiceVecinos[j]);
}
// printf("] \n");
free(generalVecinos[i]);
}
free(generalVecinos);
}
static Grafo destroyGrafo(Grafo grafo) {
for (u32 i = 0; i < grafo->cantVertices; i++) {
free(grafo->listVertices[i].indiceVecinos);
grafo->listVertices[i].indiceVecinos = NULL;
}
free(grafo->listVertices);
grafo->listVertices = NULL;
free(grafo->listLados);
grafo->listLados = NULL;
free(grafo);
return NULL;
}
// construcci'on/destrucci'on
/*debe leer desde stdin
Debo calcular el Delta
*/
Grafo ConstruirGrafo() {
FILE *fInput = stdin;
char line[1024];
u32 n, m, x, y;
Grafo myGraph;
while (fgets(line, sizeof(line), fInput)) {
if (line[0] == 'c') {
// hago skip a esta linea y continuo a la siguiente
continue;
} else if (line[0] == 'p') {
sscanf(line, "p edge %u %u", &n, &m);
// printf("n = %u, m = %u\n", n, m);
myGraph = initGrafo(n, m);
break;
} else {
// En caso de error devuelvo NULL
return NULL;
}
}
/* Tengo que sacar las ultimas dos guardas, luego de encontrar la linea con p tengo m lineas de formato e num num donde cargo los lados,
si tengo una linea con otro formato deberia dar error, despues de m lineas puede haber una cantidad arbitraria de lineas sin formato
por lo que no tengo que seguir leyendo
*/
for (u32 i = 0; i < m * 2; i = i + 2) {
fgets(line, sizeof(line), fInput);
if (line[0] != 'e') {
// ERROR en formato
return NULL;
} else {
// aca entiendo deberiamos cargar desp ordenar y desp cargar los vertices pero pa eso deberiamos recorrer los vertices y eso esta raro
sscanf(line, "e %u %u", &x, &y);
cargar_lado(myGraph->listLados, i, x, y);
}
}
// ordeno los lados de menor a mayor por primera y segunda componente
// ERROR: aca funciona para R22_93_15 pero tira malloc corrupted para el resto SE CLAVA ACAAAA
// printf("antes del sort");
qsort(myGraph->listLados, myGraph->cantLados * 2, sizeof(Tupla), cmpTuples);
// carga de vertices
cargarVertices(myGraph);
// carga de vecinos
cargarVecinos(myGraph);
// print_graph(myGraph);
return myGraph;
}
void DestruirGrafo(Grafo G) {
G = destroyGrafo(G);
}
// funciones para extraer datos del grafo. u32 debe estar definida en el .h de arriba
u32 NumeroDeVertices(Grafo G) {
return G->cantVertices;
}
u32 NumeroDeLados(Grafo G) {
return G->cantLados;
}
u32 Delta(Grafo G) {
return G->mayorGrado;
}
// funciones de extraccion de informacion de vertices
u32 Nombre(u32 i, Grafo G) {
return G->listVertices[i].nombre;
}
u32 Grado(u32 i, Grafo G) {
return G->listVertices[i].grado;
}
// vertice i y buscar el j-esimo vecino de i
u32 IndiceVecino(u32 j, u32 i, Grafo G) {
if (i >= G->cantVertices || j >= G->listVertices[i].grado) {
return (2 ^ 32) - 1;
// caso de error
}
return G->listVertices[i].indiceVecinos[j];
}