Docsity

Selection Algorithms and Closest Pair of Points, Slides of Computer Science

Partial preview of the text

Download Selection Algorithms and Closest Pair of Points and more Slides Computer Science in PDF only on Docsity!

Rank

Rank of an element is its position in ascending key order.

[2,6,7,8,10,15,18,20,25,30,35,40]

rank(2) = 0

rank(15) = 5

rank(20) = 7

Selection Problem

  • Given n unsorted elements, determine the k’th smallest element. That is, determine the element whose rank is k-1.
  • Applications

 Median score on a test.

  • k = ceil(n/2).  Median salary of Computer Scientists.  Identify people whose salary is in the bottom 10%. First find salary at the 10% rank.

D&C Selection Example

Use 3 as the pivot and partition.

rank(pivot) = 5. So pivot is the 6’th smallest element.

Find kth element of:

a 3 2 8 0 11 10 1 2 9 7 1

a 1 2 1 0 2 43 10 11 9 7 8

D&C Selection Example

  • If k = 6 (k-1 = rank(pivot)), pivot is the element we seek.
  • If k < 6 (k-1 < rank(pivot)), find k’th smallest element in left partition.
  • If k > 6 (k-1 > rank(pivot)), find (k- rank(pivot)-1)’th smallest element in right partition.

a 1 2 1 0 2 43 10 11 9 7 8

Closest Pair Of Points

  • Given n points in 2D, find the pair that are closest.

Applications

  • We plan to drill holes in a metal sheet.
  • If the holes are too close, the sheet will tear during drilling.
  • Verify that no two holes are closer than a threshold distance (e.g., holes are at least 1 inch apart). Docsity.com

Simple Solution

  • For each of the n(n-1)/2 pairs of points, determine the distance between the points in the pair.
  • Determine the pair with the minimum distance.
  • O(n 2 ) time.

Divide-And-Conquer Solution

  • When n is small, use simple solution.
  • When n is large  Divide the point set into two roughly equal parts A and B.  Determine the closest pair of points in A.  Determine the closest pair of points in B.  Determine the closest pair of points such that one point is in A and the other in B.  From the three closest pairs computed, select the one with least distance.

Example

  • Find closest pair in A.

A B

  • Let d 1 be the distance between the points in this pair.

d (^1)

Example

  • Find closest pair in B.

A B

d (^1)

  • Let d 2 be the distance between the points in this pair.

d (^2)

Example

  • Candidates lie within d of the dividing line.

A B

  • Call these regions RA and RB, respectively.

RA RB

Example

  • Let q be a point in RA.

A RA RB B

q

  • q need be paired only with those points in RB that are within d of q.y. Docsity.com

Example

A RA RB B

q

  • So the comparing region of q has at most 6 points.
  • So number of pairs to check is <= 6| RA | = O(n).

d

2d

Time Complexity

  • Create a sorted by x-coordinate list of points.

 O(n log n) time.

  • Create a sorted by y-coordinate list of points.

 O(n log n) time.

  • Using these two lists, the required pairs of points from RA and RB can be constructed in O(n) time.
  • Let n < 4 define a small instance.