CSCE 222 Lecture 25

From Notes
Jump to navigation Jump to search

« previous | Wednesday, April 6, 2011 | next »


Divide and Conquer

Example

Multiplication of Integers

In binary form, two number a and b consist of 2n bits. Each integer can be rewritten as a sum of two n-bit integers that represent the most and least significant bits.

a=(a2n1, a2n2, , a1, a0)=2nA1+A0=(a2n1, , an)+(an1, , a0)b=(b2n1, b2n2, , b1, b0)=2nB1+B0=(b2n1, , bn)+(bn1, , b0)ab=(2nA1+A0)(2nB1+B0)=(22n+2n)A1B1+2n(A1A0)(B0B1)+(2n+1)A0B0

Let f(n) be the total number of bit operations needed to multiply two n-bit integers. In this case, f(2n)=3f(n)+Cn.

Bitwise Shift Operator

a >> n shifts the binary digits of a n places to the right (essentially multiplying a by 2n).


Master Theorem

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={O(nd)a<bdO(ndlogn)a=bdO(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)