cristobal ladron

This commit is contained in:
Rodolfo Cuevas 2018-12-12 11:43:12 -03:00
parent c6e5750649
commit 905d78b6a5
4 changed files with 190 additions and 1 deletions

151
src/divide_and_conquer.c Normal file
View File

@ -0,0 +1,151 @@
/*
* 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 "points.h"
#include "brute_force.h"
#include "distance.h"
/**
* Encontrar los 2 puntos más cercano usando el metodo de dividir para conquistar
* @param points Los puntos a calcular
* @param n La cantidad de puntos en el array points
* @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) {
if (n <= 3)
return brute_force(points, n, &minimum_dist);
int compareX(const void* a, const void* b){ //ordena el arreglo de puntos de acuerdo a X
Point *p1 = (Point *)a, *p2 = (Point *)b;
return (p1->x - p2->x);
}
int compareY(const void* a, const void* b){ //ordena el arreglo de puntos de acuerdo a Y
Point *p1 = (Point *)a, *p2 = (Point *)b;
return (p1->y - p2->y);
}
// Función para encontrar la distancia minima entre dos valores de tipo flotante
float min(float x, float y)
{
return (x < y)? x : y;
}
// Función para encontrar la distancia entre los puntos más cerca del arreglo dado
float stripClosest(Point strip[], int size, float d)
{
float min = d; // inicializa en la distancia minima d
qsort(strip, size, sizeof(Point), compareY);
for (int i = 0; i < size; ++i)
for (int j = i+1; j < size && (strip[j].y - strip[i].y) < min; ++j)
if (distance(strip[i],strip[j]) < min)
min = dist(strip[i], strip[j]);
return min;
}
// Función para encontrar la distancia más corta entre puntos
float closestUtil(Point P[], int n)
{
// Si hay igual o menos puntos a 3 utilizará el de fuerza bruta
if (n <= 3)
return bruteForce(P, n);
------
int mid = n/2;
Point midPoint = P[mid];
// Consider the vertical line passing through the middle point
// calculate the smallest distance dl on left of middle point and
// dr on right side
float dl = closestUtil(P, mid);
float dr = closestUtil(P + mid, n-mid);
// Find the smaller of two distances
float d = min(dl, dr);
// Build an array strip[] that contains points close (closer than d)
// to the line passing through the middle point
Point strip[n];
int j = 0;
for (int i = 0; i < n; i++)
if (abs(P[i].x - midPoint.x) < d)
strip[j] = P[i], j++;
// Find the closest points in strip. Return the minimum of d and closest
// distance is strip[]
return min(d, stripClosest(strip, j, d) );
}
// The main functin that finds the smallest distance
// This method mainly uses closestUtil()
float closest(Point P[], int n)
{
qsort(P, n, sizeof(Point), compareX);
// Use recursive function closestUtil() to find the smallest distance
return closestUtil(P, n);
}
return closest_pair;
}
--------
float closestUtil(Point P[], int n) /*Función recursiva para encontrar la distancia más pequeña. El arreglo
P contiene todos los puntos ordenados respecto a la cordenada X */
{
int mid = n/2;
Point midPoint = P[mid];
// Consider the vertical line passing through the middle point
// calculate the smallest distance dl on left of middle point and
// dr on right side
float dl = closestUtil(P, mid);
float dr = closestUtil(P + mid, n-mid);
// Find the smaller of two distances
float d = min(dl, dr);
// Build an array strip[] that contains points close (closer than d)
// to the line passing through the middle point
Point strip[n];
int j = 0;
for (int i = 0; i < n; i++)
if (abs(P[i].x - midPoint.x) < d)
strip[j] = P[i], j++;
// Find the closest points in strip. Return the minimum of d and closest
// distance is strip[]
return min(d, stripClosest(strip, j, d) );
}
// The main functin that finds the smallest distance
// This method mainly uses closestUtil()
float closest(Point P[], int n)
{
qsort(P, n, sizeof(Point), compareX);
// Use recursive function closestUtil() to find the smallest distance
return closestUtil(P, n);
}
// Driver program to test above functions
int main()
{
Point P[] = {{2, 3}, {12, 30}, {40, 50}, {5, 1}, {12, 10}, {3, 4}};
int n = sizeof(P) / sizeof(P[0]);
printf("The smallest distance is %f ", closest(P, n));
return 0;
}

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

@ -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
@ -81,6 +82,24 @@ int main(int argc, char **argv) {
}
}
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(dist, 0.067687840)) {
fprintf(stdout, "passed\n");
passed++;
}
else {
fprintf(stdout, "failed\n");
failed++;
}
}
fprintf(stdout, "%d tests passed\n", passed);
fprintf(stdout, "%d tests failed\n", failed);