Divide and Conquer in Practice: CS 306 Lecture notes

Computer Algorithms II Lecture Notes

4 December 2008 • Divide and Conquer in Practice


Outline

The Closest-Pair Problem

Dum and Fast

closest-pair(pts[], n)

  p1 = 0
  p2 = 1
  d = dist(pts[p1], dst[p2])

  for i = 0 to n - 1
    for j = i + 1 to n - 1
      if (dist(pts[i], pts[j]) < d)
        d = dist(pts[i], pts[j])
	p1 = i
        p2 = j

  return p1, p2

Doing Better

Another Algorithm

closest-pair(pts[], n)

  sort-by-x(pts)
  p1 = 0
  p2 = 1
  d = dist(pts[p1], dst[p2])

  for i = 0; i < n; i++
    j = i + 1
    while j < n ∧ pts[j].x - pts[i].x < d
      if d > dist(pts[i], pts[j])
	p1 = i
	p2 = j
	d = dist(pts[i], pts[j])
      j++

  return p1, p2

Does It?

Dividing and Conquering

Combining Subsolutions

Analysis

Correctness

The Problem

Expensive Solutions

Bounded Search

A Stricter Bound

Algorithm

closest-pair(pts)

  if pts.size > 2
    sort pts by x
    l, r = divide(x)
    d = min(closest-pair(l), 
            closest-pair(r))
    strip = within-strip(d, x)
    sort strip by y
    for each p in strip
      adjust d w.r.t. p

  return d

Analysis

sort pts by x : O(n log n)
l, r = divide(x) : odr(n)
d = min(closest-pair(l), : T(n/2)
	closest-pair(r)) : T(n/2)
strip = within-strip(d, x) : O(n)
sort strip by y : O(n log n)
for each p in strip : O(n)
  adjust d w.r.t. p : O(1)

Cheaper Sorting

Re-analysis

closest-pair(pts)
  if pts.size > 2)
    l, r = divide-on-x(pts) : odr(n)
    dl, ptsl = closest-pair(l), : T(n/2)
    dr, ptsr = closest-pair(r), : T(n/2)
    pts = merge-on-y(ptsl, ptsr) : O(n)
    d = min(dl, dr) : O(1)
    strip = within-strip(d, pts) : O(n)
    for each p in strip : O(n)
      adjust d w.r.t. p : O(1)
  return d, pts

References


This page last modified on 27 November 2008.

Creative
    Commons License