Proyecto para encontrar 2 puntos más cercano para el curso de Análisis y Diseño de Algoritmos.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
5.7KB

  1. /*
  2. * Copyright 2018 Christopher Cromer
  3. * Copyright 2018 Rodolfo Cuevas
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  6. *
  7. * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  8. *
  9. * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  10. *
  11. * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. */
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <math.h>
  18. #include <float.h>
  19. #include "points.h"
  20. #include "brute_force.h"
  21. #include "distance.h"
  22. /**
  23. * Comparar 2 double para ver cual es mayor o si son iguales
  24. * @param a El primer double a comparar
  25. * @param b El segundo double a comparar
  26. * @return Retorna 1 si a es mayor de b, -1 si a es menor de b ó 0 si son igual
  27. */
  28. int compare_double(double a, double b) {
  29. if (a > b) {
  30. return 1;
  31. }
  32. else if (a < b) {
  33. return -1;
  34. }
  35. else {
  36. return 0;
  37. }
  38. }
  39. /**
  40. * Comparar los el eje x de 2 puntos
  41. * @param a El primer punto a comparar
  42. * @param b El segundo punto a comparar
  43. * @return Retorna la comparación entre los 2 puntos
  44. */
  45. int compare_x(const void * a, const void * b) {
  46. return compare_double((*(point_t *) a).x, (*(point_t *) b).x);
  47. }
  48. /**
  49. * Comparar los el eje y de 2 puntos
  50. * @param a El primer punto a comparar
  51. * @param b El segundo punto a comparar
  52. * @return Retorna la comparación entre los 2 puntos
  53. */
  54. int compare_y(const void * a, const void * b) {
  55. return compare_double((*(point_t *) a).y, (*(point_t *) b).y);
  56. }
  57. /**
  58. * El algoritmo de encontrar 2 puntos recursivo usando divide and conquer
  59. * @param points_x Los puntos ordenado en el eje x
  60. * @param nx La cantidad de puntos en el array points_y
  61. * @param points_y Los puntos ordenado en el eje y
  62. * @param ny La cantidad de puntos en el array points_y
  63. * @param closest_pair Las 2 pares de puntos mas cercano
  64. * @return Retorna la distance entre los dos puntos mas cercano
  65. */
  66. double divide_and_conquer_run(point_t *points_x, unsigned int nx, point_t *points_y, unsigned int ny, point_t *closest_pair) {
  67. int left;
  68. int right;
  69. int i;
  70. double mid;
  71. double d = DBL_MAX;
  72. double min_d = DBL_MAX;
  73. double x0;
  74. double x1;
  75. double x;
  76. point_t *closest_pair2;
  77. point_t *points_y2;
  78. if (nx <= 4) {
  79. closest_pair2 = brute_force(points_x, nx, &d);
  80. closest_pair[0] = closest_pair2[0];
  81. closest_pair[1] = closest_pair2[1];
  82. return d;
  83. }
  84. closest_pair2 = malloc(sizeof(point_t) * 2);
  85. points_y2 = malloc(sizeof(point_t) * ny);
  86. mid = points_x[nx / 2].x;
  87. left = -1;
  88. right = ny;
  89. for (i = 0; i < ny; i++) {
  90. if (points_y[i].x < mid) {
  91. points_y2[++left] = points_y[i];
  92. }
  93. else {
  94. points_y2[--right]= points_y[i];
  95. }
  96. }
  97. for (i = ny - 1; right < i; right ++, i--) {
  98. closest_pair2[0] = points_y2[right];
  99. points_y2[right] = points_y2[i];
  100. points_y2[i] = closest_pair2[0];
  101. }
  102. min_d = divide_and_conquer_run(points_x, nx / 2, points_y2, left + 1, closest_pair);
  103. d = divide_and_conquer_run(points_x + nx / 2, nx - nx / 2, points_y2 + left + 1, ny - left - 1, closest_pair2);
  104. if (d < min_d) {
  105. min_d = d;
  106. closest_pair[0] = closest_pair2[0];
  107. closest_pair[1] = closest_pair2[1];
  108. }
  109. d = sqrt(min_d);
  110. free(closest_pair2);
  111. left = -1; right = ny;
  112. for (i = 0; i < ny; i++) {
  113. x = points_y[i].x - mid;
  114. if (x <= -d || x >= d) {
  115. continue;
  116. }
  117. if (x < 0) {
  118. points_y2[++left] = points_y[i];
  119. }
  120. else {
  121. points_y2[--right] = points_y[i];
  122. }
  123. }
  124. while (left >= 0) {
  125. x0 = points_y2[left].y + d;
  126. while (right < ny && points_y2[right].y > x0) {
  127. right ++;
  128. }
  129. if (right >= ny) {
  130. break;
  131. }
  132. x1 = points_y2[left].y - d;
  133. for (i = right; i < ny && points_y2[i].y > x1; i++)
  134. if ((x = distance(points_y2[left], points_y2[i])) < min_d) {
  135. min_d = x;
  136. closest_pair[0] = points_y2[left];
  137. closest_pair[1] = points_y2[i];
  138. }
  139. left--;
  140. }
  141. free(points_y2);
  142. return min_d;
  143. }
  144. /**
  145. * Encontrar los 2 puntos más cercano usando el metodo de dividir para conquistar
  146. * @param point_t Los puntos a calcular
  147. * @param n La cantidad de puntos en el array point_ts
  148. * @param minimum_dist La distancia minimo encontrado
  149. * @return Retorna los 2 puntos mas cercanos
  150. */
  151. point_t * divide_and_conquer(point_t *points, unsigned int n, double *minimum_dist) {
  152. point_t *closest_pair = malloc(sizeof(point_t) * 2);
  153. point_t *points_x = malloc(sizeof(point_t) * n);
  154. point_t *points_y = malloc(sizeof(point_t) * n);
  155. memcpy(points_x, points, sizeof(point_t) * n);
  156. memcpy(points_y, points, sizeof(point_t) * n);
  157. qsort(points_x, n, sizeof(point_t), compare_x);
  158. qsort(points_y, n, sizeof(point_t), compare_y);
  159. *minimum_dist = divide_and_conquer_run(points_x, n, points_y, n, closest_pair);
  160. free(points_x);
  161. free(points_y);
  162. return closest_pair;
  163. }