MATH 302 Lecture 17

From Notes
Jump to navigation Jump to search

« previous | Wednesday, October 26, 2011 | next »


Chapter 8.3: Divide and Conquer Algorithms

Examples:

  • Binary Search on Sorted List: f(n)=f(n2)+2
  • Merge Sort: f(n)=2f(n2)+n
  • Recursive Max/Min of Unsorted List: f(n)=2f(n2)+2
  • Multiplying 2n-bit Numbers: f(2n)=3f(n)+c(n)

In general, form is:

f(n)=af(nb)+g(n)
Note: the book restricts the last term to powers of n

Master Theorem

The master theorem provides a general boilerplate solution for solving recurrence relations (like divide and conquer algorithms) that satisfy a particular format.

Definition

Let f be an increasing function satisfying a recurrence relation:

f(n)=af(nb)+cnd

Whenever

  • n=bk for some k1, a1
  • b is an integer > 1
  • c and d are real numbers ≥ 0

Then

f(n)=af(nb)+cnd={Θ(nd)a<bdΘ(ndlogn)a=bdΘ(nlogba)a>bd

Proof

Proposition. If a=bd and n is a power of b, then a function f satisfying the recurrence relation f(n)=af(n/b)+cnd is of the form f(n)=f(1)nd+cndlogbn


Proof. Let k=logbn. Then

f(n)=cnd+ac(nb)d+a2c(nb2)d++ak1c(nbk1)d+akf(1)=akf(1)+j=0k1ajc(nbj)d=akf(1)+j=0k1cnd=akf(1)+kcnd=alogbnf(1)+c(logbn)nd=bdlogbnf(1)+c(logbn)nd=ndf(1)+c(logbn)nd=O(ndlogn)


Counting

Product Rule

Suppose we have to make n1 choices followed by n2 choices. For example, selecting what to wear from n1 shirts and n2 pants. For each shirt you choose, there are n2 possible pants to choose from. Therefore, the total number of choices is their product: n1n2

Addition Rule

When choices are independently selected. For example, when choosing a representative for a high school: there are n1 freshmen, n2 sophomores, n3 juniors, and n4 seniors. There are i=14ni ways to choose a representative among all high school students.

Inclusion-Exclusion

Just like in statistics:

|AB|=|A|+|B||AB|

For example: How many bit strings of length n either begin with 1 or end with 00?

  • There are 2n1 bit strings that begin with 1
  • There are 2n2 bit strings that end with 00
  • There are 2n3 bit strings thta begin with 1 and end with 00

Therefore, there are 2n1+2n22n3 bit strings that begin with 1 or end with 00.

Division Rule

How many 3-item subsets can be made from a set of n elements?

There are n ways to choose the first element, n1 ways to choose the second element, and n2 ways to choose the last element. Since the three elements can be rearranged in 3! = 6 ways, and inclusion in a set does not rely on the order, we divide the total number of ways to choose the set by 6:

n(n1)(n2)3=(n3)

Counting Functions

How many functions are there from {a, b, c, d} to {1, 2, 3}?

For each element in the domain (a, b, c, and d), there are 3 targets to choose from in the codomain. Therefore, the number of possible functions is 3 · 3 · 3 · 3 = 34

In general, for a domain containing d items and a codomain containing c items, the number of possible functions from the domain to the codomain is cd

One-to-One Functions

For a one-to-one function, once an element in the codomain has been chosen, it cannot be pointed to by any element in the domain. Therefore, the number of possible one-to-one functions is <math>c(c-1)(c-2)\dots(c-d+1) = \frac{c!}{(c-d)!}