Merge branch 'divide_and_conquer' of UBB/points into master

This commit is contained in:
Chris Cromer 2018-12-12 18:51:24 -03:00 committed by Gitea
commit 9c6c1c9d1a
7 changed files with 234 additions and 13 deletions

View File

@ -1,7 +1,7 @@
CC=gcc
CFLAGS=-Wall -Isrc/include -DDEBUG -g
LDFLAGS=-lm
SRC=src/points.c src/timer.c src/read_file.c src/distance.c src/brute_force.c
SRC=src/points.c src/timer.c src/read_file.c src/distance.c src/brute_force.c src/divide_and_conquer.c
OBJ=$(SRC:.c=.o)
all: points informe

View File

@ -23,5 +23,5 @@
* @return Retorna la distancia entre los 2 puntos
*/
double distance(point_t point1, point_t point2) {
return sqrt(pow(point1.x - point2.x, 2) + pow(point1.y - point2.y, 2));
return pow(point1.x - point2.x, 2) + pow(point1.y - point2.y, 2);
}

181
src/divide_and_conquer.c Normal file
View File

@ -0,0 +1,181 @@
/*
* Copyright 2018 Christopher Cromer
* Copyright 2018 Rodolfo Cuevas
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 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.
*
* 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.
*
* 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.
*/
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <float.h>
#include "points.h"
#include "brute_force.h"
#include "distance.h"
/**
* Comparar 2 double para ver cual es mayor o si son iguales
* @param a El primer double a comparar
* @param b El segundo double a comparar
* @return Retorna 1 si a es mayor de b, -1 si a es menor de b ó 0 si son igual
*/
int compare_double(double a, double b) {
if (a > b) {
return 1;
}
else if (a < b) {
return -1;
}
else {
return 0;
}
}
/**
* Comparar los el eje x de 2 puntos
* @param a El primer punto a comparar
* @param b El segundo punto a comparar
* @return Retorna la comparación entre los 2 puntos
*/
int compare_x(const void * a, const void * b) {
return compare_double((*(point_t *) a).x, (*(point_t *) b).x);
}
/**
* Comparar los el eje y de 2 puntos
* @param a El primer punto a comparar
* @param b El segundo punto a comparar
* @return Retorna la comparación entre los 2 puntos
*/
int compare_y(const void * a, const void * b) {
return compare_double((*(point_t *) a).y, (*(point_t *) b).y);
}
/**
* El algoritmo de encontrar 2 puntos recursivo usando divide and conquer
* @param points_x Los puntos ordenado en el eje x
* @param nx La cantidad de puntos en el array points_y
* @param points_y Los puntos ordenado en el eje y
* @param ny La cantidad de puntos en el array points_y
* @param closest_pair Las 2 pares de puntos mas cercano
* @return Retorna la distance entre los dos puntos mas cercano
*/
double divide_and_conquer_run(point_t *points_x, unsigned int nx, point_t *points_y, unsigned int ny, point_t *closest_pair) {
int left;
int right;
int i;
double mid;
double d = DBL_MAX;
double min_d = DBL_MAX;
double x0;
double x1;
double x;
point_t *closest_pair2;
point_t *points_y2;
if (nx <= 4) {
closest_pair2 = brute_force(points_x, nx, &d);
closest_pair[0] = closest_pair2[0];
closest_pair[1] = closest_pair2[1];
return d;
}
closest_pair2 = malloc(sizeof(point_t) * 2);
points_y2 = malloc(sizeof(point_t) * ny);
mid = points_x[nx / 2].x;
left = -1;
right = ny;
for (i = 0; i < ny; i++) {
if (points_y[i].x < mid) {
points_y2[++left] = points_y[i];
}
else {
points_y2[--right]= points_y[i];
}
}
for (i = ny - 1; right < i; right ++, i--) {
closest_pair2[0] = points_y2[right];
points_y2[right] = points_y2[i];
points_y2[i] = closest_pair2[0];
}
min_d = divide_and_conquer_run(points_x, nx / 2, points_y2, left + 1, closest_pair);
d = divide_and_conquer_run(points_x + nx / 2, nx - nx / 2, points_y2 + left + 1, ny - left - 1, closest_pair2);
if (d < min_d) {
min_d = d;
closest_pair[0] = closest_pair2[0];
closest_pair[1] = closest_pair2[1];
}
d = sqrt(min_d);
free(closest_pair2);
left = -1; right = ny;
for (i = 0; i < ny; i++) {
x = points_y[i].x - mid;
if (x <= -d || x >= d) {
continue;
}
if (x < 0) {
points_y2[++left] = points_y[i];
}
else {
points_y2[--right] = points_y[i];
}
}
while (left >= 0) {
x0 = points_y2[left].y + d;
while (right < ny && points_y2[right].y > x0) {
right ++;
}
if (right >= ny) {
break;
}
x1 = points_y2[left].y - d;
for (i = right; i < ny && points_y2[i].y > x1; i++)
if ((x = distance(points_y2[left], points_y2[i])) < min_d) {
min_d = x;
closest_pair[0] = points_y2[left];
closest_pair[1] = points_y2[i];
}
left--;
}
free(points_y2);
return min_d;
}
/**
* Encontrar los 2 puntos más cercano usando el metodo de dividir para conquistar
* @param point_t Los puntos a calcular
* @param n La cantidad de puntos en el array point_ts
* @param minimum_dist La distancia minimo encontrado
* @return Retorna los 2 puntos mas cercanos
*/
point_t * divide_and_conquer(point_t *points, unsigned int n, double *minimum_dist) {
point_t *closest_pair = malloc(sizeof(point_t) * 2);
point_t *points_x = malloc(sizeof(point_t) * n);
point_t *points_y = malloc(sizeof(point_t) * n);
memcpy(points_x, points, sizeof(point_t) * n);
memcpy(points_y, points, sizeof(point_t) * n);
qsort(points_x, n, sizeof(point_t), compare_x);
qsort(points_y, n, sizeof(point_t), compare_y);
*minimum_dist = divide_and_conquer_run(points_x, n, points_y, n, closest_pair);
free(points_x);
free(points_y);
return closest_pair;
}

View File

@ -0,0 +1,19 @@
/*
* Copyright 2018 Christopher Cromer
* Copyright 2018 Rodolfo Cuevas
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 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.
*
* 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.
*
* 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.
*/
#ifndef _POINTS_DIVIDE_AND_CONQUER
#define _POINTS_DIVIDE_AND_CONQUER
point_t * divide_and_conquer(point_t *points, unsigned int n, double *dist);
#endif

View File

@ -18,11 +18,13 @@
#include <getopt.h>
#include <string.h>
#include <float.h>
#include <math.h>
#include "points.h"
#include "distance.h"
#include "timer.h"
#include "read_file.h"
#include "brute_force.h"
#include "divide_and_conquer.h"
#define POINTS_VERSION "1.0.0"
@ -163,22 +165,22 @@ int main (int argc, char **argv) {
start_algorithm("Brute force corriendo... ", n);
closest_points = brute_force(points, n, &dist);
end_algorithm();
printf("point 1: x: %f y: %f\n", closest_points[0].x, closest_points[0].y);
printf("point 2: x: %f y: %f\n", closest_points[1].x, closest_points[1].y);
printf("distance: %lf\n\n", dist);
fprintf(stdout, "point 1: x: %f y: %f\n", closest_points[0].x, closest_points[0].y);
fprintf(stdout, "point 2: x: %f y: %f\n", closest_points[1].x, closest_points[1].y);
fprintf(stdout, "distance: %lf\n\n", sqrt(dist));
free(closest_points);
closest_points = NULL;
}
if (divide) {
start_algorithm("Divide and conquer corriendo... ", n);
//closest_points = divide_and_conquer(points, n, &dist);
closest_points = divide_and_conquer(points, n, &dist);
end_algorithm();
/*printf("point 1: x: %f y: %f\n", closest_points[0].x, closest_points[0].y);
printf("point 2: x: %f y: %f\n", closest_points[1].x, closest_points[1].y);
printf("distance: %lf\n", dist);
fprintf(stdout, "point 1: x: %f y: %f\n", closest_points[0].x, closest_points[0].y);
fprintf(stdout, "point 2: x: %f y: %f\n", closest_points[1].x, closest_points[1].y);
fprintf(stdout, "distance: %lf\n", sqrt(dist));
free(closest_points);
closest_points = NULL;*/
closest_points = NULL;
}
free(points);

View File

@ -4,7 +4,7 @@ LDFLAGS=-lm
SRC=test.c
OBJ=$(SRC:.c=.o)
OBJ+=../src/read_file.o ../src/distance.o ../src/brute_force.o
OBJ+=../src/read_file.o ../src/distance.o ../src/brute_force.o ../src/divide_and_conquer.o
all: test

View File

@ -22,6 +22,7 @@
#include "points.h"
#include "read_file.h"
#include "brute_force.h"
#include "divide_and_conquer.h"
/**
* Comparar 2 double para ver si son casi igual
@ -29,7 +30,7 @@
* @param d2 El segudno double
* @return Retorna 1 si son igual o 0 sino
*/
int compare_double(double d1, double d2) {
inline int compare_double(double d1, double d2) {
double precision = 0.000000001;
if (((d1 - precision) < d2) && ((d1 + precision) > d2)) {
return 1;
@ -71,7 +72,25 @@ int main(int argc, char **argv) {
fprintf(stdout, "\tbrute force: ");
fflush(stdout);
brute_force(points, n, &dist);
if (compare_double(dist, 0.067687840)) {
if (compare_double(sqrt(dist), 0.067687840)) {
fprintf(stdout, "passed\n");
passed++;
}
else {
fprintf(stdout, "failed\n");
failed++;
}
}
if (failed > 0) {
fprintf(stdout, "\tdivide and conquer: failed\n");
failed++;
}
else {
fprintf(stdout, "\tdivide and conquer: ");
fflush(stdout);
divide_and_conquer(points, n, &dist);
if (compare_double(sqrt(dist), 0.067687840)) {
fprintf(stdout, "passed\n");
passed++;
}