Skip to content

VarunJoshi10/Closest-Pair-Problem

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 

Repository files navigation

Closest-Pair-Problem

The repository includes a C++ solution for n dimensional closest pair problem.

Aim

Implement a solution to find a minimum distance between two points. Input array: each element defined as [a,b] for example [2,3] Use distance formula.

Solution

Bruteforce method

Brute force method is a simple approach and provides a solution easily implementable. Basic idea is to compare each n dimensional points with each other, and calculate the distances. Afterwards, select the minimum of the distances, and return the point pair it belongs to. This is a compute heavy method, where n(n-1)/2 comparison is needed. Using this algorithm the problem can be solved in O(n^2) time where n is the number of points. Implementation can be found under /closestpairproblem.cpp

Divide and conquer method

Divide and conquer method approach in n dimension is not trivial. Implementation steps should be the following: first, the input set (S) needs to be divided into two halves, then recursively find their minimum. Choose the smallest from the two minimums (δ). After, points of S are projected to H hyperplane, where S' represents points that are within δ to H. Using δ as sparsity condition, only O(n) pairs need to be recursively examined. Calculation can be done in O(n(log n)^d−1 time where d is dimension. The algorithm can be optimized up to O(n log n).

Steps to find closest pair

-1) We sort all points according to x coordinates.
-2) Divide all points in two halves.
-3) Recursively find the smallest distances in both subarrays.

-4) Take the minimum of two smallest distances. Let the minimum be d.
-5) Create an array strip[] that stores all points which are at most d distance away from the middle line dividing the two sets.

-6) Find the smallest distance in strip[].
-7) Return the minimum of d and the smallest distance calculated in above step 6.

Time Complexity

Let Time complexity of above algorithm be T(n). Let us assume that we use a O(nLogn) sorting algorithm. The above algorithm divides all points in two sets and recursively calls for two sets. After dividing, it finds the strip in O(n) time. Also, it takes O(n) time to divide the array around the mid vertical line. Finally finds the closest points in strip in O(n) time.
So T(n) can be expressed as follows
-T(n) = 2T(n/2) + O(n) + O(n) + O(n)
-T(n) = 2T(n/2) + O(n)
-T(n) = T(nLogn)

Test Cases

Test Case 1

Array[]={{2, 3}, {12, 30}, {40, 50}, {5, 1}, {12, 10}, {3, 4}}
Output-
The closest pair of points are (2,3) and (3,4)
The distance between them is 1.41421 units

Test Case 2

Array[]={{4, 1}, {15, 20}, {30, 40}, {8, 4}, {13, 11}, {5, 6}}
Output-
The closest pair of points are (8,4) and (5,6)
The distance between them is 3.60555 units

Test Case 3

Array[]={{-2,3}, {3,2}, {5,6}, {7,8}, {-5,-2}, {-7,-6}, {4,5}, {6,3}}
Output-
The closest pair of points are (4,5) and (5,6)
The distance between them is 1.41421 units

References

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages