Data Structures and Algorithms B
  • Introduction
  • Stable Marrage
  • Huffman Codes
    • ABL
    • Implementation
  • Graph Algorithms
    • Connect Component
    • Bipartiteness
    • Strongly Connected Components
      • Implementation
    • Topological Sort
      • Implementation 1
      • Implementation 2
    • Dijkstra’s Shortest Path
    • Minimum Spanning Tree
      • Prims
        • Implementation 1
      • Kruskels
        • Implementation 1
      • Traveling Salesman Problem
    • k-Clustering
    • Dynamic Programming with Trees
      • Implementation 1
      • Implementation 2
    • Disjoint Sets
      • Implementation
    • Eularian Cycle
      • Implementation
    • Hamiltonian Path
      • Implementation
  • Divide and Conquer
    • Merge Sort
    • Integer Multiplication
    • Closest Pair
    • Master Method
  • Dynamic Programming
    • Interval Scheduling
    • Knapsack Problem
  • Network Flow
    • Maximum Network Flow
    • Improvements
    • Image Segmentation
  • String Algorithms
    • Z Algorithm
    • Boyer-Moore
    • Knuth-Morris-Pratt
    • Suffix Trees
      • Naive Implementation
      • Ukkonens Algorithm
        • Implementation
      • Applications
        • Longest Common Substring
        • Longest Palindromic Substring
        • Longest Repeated Substring
  • Randomized Algorithms
    • Bloom Filters
      • Implementation
Powered by GitBook
On this page
  • Naive Approach
  • 1-D Solution
  • 2-D Solution
  • Divide and Conquer
  • Achieving nlogn:
  • Simplified

Was this helpful?

  1. Divide and Conquer

Closest Pair

PreviousInteger MultiplicationNextMaster Method

Last updated 5 years ago

Was this helpful?

Input:

  • A set of points (x,y) we'll call P; S=P1,P2,P3...PnS = {P_1, P_2, P_3 ... P_n}S=P1​,P2​,P3​...Pn​

Output:

  • Return the two points that are closest to each other.

  • We define closest by euclidean distance

    • min(d=(x2−x1)2+(y2−y1)2)min(d = \sqrt{(x_2-x_1)^2 + (y_2 - y_1)^2})min(d=(x2​−x1​)2+(y2​−y1​)2​)

Assumptions:

  • No duplicate points

Naive Approach

  • O(n2)O(n^2)O(n2)

  • Double for loop, calculate the distance between every possible point, maintains the minimum along the way.

  • It is worth noting that the upper bound is n^2, but really we are calculating nC2 combination pairs. For a visual of this, see Combinatorics chapter in ADT gitbook.

1-D Solution

  • While a brute force solution still exists, we can do better

  • If we limited our points in d-1 we can solve this in nlogn by:

  • Sorting -> nlogn

  • Scanning through it again, and then finding the minimum distance between two pairs of points. -> O(N)

2-D Solution

  • Motivated by our 1-d case, we may want to try and sort both the x and y coordinate sets by their dimension and then go through there.

    • But, we can easily derive an example where although 2 coordinates appear very per 1 axis, they are in reality a lot further in the other axis.

    • We find that our 1-dimensional approach will not work.

Divide and Conquer

  • Split the pairs of points vertically, such that there are the same amount of points on each side (if odd, differ by 1).

    • We accomplish this by sorting the points respectfully by x-cord, and y-cord - and finding the median.

  • Solve the closest pairs of points on 1st half, and then in the 2nd half.

Achieving nlogn:

  • The idea is to continue dividing our data in half until we reach the point where we have 2 or 3 points. Our return condition would be to then use brute force to find the closest pair of points for that call.

  • We do the same thing with the right hand side.

  • Then we find the minimum of the two sides, lets call this min_d

  • Finally, we only consider points from the vertical line + mid_d distance away. We can be assured that points further than mid_d from the median line are not the closest.

    • Here we find the distance between crossing points.

Simplified

  1. Sort points by x-cord <- Px

  2. Sort points by y-cord <- Py

  3. Split Px in half (median; size/2)

At this point if we calculate both sides, then it would take each (n/2)2(n/2)^2(n/2)2 for each. Which is still of order n2n^2n2. We have also yet to consider the closest pair of points that are considered between these two discrete sets.

http://www.geeksforgeeks.org/closest-pair-of-points-onlogn-implementation/
http://www.yovisto.com/video/9661