Divide-Conquer-Glue Algorithms 5. D IVIDE AND C ONQUER Closest Pair - - PowerPoint PPT Presentation

divide conquer glue algorithms
SMART_READER_LITE
LIVE PREVIEW

Divide-Conquer-Glue Algorithms 5. D IVIDE AND C ONQUER Closest Pair - - PowerPoint PPT Presentation

Divide-Conquer-Glue Algorithms 5. D IVIDE AND C ONQUER Closest Pair mergesort Tyler Moore counting inversions closest pair of points CSE 3353, SMU, Dallas, TX randomized quicksort Lecture 12 median and selection


slide-1
SLIDE 1

Divide-Conquer-Glue Algorithms

Closest Pair Tyler Moore

CSE 3353, SMU, Dallas, TX

Lecture 12

Some slides created by or adapted from Dr. Kevin Wayne. For more information see http://www.cs.princeton.edu/~wayne/kleinberg-tardos. Some code reused from Python Algorithms by Magnus Lie Hetland.

  • 5. DIVIDE AND CONQUER
  • mergesort
  • counting inversions
  • closest pair of points
  • randomized quicksort
  • median and selection

2 / 19

21

  • Closest pair problem. Given points in the plane, find a pair of points

with the 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.

fast closest pair inspired fast algorithms for these problems

3 / 19

22

  • Closest pair problem. Given points in the plane, find a pair of points

with the smallest Euclidean distance between them. Brute force. Check all pairs with Θ distance calculations. 1d version. Easy algorithm if points are on a line. Nondegeneracy assumption. No two points have the same -coordinate.

4 / 19

slide-2
SLIDE 2

Closest-pair problem in one dimension

In the closest-pair problem, you are to select the pair of points (p1, p2) from a set S that are closest to each other.

dmin l1 l2 l3 r3 r1 r2

Sl = {l1, l2, l3} Sr = {r1, r2, r3}

m

dmin dmin

5 / 19

Closest-pair problem has divide-and-conquer solution

A divide-and-conquer algorithm works as follows.

1 Base Case: If the list contains two points, then they must be the

closest pair.

2 Divide: Divide the set into two halves (e.g., Sl and Sr in the figure

above). Put all points less than the midpoint m in Sl and all points greater than or equal to the midpoint in Sr.

3 Conquer: Find the closest-pair for each half ((l1, l2) for Sl and

(r1, r2) for Sr).

4 Glue: To find the closest pair in the entire set, select from 3 options: 1

closest pair in the left half ((l1, l2));

2

closest pair in the right half ((r1, r2));

3

a pair with one point each from Sl and Sr.

6 / 19

1D Glue procedure

A divide-and-conquer algorithm works as follows. Glue: To find the closest pair in the entire set, select from 3 options:

1

closest pair in the left half ((l1, l2));

2

closest pair in the right half ((r1, r2));

3

a pair with one point each from Sl and Sr.

For the closest pair to take a point from both sets, each point must be within distance dmin of the midpoint m between the two sets (here dmin = min(distance(l1, l2), distance(r1, r2))). Only the largest point in the left set lmax and the smallest point in the right set rmin could be closer than dmin. Compute distance(lmax,rmin) and update the closest pair if less than dmin.

7 / 19

Sorting solution.

Sort by -coordinate and consider nearby points. Sort by -coordinate and consider nearby points.

23

  • 8 / 19
slide-3
SLIDE 3

Sorting solution.

Sort by -coordinate and consider nearby points. Sort by -coordinate and consider nearby points.

24

  • 8

9 / 19

25

  • Divide. Subdivide region into 4 quadrants.

10 / 19

26

  • Divide. Subdivide region into 4 quadrants.
  • Obstacle. Impossible to ensure points in each piece.

11 / 19

27

  • Divide: draw vertical line so that 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.

seems like Θ(N2)

L

12 21 8

12 / 19

slide-4
SLIDE 4

Find closest pair with one point in each side, assuming that distance < δ.

Observation: only need to consider points within δ of line .

28

  • L

δ δ

12 21

13 / 19

29

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

Observation: only need to consider points within δ of line . Sort points in δ-strip by their -coordinate. Only check distances of those within positions in sorted list!

L δ

2 3 4 5 6 7

why 11?

1

δ

21 12

14 / 19

30

  • Def. Let be the point in the δ-strip, with the smallest -coordinate.
  • Claim. If ≥, then the distance

between and is at least δ. Pf.

No two points lie in same ½δ-by-½δ box. Two points at least rows apart

have distance ≥½δ. ▪

  • Fact. Claim remains true if we replace with .

δ

27 29 30 31 28 26 25

δ

2 rows

½δ ½δ ½δ

39

i j

⋮ ⋮

15 / 19

31

  • δ←

δ← δ←δδ δ

  • δδ

δ

  • 16 / 19
slide-5
SLIDE 5

32

  • Theorem. The divide-and-conquer algorithm for finding the closest pair of

points in the plane can be implemented in time. Lower bound. In quadratic decision tree model, any algorithm for closest pair (even in 1D) requires Ω quadratic tests.

  • Θ
  • ⎡⎤⎣⎦
  • 17 / 19

33

  • Q. How to improve to ?
  • A. Yes. Don't sort points in strip from scratch each time.

Each recursive returns two lists: all points sorted by -coordinate,

and all points sorted by -coordinate.

Sort by merging two pre-sorted lists.

  • Theorem. [Shamos 1975] The divide-and-conquer algorithm for finding the

closest pair of points in the plane can be implemented in time. Pf.

  • Note. See SECTION 13.7 for a randomized time algorithm.

Θ

  • ⎡⎤⎣⎦Θ
  • not subject to lower bound

since it uses the floor function

18 / 19

2D Divide-Conquer-Glue

(1, 1) (2, 6) (3, 2) (4, 5) (6, 1) (6, 5) (9, 4)

Step 0: Get Sorted Lists seq = [(4,5),(1,1),(6,5),(3,2),(6,1),(9,4),(2,6)] seqx = [(1,1),(2,6),(3,2),(4,5),(6,1),(6,5),(9,4)] seqy = [(1,1),(6,1),(3,2),(9,4),(6,5),(4,5),(2,6)]

19 / 19

2D Divide-Conquer-Glue

(1, 1) (2, 6) (3, 2) (4, 5) (6, 1) (6, 5) (9, 4)

Divide

m = 5

lftx = [(1,1),(2,6),(3,2),(4,5)], rgtx=[(6,1),(6,5),(9,4)] lfty = [(1,1),(3,2),(4,5),(2,6)], rgty=[(6,1),(9,4),(6,5)] Conquer: invoke cpp helper recursively with left and right halves

19 / 19

slide-6
SLIDE 6

2D Divide-Conquer-Glue

(1, 1) (2, 6) (3, 2) (4, 5) (6, 1) (6, 5) (9, 4)

Glue

L(dmin) R(dmin)

lfty = [(1,1),(3,2),(4,5),(2,6)], rgty=[(6,1),(9,4),(6,5)]

m = 5 dmin (3, 2) (4, 5) (6, 1) (6, 5)

ymin = [(6,1),(3,2),(6,5),(4,5)] newy = [(1,1),(6,1),(3,2),(9,4),(6,5),(4,5),(2,6)] Check 6 neighbors of every point in ymin for distance < dmin

d

min < dmin

19 / 19