0% found this document useful (0 votes)
65 views6 pages

Closest Pair Problem

This document discusses three algorithms for finding the closest pair of points in a one-dimensional array: brute force, iterative, and recursive. The brute force method uses nested loops to calculate all distances, giving it O(n^2) time complexity. The iterative method sorts the array in O(n log n) time before iterating through to find the minimum distance, resulting in O(n log n) time complexity. The recursive method recursively divides the array into sub-arrays and finds the closest distances within them and between them, also with O(n log n) time complexity. Sample C++ code is provided to implement these three algorithms.

Uploaded by

Air
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views6 pages

Closest Pair Problem

This document discusses three algorithms for finding the closest pair of points in a one-dimensional array: brute force, iterative, and recursive. The brute force method uses nested loops to calculate all distances, giving it O(n^2) time complexity. The iterative method sorts the array in O(n log n) time before iterating through to find the minimum distance, resulting in O(n log n) time complexity. The recursive method recursively divides the array into sub-arrays and finds the closest distances within them and between them, also with O(n log n) time complexity. Sample C++ code is provided to implement these three algorithms.

Uploaded by

Air
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

//CLOSEST PAIR PROBLEM//

Name:Setty Rajeshwar Rao


Registration no:19MCS0054

AIM: To find the closest pair of the given one-dimensional array using: -
 Brute Force Method
 Iterative Method
 Recursive Method

 Explanation:
 In order to find the closest distance within a given points we have applied a nested loop to find
the distance between two points in a Brute-Force-method.
 The worst case, average case and best case is O (n2 ). Same for all the cases.

 One Dimentional Iterative: In one dimensional iterative method we check the


closest distance of the given inputs by using looping concept.
 In this method it takes O(log n) to sort the array and O(n) time complexity to find the closest
distance .
 The worst time complexity of this method is O(n log n).

 ONE Dimentional Recursive: In one dimensional recursive method it divides the


given array into sub array using divide and conquer concept and sort the array .

 In this method it finds the closest distance in the sub-arrays and compare the distance of the
sub array to find the least distance.

 The time complexity of the one dimensional recursive method is O (n log n).

// Program :
#include<iostream>
#include<cmath>
#include<limits>
using namespace std;
class Point
{
private:
unsigned int dim;
double* comp;
public:
// parameterless ctor
Point()
{
dim = 1;
comp = new double[dim];

}
//parameterized ctor
Point(unsigned int d)
{
dim = d;
comp = new double[dim];
}
// Copy Contructor
Point(const Point& other)
{
dim = other.dim;
comp = new double[dim];
for (int i = 0;i < dim;i++)
{
comp[i] = other.comp[i];
}
}
// operator overloading =
Point operator=(const Point& pt)
{
if (this != &pt)
{
dim = pt.dim;
if (comp != NULL)
{
delete[] comp;
comp = NULL;
}
comp = new double[dim];
for (int i = 0;i < dim;i++)
{
comp[i] = pt.comp[i];
}
}
return (*this);
}
friend ostream& operator<<(ostream& out, const Point& pt)
{
out << "(";
for (int i = 0;i < pt.dim;i++) {
out << pt.comp[i] << ", ";
}
out << "\b\b";
out << ")\n";
return out;
}
friend istream& operator>>(istream & in, Point & pt)
{
for (int i = 0;i < pt.dim;i++)
{
cout << "\tEnter component " << (i + 1) << " :";
in >> pt.comp[i];
}
return in;
}
// dtor
~Point()
{
delete[] comp;
comp = NULL;
}
double Edist(const Point& pt)
{
double result = 0.0;
for (int i = 0;i < dim;i++)
{
result += pow(this->comp[i] - pt.comp[i], 2);
}
return sqrt(result);
}
friend double BFM(Point* pts, unsigned int npts)

{
double mdist = std::numeric_limits<double>::infinity();
// Insert your code here
for (int i = 0;i < npts - 1;i++) {
for (int j = i + 1;j < npts;j++) {
double dist = pts[i].Edist(pts[j]);
if (dist < mdist)
{
mdist = dist;
}
}
}
return mdist;
}
friend int compareX(const void* p_1, const void* p_2);
friend double OneDCPD(Point* pts, int npts)
{
double mdist = numeric_limits<double>::infinity();
// Insert your code here
for (int i = 0;i < npts - 1;i++) {
double dist = pts[i].Edist(pts[i + 1]);
if (dist < mdist)
{
mdist = dist;
}
}
return mdist;
}
};
int compareX(const void* p_1, const void* p_2)
{
Point* p1 = (Point*)p_1;
Point* p2 = (Point*)p_2;
return (p1->comp[0] - p2->comp[0]);
}
double ROneDCPD(Point* pts, int npts)
{
//Insert your code
int mid = npts / 2;
if (npts == 1)
{
return numeric_limits<double>::infinity();
}
if (npts == 2 || npts == 3)
{
return BFM(pts, npts);
}
double min_l = ROneDCPD(pts, mid);
double min_r = ROneDCPD(&pts[mid], npts - mid);
double min_lr = numeric_limits<double>::infinity();
if (min_l <= min_r)
{
min_lr = min_l;
}
else
{
min_lr = min_r;
}
double dist = pts[mid].Edist(pts[mid - 1]);
if (dist < min_lr)
{
return dist;
}
return min_lr;
}
int main()
{
Point p[10];
for (int i = 0;i < 10;i++)
{
cin >> p[i];
cout << p[i];
}
cout << "C - Pair distance(BFM) O(n ^ 2) : " << BFM(p, 10) << endl;
qsort(p, 10, sizeof(Point), compareX);
cout << "1D C-Pair distance (O(nlogn)):" << OneDCPD(p, 10) << endl;
cout << "1D Recursive C-Pair distance O(nlogn):" << ROneDCPD(p, 10) << endl;
return 0;
}

Output:

You might also like