150 likes | 294 Views
CS 336 Feb 13, 2012. Prof. Tandy Warnow. Today’s topics. Review of exam Proofs by contradiction Big-oh. Proof by contradiction. How do we prove that the square root of 2 is irrational? Technique: assume it is rational (the ratio of two integers) and derive a contradiction.
E N D
CS 336Feb 13, 2012 Prof. Tandy Warnow
Today’s topics • Review of exam • Proofs by contradiction • Big-oh
Proof by contradiction • How do we prove that the square root of 2 is irrational? • Technique: assume it is rational (the ratio of two integers) and derive a contradiction.
Proof that square root of 2 is irrational • Suppose √2 = p/q for integers p and q that are relatively prime to each other (in particular, not both even) • Then 2 = p2/q2 • And therefore 2q2 = p2 • The left-hand side is even, and therefore the right hand side is even. • Therefore p is even. • Therefore 2q2 is divisible by 4. • Therefore q2 is divisible by 2 • Therefore q is even. • Contradiction!
Proof that there are an infinite number of primes • Suppose there is only a finite number of primes. We list them p1, p2, p3, …, pk. • Now let X = p1 p2 p3 … pk +1 • Can X be a prime? • What prime can divide X? • Contradiction!
Big-Oh • A function f(n) is Big-Oh of g(n) if and only if there exists constants C and C’ such that for all n>C, f(n) ≤ C’g(n). • This is written as f(n) is O(g(n)) • Don’t write f(n) = O(g(n)) (it isn’t equality!)
Proving that a function is big-oh of another • You need to find the constants C and C’, and prove the statement holds. • Example: what constants do you need for • f(n)=3n2 and g(n)=n2+5? • f(n)=3n2 and g(n)=n2-5? • f(n)=3n2 and g(n)=n3-5?
Techniques for guessing • If f(n) and g(n) are both polynomials then just look at the degree. • If they have the same degree, then both are big-oh of each other. • If degree(f) < degree(g) then f(n) is big-oh of g(n), but not vice-versa • Example: • f(n)=3n2 and g(n)=n3+2n+5? • f(n)=3n2 and g(n)=n2+5n3+5? • f(n)=3n2 and g(n)=n+5?
Other techniques • Logarithms grow more slowly than polynomials • Polynomials grow more slow than exponentials • Examples • F(n)= 3 log n, G(n) = n+5 • F(n) = 3 log5 n, G(n) = n-100 • F(n) = 3 log10n, G(n) = √n • F(n) = 5n100, G(n) = 3n
An equivalent definition • f(n) is O(g(n)) if and only if there are constants C and C’ s.t. f(n) ≤ C’g(n) when n>C. • Therefore f(n) is O(g(n)) if and only if there is a constant C’ s.t. limn∞ f(n)/g(n) ≤ C’
Other techniques • L’Hôpital’s rule • Assuming f(n) and g(n) are both differentiable: limn∞ f(n)/g(n) = limn∞f’(n)/g’(n) • Therefore, we can test if f(n) is big-oh of g(n) by seeing if limn∞ f’(n)/g’(n) ≤ C’
Using L’Hôpital’s rule • limn f(n)/g(n) = limn f’(n)/g’(n) • Suppose f(n)=5n and g(n)=2n2-1000 limn f(n)/g(n) = limn f’(n)/g’(n) = limn 5/(4n) = 0 < 1 Therefore f(n) is O(g(n)) (we set C’=1)
Using L’Hôpital’s rule • limn f(n)/g(n) = limn f’(n)/g’(n) • Suppose f(n)=5n and g(n)=ln n limn f(n)/g(n) = limn f’(n)/g’(n) = limn 5/(1/n) = 5n = Therefore f(n) is NOT O(g(n))
Using L’Hôpital’s rule • limn f(n)/g(n) = limn f’(n)/g’(n) • Suppose f(n)=5n and g(n)= 10n-200 limn f(n)/g(n) = limn f’(n)/g’(n) = limn 5/10 = 1/2 Therefore f(n) is O(g(n)) (we set C’=1)
Using L’Hôpital’s rule • limn f(n)/g(n) = limn f’(n)/g’(n) • Technique to determine if f(n) is O(g(n)) • Compute the limit of f(n)/g(n) • If this is any constant, then f(n) is O(g(n)) • If this is infinity, then f(n) is not O(g(n))