+ All Categories
Home > Documents > Lecture 2: Math Review and Asymptotic Analysis

Lecture 2: Math Review and Asymptotic Analysis

Date post: 05-Feb-2016
Category:
Upload: ludwig
View: 33 times
Download: 0 times
Share this document with a friend
Description:
Lecture 2: Math Review and Asymptotic Analysis. Common Math Functions. Floors and Ceilings: x-1 < └ x ┘ < x < ┌ x ┐ < x+1. Modular Arithmetic: a mod n = a – └ a/n ┘ n. Factorials: n! = 1 if n = 0 n! = n * (n-1)! If n > 0. n! < n n. Exponentials. a 0 = 1 a 1 = a a -1 = 1/a - PowerPoint PPT Presentation
52
Lecture 2: Math Review and Asymptotic Analysis
Transcript
Page 1: Lecture 2: Math Review and Asymptotic Analysis

Lecture 2: Math Review and Asymptotic Analysis

Page 2: Lecture 2: Math Review and Asymptotic Analysis

Common Math Functions

• Floors and Ceilings: x-1 < └x┘< x <┌x┐< x+1.

• Modular Arithmetic: a mod n = a – └a/n ┘ n.

• Factorials:n! = 1 if n = 0n! = n * (n-1)! If n > 0.n! < nn

Page 3: Lecture 2: Math Review and Asymptotic Analysis

Exponentials

• a0 = 1

• a1 = a

• a-1 = 1/a

• (am)n = amn

• aman = am+n

• 00 = 1 ..when convenient

Page 4: Lecture 2: Math Review and Asymptotic Analysis

Logarithm Rules

• lg(n) = log2(n) Binary logarithm• ln(n) = loge(n) Natural logarithm• lgk(n) = (lg(n))k Exponentiation• lglg(n) = lg(lg(n)) Composition• logb(mn) = logb(m) + logb(n)• logb(m/n) = logb(m) – logb(n)• logb(mn) = n · logb(m)• logb(x) = logd(x) / logd(b) Change of Base Rule• ln(x) = logd(x) / logd(e)• log(1) = 0, lg(1) = 0• log(10) = 1, lg(2) = 1• log(100) = 2, lg(4) = 2

Page 5: Lecture 2: Math Review and Asymptotic Analysis

Summation Formulas• Arithmetic series:

• Geometric series:

– Special case if x < 1:

• Harmonic series:

• Other:

2

)1( nn

n

k

nk1

...21

11

11

xx

xn

nn

k

k xxxx ...1 2

0

x11

0k

kx

nln

n

k nk1

1...

2

11

1

n

k

k1

lg nn lg

1

1

1

pn

p

n

k

pppp nk1

...21

Page 6: Lecture 2: Math Review and Asymptotic Analysis

Fibonacci Numbers

• F0 = 0

• F1 = 1

• Fi = Fi-1 + Fi-2

Page 7: Lecture 2: Math Review and Asymptotic Analysis

Proofs

• A proof is a logical argument that, assuming certain axioms, some statement is necessarily true.

• A few common proof techniques:– Direct Proof– Proof by Induction– Proof by Contradiction– Proof by Contraposition– Proof by Exhaustion– Proof by Counterexample

Page 8: Lecture 2: Math Review and Asymptotic Analysis

Direct Proof

• A conclusion is established by logically combining earlier definitions, axioms and theorems

Page 9: Lecture 2: Math Review and Asymptotic Analysis

Direct Proof: Example

• Pythagorean Theorem: For a right triangle, with sides (legs) a and b, and hypotenuse c, c²=a²+b².

• Proof (courtesy Legendre): 1. ABC, CBX, and ACX are similar triangles.2. Corresponding parts of similar triangles are

proportional: a/x=c/a → a²=cxb/(c-x)=c/b → b²=c²-cx → c²=cx+b²

3. Substituting a² for cx, we find c²=a²+b².

C A

BX

xc

b

a

Page 10: Lecture 2: Math Review and Asymptotic Analysis

Proof by Induction

• A base case is proven, and an induction rule used to prove an series (possibly infinite) of other cases.

Page 11: Lecture 2: Math Review and Asymptotic Analysis

Proof by Induction: Example

• Theorem: 1 + 2 + … + n = n(n+1) / 2

• Proof (courtesty Gauss):1. Base Case: If n = 0, 0 = 0(0+1) / 2.2. Inductive Hypothesis: Assume the statement is true

for n = m, i.e.,1 + 2 + … + m = m(m+1) / 23. Inductive Step: Show that n = m + 1 holds:

1 + 2 + … + m + m + 1 = (m+1)(m+1+1) / 2Since we know the theorem holds for n=m, we subtract those terms from each side..m + 1 = (m2 + 3m + 2) / 2 - (m2 + m) / 2m + 1 = (2m + 2)/2

Page 12: Lecture 2: Math Review and Asymptotic Analysis

Proof by Contradiction

• One shows that if a statement were false, a logical contradiction occurs, and thus the statement must be true.

Page 13: Lecture 2: Math Review and Asymptotic Analysis

Proof by Contradiction: Example

• Theorem: There exists an infinite number of prime numbers.

• Proof (courtesy of Euclid):1. Assume that there are a finite number of primes. 2. Then there is a largest prime, p. Consider the number q =

(2x3x5x7x...xp)+1. q is one more than the product of all primes up to p. q > p. And, q is not divisible by any prime up to p. For example, it is not divisible by 7, as it is one more than a multiple of 7.

3. If q is not a prime and it is not divisible by any prime <= p, then it must be divisible by a prime > p.

4. That is a contradiction, as p was assumed to be the largest prime. So, there is no largest prime. In other words, there are infinitely many primes.

Page 14: Lecture 2: Math Review and Asymptotic Analysis

Proof by Contraposition

• In order to prove A→B, prove ¬B → ¬A.

Page 15: Lecture 2: Math Review and Asymptotic Analysis

Proof by Contraposition

• Theorem: For all natural numbers: If n2 is even, then n is even as well.

• Proof:1. Contraposition: If n is odd, then n2 is odd.2. Let n = 2q+1, where q is a natural number.

Then n2 = (2q+1)2 = 4q2+4q+1 = 2(2q2+2q)+1.

3. Let p = 2q2+2q, then n2 = 2p+1. Since 2p must be even, n2 must be odd proving the contraposition.

Page 16: Lecture 2: Math Review and Asymptotic Analysis

Proof by Exhaustion

• The conclusion is established by dividing the problem into a finite number of exhaustive cases and proving each one separately.

Page 17: Lecture 2: Math Review and Asymptotic Analysis

Proof by Exhaustion: Example

• Theorem: Every cube number is either a multiple of 9 or is 1 more or 1 less than a multiple of 9.

• Proof:Each cube number is the cube of an integer n. n is either a multiple

of 3, or is 1 more or 1 less than a multiple of 3. The following 3 cases are exhaustive:

1. Case 1: If n is a multiple of 3 then the cube of n is a multiple of 27, and so certainly a multiple of 9.

2. Case 2: If n is 1 more than a multiple of 3 then the cube of n is 1 more than a multiple of 9.

3. Case 3: If n is 1 less than a multiple of 3 then the cube of n is 1 less than a multiple of 9.

Page 18: Lecture 2: Math Review and Asymptotic Analysis

Proof by Counterexample

• Given an assertion of the form : For all x, P(x) is true, disprove it by showing that there is a c such that ¬P(c) is true.

Page 19: Lecture 2: Math Review and Asymptotic Analysis

Proof by Counterexample: Example

• Conjecture: The square root of every integer is irrational.

• Counterexample: Given the function f(x) = √n, let n = 4. Clearly, √4 = 2 and 2 is rational.

Page 20: Lecture 2: Math Review and Asymptotic Analysis

Loop Invariants

Another proof technique for proving correctness for loops is to prove these properties hold for a loop invariant (we saw an example last time):

• Initialization: It is true prior to the first iteration.• Maintenance: It is true before an iteration of the

loop and remains true before the next iteration.• Termination: When the loop terminates, the

invariant yields a useful property which helps show the algorithm is correct.

Page 21: Lecture 2: Math Review and Asymptotic Analysis

Loop Invariants: Example

• Show that the following pseudocode correctly determines if x is in the array A:for i=0 to len(A):

if (x == A[i]) return truereturn false

• Loop Invariant: At the start of each for loop, the subsequence A[0]..A[i-1] does not contain the variable x.

Page 22: Lecture 2: Math Review and Asymptotic Analysis

Loop Invariants: Example

• Initialization: The loop invariant is true at initialization because the subsequence A[0]..A[-1] is an empty subsequence and by definition x is not contained in the subsequence.

• Maintenance: As we consider the (k-1)st element of A, if it is in A we return true. If it is not we continue. Therefore if we consider the kth element the (k-1)st element must not have been equal to x and subsequently any earlier element.

• Termination: The loop terminates when i=len(A) or when x is found in the array. From the maintenance property we know that if i=len(A), then A[0]..A[len(A)-1] does not contain x. If i != len(A) then the loop terminated because x was found.

Page 23: Lecture 2: Math Review and Asymptotic Analysis

Danger: Proof by Example

• Conjecture: For arbitrary sets A and B, the sets A \ B, B \ A, and A ∩ B are pair wise disjoint.

• Proof:Let A = {1,3,5,7} and B = {2,4,6,8}.Then ...NO! In general, an example is almost never a proof. * Not to be confused with proof by construction (which you are unlikely to use in this class).

Page 24: Lecture 2: Math Review and Asymptotic Analysis

Danger: Proof by Example

• Conjecture: The Fibonacci sequence, F(x), is always odd.

• Proof: Consider x=2; F(2)=1. Or consider x=10; F(10)=55, etc.. Clearly the Fibonacci sequence is always odd.

NO! Even if the conjecture were true this is not a correct proof.

Page 25: Lecture 2: Math Review and Asymptotic Analysis

Growth of Functions Review

• 1 Constant

• log nLogarithmic

• n Linear

• n log n

• n2 Quadratic

• n3 Cubic

• 2n Exponential

Page 26: Lecture 2: Math Review and Asymptotic Analysis

Asymptotic Notation

How do we describe asymptotic efficiency?• O(g(n)): g(n) is an asymptotic upper bound• o(g(n)): g(n) is an upper bound that is not

asymptotically tight • Ω(g(n)): g(n) is an asymptotic lower bound• ω(g(n)): g(n) is a lower bound that is not

asymptotically tight • Θ(g(n)): g(n) is an asymptotically tight

bound

Page 27: Lecture 2: Math Review and Asymptotic Analysis

O-Notation

• O(g(n)) is pronounced “Big-oh of g of n” or sometimes just “Oh of g of n”

• We use this notation when we can only find an upper bound on a function.

• O(g(n)) = { f(n): there exists some positive constants c and n0 such that:0 < f(n) < cg(n) for all n > n0}.

Page 28: Lecture 2: Math Review and Asymptotic Analysis

O-Notation Example

• Prove: ½ n2 – 3n = O(n2)

• Proof:1. We must choose c and n0 s.t.:

½ n2 – 3n < cn2 for all n > n0.

2. Dividing by n2 yields: ½ – 3/n < c

3. This holds if we choose c > ½ and n0 > 7.

Page 29: Lecture 2: Math Review and Asymptotic Analysis

o-Notation

• o(g(n)) is pronounced “Little-oh of g of n”

• O(g(n)) may or may not be tight. We use o(g(n)) to specify that it is not asymptotically tight.

• o(g(n)) = { f(n): for any positive constant c>0 there exists a positive constant n0>0 such that: 0 < f(n) < cg(n) for all n > n0}.

Page 30: Lecture 2: Math Review and Asymptotic Analysis

o-Notation Example

• For example, 2n = o(n2) because n2 is not an asymptotically tight upper bound for 2n. It is, however, an upper bound.

• But, 2n2 ≠ o(n2) because n2 would be an asymptotically tight upper bound.

Page 31: Lecture 2: Math Review and Asymptotic Analysis

o-Notation Example

• Prove: 2n = o(n2)

• Proof:1. Let n0 = 3/c.

2. Substituting n0 for n in 2n < cn2:2(3/c) < c(3/c)2

6/c < 9/c.

3. Clearly the o-Notation definition holds.

Page 32: Lecture 2: Math Review and Asymptotic Analysis

Ω-Notation

• Ω(g(n)) is pronounced “Big-omega of g of n” or sometimes just “Omega of g of n”

• We use this notation when we can only find a lower bound on a function.

• Ω(g(n)) = { f(n): there exists some positive constants c and n0 such that:0 < cg(n) < f(n) for all n > n0}.

Page 33: Lecture 2: Math Review and Asymptotic Analysis

Ω-Notation Example

• Prove: ½ n2 – 3n = Ω (n2)

• Proof:1. We must choose c and n0 s.t.:

cn2 < ½ n2 – 3n for all n > n0.

2. Dividing by n2 yields:c < ½ – 3/n

3. This holds if we choose c > 1/14 and n0 > 7.

Page 34: Lecture 2: Math Review and Asymptotic Analysis

ω-Notation

• ω(g(n)) is pronounced “Little-omega of g of n”

• Ω(g(n)) may or may not be tight. We use ω(g(n)) to specify that it is not asymptotically tight.

• ω(g(n)) = { f(n): for any positive constant c>0 there exists a positive constant n0>0 such that: 0 < cg(n) < f(n) for all n > n0}.

Page 35: Lecture 2: Math Review and Asymptotic Analysis

ω-Notation Example

• For example, n2/2 = ω(n) because n is not an asymptotically tight lower bound for n2/2. It is, however, a lower bound.

• But, n2/2 ≠ ω (n2) because n2 would be an asymptotically tight upper bound.

Page 36: Lecture 2: Math Review and Asymptotic Analysis

ω-Notation Example

• Prove: n2/2 = ω(n)

• Proof:1. Let n0 = 3c.

2. Substituting n0 for n in cn < n2/2c(3c) < (3c)2/26c2 < 9c2

3. Clearly the ω-Notation definition holds.

Page 37: Lecture 2: Math Review and Asymptotic Analysis

Θ - Notation

• Θ(g(n)) is pronounced “Theta of g of n”

• We use this notation when g(n) provides an upper and lower bound for a function. That is, it is asymptotically tight. This is a stronger statement.

• Θ(g(n)) = { f(n): there exists some positive constants c1, c2 and n0 such that:0 < c1g(n) < f(n) < c2g(n) for all n > n0}.

Page 38: Lecture 2: Math Review and Asymptotic Analysis

Θ - Notation

• You may notice that f(n) = Θ(g(n)) implies f(n) = O(g(n)) and f(n) = Ω(g(n)). The converse is not true.

• f(n) = Θ(g(n)) sandwhiches f(n) between an upper and lower bound.

Page 39: Lecture 2: Math Review and Asymptotic Analysis

Θ -Notation Example

• Prove: ½ n2 – 3n = Θ(n2)

• Proof:1. We must choose c and n0 s.t.:

c1n2 < ½ n2 – 3n < c2n2 for all n > n0.

2. Dividing by n2 yields:c1 < ½ – 3/n < c2

3. This holds if we choose c1 > 1/14 and c2 > ½ and n0 > 7.

Page 40: Lecture 2: Math Review and Asymptotic Analysis

Θ – Notation Example

• Prove: ½ n2 – 3n = Θ(n2)

• Alternate Proof:1. We already proved ½ n2 – 3n = O(n2) and

that ½ n2 – 3n = Ω(n2), then clearly the n2 bound on ½ n2 – 3n is asymptotically tight and ½ n2 – 3n = Θ(n2).

Page 41: Lecture 2: Math Review and Asymptotic Analysis

Asymptotic Notation in Equations and Inequalities

• 2n2 + 3n + 1 = 2n2 + Θ(n) means 2n2 + 3n + 1 = 2n2 + f(n) where f(n) is a function in the set Θ(n).

• We use this to impart meaning and remove clutter:2n2 + 3n + 1 = 2n2 + Θ(n) =Θ(n2)

Page 42: Lecture 2: Math Review and Asymptotic Analysis

Transitivity

• f(n) = O(g(n)) && g(n) = O(h(n)) → f(n) = O(h(n)).• f(n) = o(g(n)) && g(n) = o(h(n)) → f(n) = o(h(n)).• f(n) = Ω(g(n)) && g(n) = Ω(h(n)) → f(n) = Ω(h(n)).• f(n) = ω(g(n)) && g(n) = ω(h(n)) → f(n) = ω(h(n)).• f(n) = Θ(g(n)) && g(n) = Θ(h(n)) → f(n) = Θ(h(n)).

Page 43: Lecture 2: Math Review and Asymptotic Analysis

Reflexivity

• f(n) = O(f(n)),

• f(n) = Ω(f(n)),

• f(n) = Θ(f(n))

But NOT:

• f(n) = o(f(n)),

• f(n) = ω(f(n)).

Page 44: Lecture 2: Math Review and Asymptotic Analysis

Symmetry

• f(n) = Θ(g(n)) if and only if g(n) = Θ(f(n)).

Does not hold for O, o, Ω, or ω but the following transpose symmetries do:

• f(n) = O(g(n)) if and only if g(n) = Ω(f(n)).

• f(n) = o(g(n)) if and only if g(n) = ω(f(n)).

Page 45: Lecture 2: Math Review and Asymptotic Analysis

An Analogy

The comparison of functions roughly analogizes to the comparison of real numbers:

• f(n) = O(g(n)) … a < b• f(n) = o(g(n)) … a < b• f(n) = Ω(g(n)) … a > b• f(n) = ω(g(n)) … a > b• f(n) = Θ(g(n)) … a = b

Page 46: Lecture 2: Math Review and Asymptotic Analysis

Insertion Sort.. again

• Last time we came up with this running time for insertion sort:T(n) = (c4+c5+c6)n2/2 + (c1+c2+c3+c4/2-c5/2-c6/2c7)n – (c2 + c3 + c4 + c7)

• With some hand waving I claimed that T(n) = Θ(n2)

• Prove more formally this is true. Simplification: T(n) = dn2 + en – f where d, e and f are constants.

Page 47: Lecture 2: Math Review and Asymptotic Analysis

A Warm Up Exercise

• Show for any real constants a and b > 0,(n + a)b = Θ(nb).

• Recall: Θ(g(n)) = { f(n): there exists some positive constants c1, c2 and n0 such that:0 < c1g(n) < f(n) < c2g(n) for all n > n0}.

Page 48: Lecture 2: Math Review and Asymptotic Analysis

A Warm Up Exercise

• Show for any real constants a and b > 0,(n + a)b = Θ(nb).

• Proof:1. c1nb < (n + a)b < c2nb

2. (n+a)b = nb(1+a)b

3. Let c1 = 1

4. Let c2 = (2+a)b

5. Let n0 = 1.

Page 49: Lecture 2: Math Review and Asymptotic Analysis

Insertion Sort.. Again

• Prove: T(n) = dn2 + en – f

• Recall: Θ(g(n)) = { f(n): there exists some positive constants c1, c2 and n0 such that:0 < c1g(n) < f(n) < c2g(n) for all n > n0}.

Page 50: Lecture 2: Math Review and Asymptotic Analysis

Insertion Sort.. again

• Prove: T(n) = dn2 + en – f = Θ(n2)

• Proof:c1n2 < dn2 + en – f < c2n2

Let n0 = f*max(1,1/d)*max(1,1/e)Note: min(d,e)n2 < dn2 + en – fLet c1 = min(d,e).Note: dn2 + en – f < (d+e)n2

Let c2 = (d+e).

Page 51: Lecture 2: Math Review and Asymptotic Analysis

More Examples

• Prove: lg(n!) = Θ(n lg n)

Page 52: Lecture 2: Math Review and Asymptotic Analysis

More Examples

• Prove: lg(n!) = O(n lg n)

• Proof:1. n! < nn

2. lg(nn) = n lg (n)

3. lg(nn) > lg(n!)

4. n lg (n) > lg(n!)

5. lg(n!) = O(n lg n)


Recommended