Skip to content

Commit 17ab4ba

Browse files
committed
Added findClosestPair()
1 parent 0f25355 commit 17ab4ba

File tree

6 files changed

+99
-1
lines changed

6 files changed

+99
-1
lines changed

Diff for: closest_pair_problem/bruteForce.c

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "header_files/declarations.h"
2+
3+
PairedPoints* bruteForce(Point* points, int left, int right) {
4+
5+
int minDistance = INT_MAX;
6+
double calculatedDistance;
7+
PairedPoints* closestPaired = (PairedPoints*)malloc(sizeof(PairedPoints));
8+
9+
for (int i = left; i <= right; i++) {
10+
for (int j = i + 1; j <= right; j++) {
11+
calculatedDistance = calculateDistance(points[i], points[j]);
12+
if (calculatedDistance < minDistance) {
13+
closestPaired->p1 = points[i];
14+
closestPaired->p2 = points[j];
15+
closestPaired->distance = calculatedDistance;
16+
}
17+
}
18+
}
19+
20+
return closestPaired;
21+
22+
}

Diff for: closest_pair_problem/calculateDistance.c

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "header_files/declarations.h"
2+
3+
int calculateDistance(Point p1, Point p2) {
4+
int diffX = p1.x - p2.x;
5+
int diffY = p1.y - p2.y;
6+
return sqrt(pow(diffX, 2) + pow(diffY, 2));
7+
}

Diff for: closest_pair_problem/findClosestPair.c

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "header_files/declarations.h"
2+
3+
PairedPoints findClosestPair(Point* points, int left, int right) {
4+
5+
int numOfPoints = (right - left) + 1;
6+
if (numOfPoints <= 3)
7+
return *bruteForce(points, left, right);
8+
9+
int mid = (left + right) / 2;
10+
11+
PairedPoints minPPL = findClosestPair(points, left, mid);
12+
PairedPoints minPPR = findClosestPair(points, mid + 1, right);
13+
14+
PairedPoints minPP;
15+
if (minPPL.distance < minPPR.distance)
16+
minPP = minPPL;
17+
else
18+
minPP = minPPR;
19+
20+
Point split[MAX];
21+
int j = 0;
22+
for (int i = left; i <= right; i++) {
23+
if (abs(points[i].x - points[mid].x) < minPP.distance) {
24+
split[j] = points[i];
25+
j++;
26+
}
27+
}
28+
29+
PairedPoints PPM = lookNearOfMid(split, j, minPP.distance);
30+
31+
if (PPM.distance != INT_MIN)
32+
return PPM;
33+
else
34+
return minPP;
35+
36+
}

Diff for: closest_pair_problem/header_files/common.h

+4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
#include <stdio.h>
44
#include <stdlib.h>
5+
#include <math.h>
56

67
#define TRUE 1
78
#define FALSE 0
89

910
#define x_axis 1
1011
#define y_axis 0
1112

13+
#define MAX 2000 //for split array
14+
1215

1316
typedef struct {
1417
int x;
@@ -18,5 +21,6 @@ typedef struct {
1821
typedef struct {
1922
Point p1;
2023
Point p2;
24+
double distance;
2125
}PairedPoints;
2226

Diff for: closest_pair_problem/header_files/declarations.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@ int eliminateSamePoints(Point* arr, int N);
77
void printPoints(Point* points, int N);
88
void quickSort(Point arr[], int first, int last, int axis);
99
int partition(Point arr[], int first, int last, int axis);
10-
int swap(int arr[], int index1, int index2);
10+
int swap_x_axis(Point arr[], int index1, int index2);
11+
int swap_y_axis(Point arr[], int index1, int index2);
12+
PairedPoints* bruteForce(Point* points, int left, int right);
13+
int calculateDistance(Point p1, Point p2);
14+
PairedPoints lookNearOfMid(Point split[], int size, int minDistance);
15+
PairedPoints findClosestPair(Point* points, int left, int right);

Diff for: closest_pair_problem/lookNearOfMid.c

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "header_files/declarations.h"
2+
3+
PairedPoints lookNearOfMid(Point split[], int size, int minDistance) {
4+
5+
quickSort(split, 0, size - 1, y_axis);
6+
printPoints(split, size);
7+
PairedPoints closestPairedPoints;
8+
closestPairedPoints.distance = INT_MIN; //to control
9+
10+
for (int i = 0; i < size; i++) {
11+
for (int j = i + 1; j < size; j++) {
12+
if (abs(split[i].y - split[j].y) < minDistance) {
13+
closestPairedPoints.p1 = split[i];
14+
closestPairedPoints.p2 = split[j];
15+
closestPairedPoints.distance = calculateDistance(split[i], split[j]);
16+
}
17+
18+
19+
}
20+
}
21+
22+
return closestPairedPoints;
23+
24+
}

0 commit comments

Comments
 (0)