Closest Pair of Points in the Plane Inge Li Grtz Thank you to Kevin - - PowerPoint PPT Presentation

closest pair of points in the plane
SMART_READER_LITE
LIVE PREVIEW

Closest Pair of Points in the Plane Inge Li Grtz Thank you to Kevin - - PowerPoint PPT Presentation

Closest Pair of Points Closest pair of points. Given n points in the plane, find a pair with smallest euclidean distance between them. Closest Pair of Points in the Plane Inge Li Grtz Thank you to Kevin Wayne for inspiration to slides. The


slide-1
SLIDE 1

Closest Pair of Points in the Plane

Inge Li Gørtz

Thank you to Kevin Wayne for inspiration to slides. The slides on the deterministic algorithm for finding the closest pair of points is a modification of slides made by Kevin.

  • Closest pair of points. Given n points in the plane, find a pair with smallest

euclidean distance between them.

Closest Pair of Points

Thank you to Kevin Wayne for inspiration to slides.

  • Closest pair of points. Given n points in the plane, find a pair with smallest euclidean

distance between them.

  • Fundamental geometric primitive.
  • Graphics, computer vision, geographic information systems, molecular modeling,

air traffic control.

  • Special case of nearest neighbor, Euclidean MST, Voronoi diagrams.
  • Brute force. Compare all pairs => O(n2) time.
  • 1-D version. Sort and scan => O(n log n) time.
  • Simplifying assumption. No two points coincide (for a simpler presentation).

Closest Pair of Points

Thank you to Kevin Wayne for inspiration to slides.

Closest pair of points

A divide-and-conquer algorithm

Thank you to Kevin Wayne for inspiration to slides. The slides on the deterministic algorithm for finding the closest pair of points is a modification of the slides made by Kevin.

slide-2
SLIDE 2
  • Divide:
  • Conquer:
  • Combine:

Closest pair: Divide-and-Conquer

Thank you to Kevin Wayne for inspiration to slides.

  • Divide: draw vertical line L so that roughly n/2 points on each side.
  • Conquer:
  • Combine:

Closest pair: Divide-and-Conquer

L

Thank you to Kevin Wayne for inspiration to slides.

  • Divide: draw vertical line L so that roughly n/2 points on each side.
  • Conquer: find closest pair in each side recursively.
  • Combine:

Closest pair: Divide-and-Conquer

L 12 21

Thank you to Kevin Wayne for inspiration to slides.

  • Divide: draw vertical line L so that roughly n/2 points on each side.
  • Conquer: find closest pair in each side recursively.
  • Combine:
  • Find closest pair with one point in each side.
  • Return best of 3 solutions

Closest pair: Divide-and-Conquer

L 12 21 8

seems like Θ(n2)

Thank you to Kevin Wayne for inspiration to slides.

slide-3
SLIDE 3
  • Find closest pair with one point in each side, assuming that distance < δ.

Find closest pair with point on each side

12 21 δ = min(12, 21) L

Thank you to Kevin Wayne for inspiration to slides.

  • Find closest pair with one point in each side, assuming that distance < δ.
  • Observation: only need to consider points within δ of line L.

Find closest pair with point on each side

12 21 δ L δ = min(12, 21)

Thank you to Kevin Wayne for inspiration to slides.

  • Find closest pair with one point in each side, assuming that distance < δ.
  • Observation: only need to consider points within δ of line L.
  • Sort points in 2δ-strip by their y coordinate.

Find closest pair with point on each side

12 21 δ L δ = min(12, 21)

1 2 3 4 5 6 7

Thank you to Kevin Wayne for inspiration to slides.

  • Find closest pair with one point in each side, assuming that distance < δ.
  • Observation: only need to consider points within δ of line L.
  • Sort points in 2δ-strip by their y coordinate.
  • Only check distances between these points within 7 positions in sorted

list!

Find closest pair with point on each side

12 21 δ L δ = min(12, 21)

1 2 3 4 5 6 7

Thank you to Kevin Wayne for inspiration to slides.

slide-4
SLIDE 4
  • Def. Let si be the point in the 2δ-strip, with


the ith smallest y-coordinate.

  • Claim. If |i – j| ≥ 8, then the distance between


si and sj is at least δ.

  • Pf.
  • sj at most δ apart from si then the difference in

y-coordinate is at most δ.

  • No two points lie in same ½δ-by-½δ box:
  • At most 7 points within distance δ (one in each
  • ther box). ▪

Find closest pair with point on each side

δ

29 30 31 28 26 25

δ

½δ ½δ

39

i j

27

δ

s✓1 2δ ◆2 + ✓1 2δ ◆2 = r 1 2 δ ≈ 0.7 δ < δ

Thank you to Kevin Wayne for inspiration to slides.

Closest Pair Algorithm

Closest-Pair(p1, …, pn) { If n < 4 compute closest pair by comparing all pairs. Compute separation line L such that half the points
 are on one side and half on the other side. δ1 = Closest-Pair(left half) δ2 = Closest-Pair(right half) δ = min(δ1, δ2) Delete all points further than δ from separation line L Sort remaining points by y-coordinate. Scan points in y-order and compare distance between
 each point and next 7 neighbors. If any of these
 distances is less than δ, update δ. return δ. } O(n log n) 2T(n / 2) O(n) O(n log n) O(n)

Thank you to Kevin Wayne for inspiration to slides.

  • Analysis:
  • T(n) = 2T(n/2) + O(n log n), for n > 4. T(n) = O(1), for n ≤ 4.
  • T(n) = O(n log2 n).
  • Can improve this by pre-sorting points:
  • Start by constructing 2 sorted lists X and Y containing all points sorted

after x- and y-coordinate, resp.

  • Divide:
  • Split X-array in middle.
  • Use linear time to split Y-array into 2 (according to x-coordinate).
  • Combine:
  • Prune Y-array (only consider points with x-coordinate within δ of L).

Closest Pair Algorithm Closest Pair Algorithm

Presort points into lists X and Y after x- and y-coordinate, respectively. Closest-Pair(X[1…n],Y[1…n]) { If n < 4 compute closest pair by comparing all pairs. Compute separation line L such that half the points
 are on one side and half on the other side. δ1 = Closest-Pair(left half) δ2 = Closest-Pair(right half) δ = min(δ1, δ2) Delete all points further than δ from separation line L Sort remaining points by y-coordinate. Scan points in y-order and compare distance between
 each point and next 7 neighbors. If any of these
 distances is less than δ, update δ. return δ. }

O(n) 2T(n / 2) O(n) O(n) O(n)

slide-5
SLIDE 5
  • Analysis:
  • Total time: T(n) + O(n log n).
  • T(n) = 2T(n/2) + O(n), n>4.
  • T(n) = O(1), n ≤ 4.
  • Thus T(n) = O(n log n).
  • In total O(n log n).

Closest Pair Algorithm

Closest pair of points

A randomized algorithm

  • Assume wlog that points are in the unit square.
  • Sort points in random order.
  • Let δ = d(p1,p2). Check for each point pi (in order) if there exists a point pj, j<i, such that

d(pi,pj) < δ.

  • If such a point found. Update δ.

Randomized algorithm

3 4 11 9 10 15 5 1 2 14 12 6 13 8 7 δ2 δ1

How to check a point

3 4 11 9 10 15 5 1 2 14 6 13 8 7 δ δ/2 δ/2 12

  • δ current smallest distance. Divide unit square into subsquares with side lengths δ/2.
slide-6
SLIDE 6

How to check a point

3 4 11 9 10 15 5 1 2 14 6 13 8 7 δ/2 δ/2 12

  • δ current smallest distance. Divide unit square into subsquares with side lengths δ/2.
  • If two points i and j are in the same subsquare then d(i,j) < δ.

How to check a point

3 4 11 9 10 15 5 1 2 14 6 13 8 7 δ/2 δ/2 12

  • δ current smallest distance. Divide unit square into subsquares with side lengths δ/2.
  • If two points i and j are in the same subsquare then d(i,j) < δ.
  • If d(i,j) < δ then j is in the 5x5 grid of subsquares around i.
  • Use hashtable to store which square a point is in. Only store points already

looked at (red points).

Closest Pair of Points: Randomized algorithm

3 4 11 9 10 15 5 1 2 14 12 6 13 8 7 δ2 δ1

Closest Pair of Points

3 4 11 9 10 15 5 1 2 14 12 6 13 8 7 δ2 δ3

  • Use hashtable to store which square a point is in. Only store points already

looked at (red points).

  • When starting new round: rehash all points from 1…i.
slide-7
SLIDE 7

Closest Pair of Points

3 4 11 9 10 15 5 1 2 14 12 6 13 8 7 δ3

  • Use hashtable to store which square a point is in. Only store points already

looked at (red points).

  • When starting new round: rehash all points from 1…i.
  • Number of lookup operations:
  • Number of distance calculations:
  • Number of MakeDictionary operations:

Closest Pair of Points: Analysis

  • Number of lookup operations:
  • Number of distance calculations:
  • Number of MakeDictionary operations:
  • Number of insertions:
  • Random variable X = number of insertions
  • Random variable
  • Pr[Xi = 1] ≤ 2/i
  • Expected number of insertions:
  • Use hashtable as dictionary: O(n) time in total.

Closest Pair of Points: Analysis

Xi = ( 1 i causes δ to change

  • therwise

E[X] = n +

n

X

i=1

i · E[Xi] = n +

n

X

i=1

i · Pr[Xi] ≤ n +

n

X

i=1

i · 2/i = n + 2n = 3n .