-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_tree.c
294 lines (256 loc) · 7.72 KB
/
generate_tree.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
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include "generate_tree.h"
#define SWAP(x, y) {const point_t* temp = x; x = y; y = temp;}
/**
* @brief generates a leaf node
* @param pts is a set that contains one point
* @return a node is created with the point in pts and a pointer to it is returned
*/
static node_t* leaf_node(const point_t* pts[]);
/**
* @brief generate a node of the tree with the specified parametres
* @param d the discriminant of the node
* @param pto the point to be associated with the node
* @param l_son the left son of the node
* @param r_son the right son of the node
* @return pointer to the created node
*/
static node_t* gen_node(int d, const point_t *pto, const node_t *l_son, const node_t *r_son);
/**
* @brief calculates the variance of the d-th keys of points in pts
* @param d index of the keys to be checked
* @param pts points to be checked
* @return the variance
*/
static float var_nodes(int d, const point_t* pts[]);
/**
* @brief calculates the point with median key of index d
* @param d index of the keys to be checked
* @param pts points to be checked
* @return pointer to the median point
*/
static point_t* median_point(int d, const point_t* pts[]);
/**
* @brief finds the sets of the acceptable points for the left and right subtrees of a node Q
* @param l_pts array of valid points for the left subtree of Q
* @param r_pts array of valid points for the right subtree of Q
* @param pts the set of all points to be evaluated in order to decide which can be in l_pts and which in r_pts
* @param d the discriminant of Q
* @param med the point to be associated with Q
* @return the function does not return any value but keeps the valid points in l_pts and r_pts
* (which should be empty at call)
*/
static void sub_tree_find(const point_t* l_pts[], const point_t* r_pts[], const point_t* pts[], int d, const point_t* med);
static node_t* leaf_node(const point_t* pts[])
{
node_t* leaf = (node_t*) malloc(sizeof(node_t));
if (leaf == NULL){fprintf(stderr, "Can't allocate enought memory for the leaf"); exit(1);}
leaf -> pto = pts[0];
leaf -> disc = -1;
leaf -> l_son = NULL;
leaf -> r_son = NULL;
return leaf;
}
static node_t* gen_node(int d, const point_t *pto, const node_t *l_son, const node_t *r_son)
{
node_t* internal = (node_t*) malloc(sizeof(node_t));
if (internal == NULL){fprintf(stderr, "Can't allocate enought memory for the non-leaf"); exit(1);}
internal -> pto = pto;
internal -> disc = d;
internal -> l_son = (node_t*)l_son;
internal -> r_son = (node_t*)r_son;
return internal;
}
//This function scans the entire dataset and since it is called at each level of the
//k-d tree the overall time complexity is O(Nlog(N)). IS THERE A BETTER WAY?
static float var_nodes(int d, const point_t* pts[])
{
double sq_sum = 0;
double sum_sq = 0;
int i;
for (i = 0; i < PTS_N; ++i)
{
if (pts[i] == NULL)
break;
//DBG
//printf("VAR: READ POINT #%d:", i);
//for (int j = 0; j < DIM_K; ++j)
// printf(" %f", pts[i] -> keys[j]);
//printf("\n");
sq_sum += pts[i] -> keys[d];
sum_sq += (pts[i] -> keys[d])*(pts[i] -> keys[d]);
}
//if (i == 0)//useless since build_tree returns before calling this function if it recives 0 points in input
// return -1;
sq_sum = sq_sum*sq_sum;
return (float) ((double) 1/(i))*(sum_sq - ((double) 1/(i))*sq_sum);
}
//Lomuto partition
static int partition(const point_t* pts[], int d, int beginning, int end, int ind_pivot)
{
const point_t* pivot = pts[ind_pivot];
SWAP(pts[ind_pivot], pts[end])
int p_ind = beginning;
for (int i = beginning; i < end; ++i)
{
if ((pts[i] -> keys[d]) <= (pivot -> keys[d]))
{
SWAP(pts[i], pts[p_ind]);
p_ind++;
}
}
SWAP(pts[p_ind], pts[end]);
return p_ind;
}
//This algorithm has expected complexity O(N) (so it does not affect the
//performace of the algorithm which remains polylogarithmic), but worst case complexity is
//O(N^2).
static point_t* quick_select(const point_t* pts[], int d, int beginning, int end, int med_index)
{
if (beginning == end)
return (point_t*)pts[beginning];
int pivot_index = beginning + rand() % (end - beginning + 1);
pivot_index = partition(pts, d, beginning, end, pivot_index);
if (pivot_index == med_index)
return (point_t*)pts[pivot_index];
else if (pivot_index > med_index)
return quick_select(pts, d, beginning, pivot_index - 1, med_index);
else
return quick_select(pts, d, pivot_index + 1, end, med_index);
}
//Potentially some outlier instances might take more than usual due to the
//impletentation.
static point_t* median_point(int d, const point_t* pts[])
{
int num_pts;
for (num_pts = 0; num_pts < PTS_N; num_pts++)
if (pts[num_pts] == NULL)
break;
int med_index = num_pts/2;
return quick_select(pts, d, 0, num_pts - 1, med_index);
}
//This function scans the entire dataset and since it is called at each level of the
//k-d tree the overall time complexity is O(Nlog(N)). IS THERE A BETTER WAY?
static void sub_tree_find(const point_t* l_pts[], const point_t* r_pts[], const point_t* pts[], int d, const point_t* med)
{
int i, j;
i = j = 0;
for (int h = 0; h < PTS_N; h++)
{
if (med == pts[h])
continue;
if (pts[h] == NULL)
return;
if((med -> keys[d]) >= (pts[h] -> keys[d]))
l_pts[i++] = pts[h];
else
r_pts[j++] = pts[h];
}
}
node_t* build_tree(const point_t* pts[])
{
//DBG
//printf("NEW INSTANCE OF BUILD_TREE\n");
int num_pts;
for (num_pts = 0; num_pts < PTS_N; num_pts++)
if (pts[num_pts] == NULL)
break;
//DBG
//printf("COUNT BUILD_TREE SUCCESSFUL WITH RESULT %d\n", num_pts);
//printf("FOUND POINTS IN BUILD_TREE:\n");
//for (int i = 0; i < PTS_N; ++i)
//{
// if (pts[i] == NULL)
// break;
// printf("POINT #%d:", i);
// for (int j = 0; j < DIM_K; ++j)
// {
// printf(" %f", pts[i] -> keys[j]);
// }
// printf("\n");
//}
if (num_pts == 0)
{
//DBG
//printf("NO POINTS IN BUILD_TREE\n");
return NULL;
}
if (num_pts == 1)
{
//DBG
//printf("BASE CASE IN BUILD_TREE WITH POINT:");
//for (int j = 0; j < DIM_K; ++j)
//{
// printf(" %f", pts[0] -> keys[j]);
//}
//printf("\n");
return leaf_node(pts);
}
int disc = -1;
float temp_var = 0.0f;
const point_t *med = NULL;
const point_t *l_pts[num_pts], *r_pts[num_pts];
for (int i = 0; i < num_pts; ++i){l_pts[i] = r_pts[i] = NULL;}
//DBG
//printf("INITIALIZATION OF BUILD_TREE SUCCESSFUL\n");
float max_var = -1.0f;
for (int i = 0; i < DIM_K; ++i)
{
if ((temp_var = var_nodes(i, pts)) > max_var)
{
max_var = temp_var;
disc = i;
}
//DBG
//printf("CALCULATED VARIANCE: %f AT INDEX %d\n", temp_var, i);
}
//MEASURE PERFORMANCE WITHOUT THE CALCULATION OF VARIANCE
//static int disc_tracker = 0;
//disc = disc_tracker;
//disc_tracker = (disc_tracker+1)%DIM_K;
//DBG
//printf("FOUND DISC: %d\n", disc);
med = median_point(disc, pts);
//MEASURE PERFORMANCE WITHOUT THE CALCULATION OF MEDIAN
//med = pts[0];
//DBG
//printf("FOUND MEDIAN:");
//for (int i = 0; i < DIM_K; ++i)
// printf(" %f", med -> keys[i]);
//printf("\n");
sub_tree_find(l_pts, r_pts, pts, disc, med);
//DBG
//printf("SUCCESSFUL PARTITION INTO SUBSETS:\n");
//printf("LEFT SUBSET:\n");
//for (int i = 0; i < PTS_N; ++i)
//{
// if (l_pts[i] == NULL)
// break;
// for (int j = 0; j < DIM_K; ++j)
// printf("%f ", l_pts[i] -> keys[j]);
// printf("\n");
//}
//printf("RIGHT SUBSET:\n");
//for (int i = 0; i < PTS_N; ++i)
//{
// if (r_pts[i] == NULL)
// break;
// for (int j = 0; j < DIM_K; ++j)
// printf("%f ", r_pts[i] -> keys[j]);
// printf("\n");
//}
//printf("GENERATING NEW NODE\n");
return gen_node(disc, med, build_tree(l_pts), build_tree(r_pts));
}
void free_tree(node_t* node)
{
if (node -> l_son != NULL)
free_tree(node -> l_son);
if (node -> r_son != NULL)
free_tree(node -> r_son);
free(node);
node = NULL;
return;
}