-
Notifications
You must be signed in to change notification settings - Fork 0
/
p4.c
207 lines (177 loc) · 4.29 KB
/
p4.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
#include <stdio.h>
#include <stdlib.h>
#include "p4.h"
#include "bootstrap.h"
int main()
{
inicializar_semilla();
test_dijkstra_1();
test_dijkstra_2();
time_dijkstra();
return 0;
}
void test_dijkstra_1()
{
int tam = 5;
matriz m = (int *[5]){};
m[0] = (int[5]){0, 1, 8, 4, 7};
m[1] = (int[5]){1, 0, 2, 6, 5};
m[2] = (int[5]){8, 2, 0, 9, 5};
m[3] = (int[5]){4, 6, 9, 0, 3};
m[4] = (int[5]){7, 5, 5, 3, 0};
matriz d_obj = (int *[5]){};
d_obj[0] = (int[5]){0, 1, 3, 4, 6};
d_obj[1] = (int[5]){1, 0, 2, 5, 5};
d_obj[2] = (int[5]){3, 2, 0, 7, 5};
d_obj[3] = (int[5]){4, 5, 7, 0, 3};
d_obj[4] = (int[5]){6, 5, 5, 3, 0};
matriz d = crearMatriz(tam);
dijkstra(m, d, tam);
printf("Input:\n");
print_matriz(m, tam);
printf("Target:\n");
print_matriz(d_obj, tam);
printf("Result:\n");
print_matriz(d, tam);
printf("\n");
liberarMatriz(d, tam);
}
void test_dijkstra_2()
{
int tam = 4;
matriz m = (int *[4]){};
m[0] = (int[4]){0, 1, 4, 7};
m[1] = (int[4]){1, 0, 2, 8};
m[2] = (int[4]){4, 2, 0, 3};
m[3] = (int[4]){7, 8, 3, 0};
matriz d_obj = (int *[4]){};
d_obj[0] = (int[4]){0, 1, 3, 6};
d_obj[1] = (int[4]){1, 0, 2, 5};
d_obj[2] = (int[4]){3, 2, 0, 3};
d_obj[3] = (int[4]){6, 5, 3, 0};
matriz d = crearMatriz(tam);
dijkstra(m, d, tam);
printf("Input:\n");
print_matriz(m, tam);
printf("Target:\n");
print_matriz(d_obj, tam);
printf("Result:\n");
print_matriz(d, tam);
printf("\n");
liberarMatriz(d, tam);
}
double n_to_2_7(double n)
{
return pow(n, 2.7);
}
double n_to_2_9(double n)
{
return pow(n, 2.9);
}
double n_to_3_1(double n)
{
return pow(n, 3.1);
}
void time_dijkstra()
{
matriz m;
matriz d;
double t;
double times[EXP_INCREMENT_STEPS];
EXPONENTIAL_INCREMENT({
// Inicializar
m = crearMatriz(n);
iniMatriz(m, n);
d = crearMatriz(n);
// ^
t = TIME_IT(dijkstra(m, d, n));
if (t < TRUSTED_TIME)
t = TIME_REPEATED(iniMatriz(m, n),
dijkstra(m, d, n));
times[i] = t;
liberarMatriz(m, n);
liberarMatriz(d, n);
});
// (n * (n-1) * 2log n )/2 ->
print_table(times,
"n^2.7", "n^2.9", "n^3.1",
n_to_2_7, n_to_2_9, n_to_3_1);
}
void print_matriz(matriz m, int tam)
{
int i, j;
for (i = 0; i < tam; i++)
{
for (j = 0; j < tam; j++)
printf("%2d ", m[i][j]);
printf("\n");
}
}
matriz crearMatriz(int n)
{
int i;
matriz aux;
if ((aux = malloc(n * sizeof(int *))) == NULL)
return NULL;
for (i = 0; i < n; i++)
if ((aux[i] = malloc(n * sizeof(int))) == NULL)
return NULL;
return aux;
}
void iniMatriz(matriz m, int n)
{
int i, j;
for (i = 0; i < n; i++)
for (j = i + 1; j < n; j++)
m[i][j] = rand() % TAM_MAX + 1;
for (i = 0; i < n; i++)
for (j = 0; j <= i; j++)
if (i == j)
m[i][j] = 0;
else
m[i][j] = m[j][i];
}
void liberarMatriz(matriz m, int n)
{
int i;
for (i = 0; i < n; i++)
free(m[i]);
free(m);
}
void dijkstra(matriz grafo, matriz distancias, int tam)
{
int n, i, j, min, v = 0;
int *noVisitados = malloc(tam * sizeof(int));
for (n = 0; n < tam; n++)
{
for (i = 0; i < tam; i++)
{
noVisitados[i] = 1;
distancias[n][i] = grafo[n][i];
}
noVisitados[n] = 0;
for (j = 0; j < tam - 1; j++)
{
min = TAM_MAX + 1;
for (i = 0; i < tam; i++)
{
if (distancias[n][i] != 0 &&
distancias[n][i] < min &&
noVisitados[i])
{
min = distancias[n][i];
v = i;
}
}
noVisitados[v] = 0; // noVisitados - { v }
for (i = 0; i < tam; i++)
{
if (!noVisitados[i])
continue;
if (distancias[n][i] > distancias[n][v] + grafo[v][i])
distancias[n][i] = distancias[n][v] + grafo[v][i];
}
}
}
free(noVisitados);
}