<<

Example3,Slide3...... 17 ProvingBig-Oh: Example4...... 18 Example4,Slide2...... 19 Order Notation and Time Complexity Big-Omega Notation 20 Definitions ...... 20 TricksforProvingBig-Omega ...... 21 The computing scientist’s main challenge is not to get SuggestedApproach...... 22 confused by the complexities of his own making. (. W. ProvingBig-Omega: Example1...... 23 Dijkstra) ProvingBig-Omega: Example2...... 24 Example2,Slide2...... 25 ProvingBig-Omega: Example3...... 26 Controlling complexity is the essence of computer Example3,Slide2...... 27 programming. (Brian Kernigan) Example3,Slide3...... 28 ProvingBig-Omega: Example4...... 29 Example4,Slide2...... 30 If it doesn’t work, it doesn’t matter how fast it doesn’t work. (Mich Ravera) Proving Not Big-Oh 31 HowtoProveBig-OhisFalse ...... 31 NotBig-OhTheorem ...... 32 NotBig-OhTheorem ...... 33

Definitions...... 2 Time Complexity 3 ComplexityofFinding the Maximum...... 3 LinearSearch...... 4 BubbleSort...... 5 Big-Oh Notation 6 Definitions ...... 6 One Approach for Finding Witnesses...... 7 TricksforProvingBig-Oh ...... 8 ProvingBig-Oh: Example1...... 9 Example1,Slide2...... 10 Example1,Slide3...... 11 ProvingBig-Oh: Example2...... 12 Example2,Slide2...... 13 Example2,Slide3...... 14 ProvingBig-Oh: Example3...... 15 Example3,Slide2...... 16

1 2 Definitions  Problem Size. The number of elements in an ’s input. Usually Assume n ≥ 1. denoted as n. procedure linear search (k, a1,...,an)  Time Complexity. The number of steps needed by an algorithm to solve a loc := 0 1 problem of size n. i := 1 1  . The memory needed by an algorithm to solve a problem while i ≤ n ∧ loc =0 2 to n +1 of size n. if k = ai then 1 to n  Worst-Case Analysis. An upper bound on the time or space complexity of loc := i 0 or 1 an algorithm. else i := i +1 0 to n  Big-Oh Notation. Used to summarize the worst-case complexity of an return loc 1 algorithm to within a constant factor. Total: 7 (while twice, others once except else) to 3n +4 (loc stays 0) CS 2233 Discrete Mathematical Structures Order Notation and Time Complexity – 2 CS 2233 Discrete Mathematical Structures Order Notation and Time Complexity – 4

Bubble Time Complexity 3 procedure (a1,..., an) i := 1 1 Complexity of Finding the Maximum while i aj+1 then (n − 1) procedure maximum(a1,...,an) 2 temp := aj 0 to (n − 1) max := a1 1 2 aj := aj+1 0 to (n − 1) i := 2 1 2 while i ≤ n n aj+1 := temp 0 to (n − 1) j := j +1 (n − 1)2 if max < ai then n − 1 i := i +1 n − 1 max := ai 0 to n − 1 i := i +1 n − 1 2 2 return max Total: 3n − 2n +1 to 6n − 8n +4 1 CS 2233 Discrete Mathematical Structures Order Notation and Time Complexity – 5 Total: 3n +1 to 4n CS 2233 Discrete Mathematical Structures Order Notation and Time Complexity – 3

3 4 Big-Oh Notation 6 Proving Big-Oh: Example 1 Definitions Show that 3n +7 is O(n). In this case, f(n)=3n +7 and g(n) = n.  Big-Oh notation provides an upper bound on a to within a n f(n) g(n) ⌈f(n)/g(n)⌉ constant factor. 1 10 1 10  Let f and g be functions from nonnegative numbers to nonnegative 10 37 10 4 numbers. 100 307 100 4  f(n) is O(g(n)) if there are positive constants C and k such that: f(n) ≤ C ∗ g(n) whenever n>k This table suggests trying k =1 and C = 10 or k = 10 and C =4.  f(n) is O(g(n)) is equivalent to: ∃C ∃k ∀n (n>k → f(n) ≤ C ∗ g(n)) Proving either one is good enough to prove big-Oh. CS 2233 Discrete Mathematical Structures Order Notation and Time Complexity – 9  To prove big-Oh, find witnesses, specific values for C and k, and prove n>k implies f(n) ≤ C ∗ g(n). Example 1, Slide 2 CS 2233 Discrete Mathematical Structures Order Notation and Time Complexity – 6 Try k =1 and C = 10. Want to prove n > 1 implies 3n +7 ≤ 10n. One Approach for Finding Witnesses Assume n > 1. Want to show 3n +7 ≤ 10n.  Generate a table for f(n) and g(n). using n =1, n = 10 and n = 100. 7 is the lowest-order term, so work on that first. [Use values smaller than 10 and 100 if you wish.] n > 1 implies 7n > 7, which implies  Guess C = ⌈f(1)/g(1)⌉ 3n +7 < 3n +7n = 10n. (or C = ⌈f(10)/g(10)⌉).  Check that f(10) ≤ C ∗ g(10) and f(100) ≤ C ∗ g(100). This finishes the proof. A short version is: [If this is not true, f(n) might not be O(g(n)).] n > 1 implies 3n +7 < 3n +7n = 10n.  Choose k =1 (or k = 10). CS 2233 Discrete Mathematical Structures Order Notation and Time Complexity – 10  Prove that ∀n(n>k → f(n) ≤ C ∗ g(n)). [It’s ok if you end with a larger, but still constant, value for C.] Example 1, Slide 3 CS 2233 Discrete Mathematical Structures Order Notation and Time Complexity – 7 Try k = 10 and C =4. Want to prove n > 10 implies 3n +7 ≤ 4n. Tricks for Proving Big-Oh Assume n > 10. Want to show 3n +7 ≤ 4n.  Assume n > 1 if you chose k =1 (or n > 10 if you chose k = 10). Work on the lowest-order term first.  To prove f(n) ≤ C ∗ g(n), you need to find expressions larger than f(n) n > 10 implies n > 7, which implies and smaller than C ∗ g(n). 3n +7 < 3n + n =4n.  If the lowest-order term is negative, just eliminate it to obtain a larger expression. This finishes the proof. A short version is:  Repeatedly use n>k and 2n > 2k and 3n > 3k and so on to “convert” n > 10 implies 3n +7 < 3n + n =4n. CS 2233 Discrete Mathematical Structures Order Notation and Time Complexity – 11 the lowest-order term into a higher-order term.  Check that your expressions are less than C ∗ g(n) by using n = 100.

CS 2233 Discrete Mathematical Structures Order Notation and Time Complexity – 8

5 6 Proving Big-Oh: Example 2 Example 2, Slide 3 2 2 2 Try k = 10 and C =2. Show that n +2n +1 is O(n ). In this case, f(n) = n +2n +1 and 2 2 2 Want to prove n > 10 implies n +2n +1 ≤ 2n . g(n) = n . Assume n > 10. Want to show n2 +2n +1 ≤ 2n2. n f(n) g(n) ⌈f(n)/g(n)⌉ Work on the lowest-order term first. 1 4 1 4 n > 10 implies n > 1, which implies 10 121 100 2 n2 +2n +1 10 implies n > 3 and n2 > 3n, which implies n2 +3n 10 implies n +2n +1 1 implies n2 +2n +1 ≤ 4n2. Show that (n + 1) is O(n ). In this case, f(n)=(n + 1) and g(n) = n . 2 2 Assume n > 1. Want to show n +2n +1 ≤ 4n . n f(n) g(n) ⌈f(n)/g(n)⌉ Work on the lowest-order term first. 1 8 1 8 n > 1 implies n2 +2n +1 1 implies 3n > 3 and 3n > 3n, which implies This table suggests trying k =1 and C =8 or k = 10 and C =2. n2 +3n 1 implies n2 +2n +1 1 implies (n + 1)3 ≤ 8n3. Assume n > 1. Want to show (n + 1)3 ≤ 8n3. n > 1 implies (n + 1)3 < (n + n)3 = (2n)3. It’s a mistake to infer (2n)3 and 2n3 are equal. (2n)3 = (2n) ∗ (2n) ∗ (2n)=8n3 This finishes the proof. A short version is: n > 1 implies (n + 1)3 < (n + n)3 = (2n)3 =8n3 CS 2233 Discrete Mathematical Structures Order Notation and Time Complexity – 16

7 8 Example 3, Slide 3 Example 4, Slide 2 Try k = 100 and C =8. Want to prove Try k = 10 and C =2. n > 100 implies 8n3 − 12n2 +6n − 1 ≤ 8n3 3 3 Want to prove n > 10 implies (n + 1) ≤ 2n . 3 3 3 2 Assume n > 100. Want to show f(n) ≤ 8n . We will need (n + 1) = n +3n +3n +1. 3 3 The lowest-order term is negative, so eliminate it. Assume n > 10. Want to show (n + 1) ≤ 2n . 8n3 − 12n2 +6n − 1 < 8n3 − 12n2 +6n. n > 10 implies n3 +3n2 +3n +1 100 implies n > 6, which implies n > 10 implies 8n3 − 12n2 +6n < 8n3 − 12n2 +(n) ∗ n =8n3 − 11n2. n3 +3n2 +4n 10 implies n +4n 100 implies 8n3 − 11n2 ≤ 8n3. This finishes the proof. A short version is: This finishes the proof. A short version is: 3 3 2 3 2 3 2 3 n > 10 implies (n +1) = n +3n +3n +1 100 implies 8n3 − 12n2 +6n − 1 < 8n3 − 12n2 +6n < 8n3 − 11n2 < 8n3. CS 2233 Discrete Mathematical Structures Order Notation and Time Complexity – 17 CS 2233 Discrete Mathematical Structures Order Notation and Time Complexity – 19

Proving Big-Oh: Example 4 Show that 8n3 − 12n2 +6n − 1 is O(n3). In this case, Big-Omega Notation 20 f(n)=8n3 − 12n2 +6n − 1 and g(n) = n3. Definitions n f(n) g(n) ⌈f(n)/g(n)⌉  Big-Omega notation provides a lower bound on a function to within a 1 1 1 1 constant factor. 10 6859 1000 7  Let f and g be functions from nonnegative numbers to nonnegative 100 7,880,599 1,000,000 8 numbers. 1000 7,988,005,999 1,000,000,000 8  f(n) is Ω(g(n)) if there are positive constants C and k such that: C =1 and C =7 are too small for n = 100. C =8 works for n = 1000 and f(n) ≥ C ∗ g(n) whenever n>k the other values of n, so try k = 100 and C =8.  f(n) is Ω(g(n)) is equivalent to [k =1 and k = 10 would also work.] ∃C ∃k ∀n (n>k → f(n) ≥ C ∗ g(n)) CS 2233 Discrete Mathematical Structures Order Notation and Time Complexity – 18  To prove big-Omega, find witnesses, specific values for C and k, and prove n>k implies f(n) ≥ C ∗ g(n).

CS 2233 Discrete Mathematical Structures Order Notation and Time Complexity – 20

9 10 Tricks for Proving Big-Omega Proving Big-Omega: Example 2  Assume n > 1 if you chose k =1 (or n > 10 if you chose k = 10). Show that n2 − 2n +1 is Ω(n2). In this case, f(n) = n2 − 2n +1 and  To prove f(n) ≥ C ∗ g(n), you need to find expressions smaller than f(n) g(n) = n2. and larger than C ∗ g(n).  If the lowest-order term is positive, just eliminate it to obtain a larger n f(n) g(n) ⌈g(n)/f(n)⌉ C? expression. 1 1 1 1 1  Repeatedly use −k > −n and −0.1k > −0.1n and so on to “convert” the 10 81 100 2 1/2 lowest-order term into a higher-order term. 100 9801 10000 2 1/2  Check that your expressions are greater than C ∗ g(n) by using n = 100. This table suggests trying k = 10 and C =1/2. Trying C =1 is too large. CS 2233 Discrete Mathematical Structures Order Notation and Time Complexity – 21 CS 2233 Discrete Mathematical Structures Order Notation and Time Complexity – 24

Finding Big-Omega Witnesses Example 2, Slide 2 Try k = 10 and C =1/2.  Generate a table for f(n) and g(n). using n =1, n = 10 and n = 100. Want to prove n > 10 implies n2 − 2n +1 ≥ n2/2. [Use values smaller than 10 and 100 if you wish.] 2  Guess 1/C = ⌈g(1)/f(1)⌉ Assume n > 10. Want to show f(n) ≥ n /2. (or more likely 1/C = ⌈g(10)/f(10)⌉). The lowest-order term is positive, so eliminate.  Check that f(10) ≥ C ∗ g(10) and f(100) ≥ C ∗ g(100). n2 − 2n +1 >n2 − 2n [If this is not true, f(n) might not be Ω(g(n)).]  n > 10 implies −10 > −n, implies −2 > −0.2n. Choose k =1 (or k = 10). 2 2 2 2  Prove that ∀n(n>k → f(n) ≥ C ∗ g(n)). −2 > −0.2n implies n − 2n>n − 0.2n =0.8n . [It’s ok if you end up with a smaller, but still positive, value for C.] n > 10 implies 0.8n2 >n2/2. CS 2233 Discrete Mathematical Structures Order Notation and Time Complexity – 22 This finishes the proof. A short version is: n > 10 implies n2 − 2n +1 >n2 − 2n > 0.8n2 >n2/2 Proving Big-Omega: Example 1 CS 2233 Discrete Mathematical Structures Order Notation and Time Complexity – 25 Show that 3n +7 is Ω(n). In this case, f(n)=3n +7 and g(n) = n. Proving Big-Omega: Example 3 n f(n) g(n) ⌈g(n)/h(n)⌉ C? Show that (n − 1)3 is Ω(n3). In this case, f(n)=(n − 1)3 and g(n) = n3. 1 10 1 1 1 10 37 10 1 1 n f(n) g(n) ⌈g(n)/f(n)⌉ C? 100 307 100 1 1 1 0 1 undefined undefined 10 729 1000 2 1/2 This table suggests trying k =1 and C =1. 100 970299 1000000 2 1/2 Want to prove n > 1 implies 3n +7 ≥ n. This table suggests trying k = 10 and C =1/2. Short proof: n > 1 implies 3n +7 > 3n>n. CS 2233 Discrete Mathematical Structures Order Notation and Time Complexity – 23 Trying k =1 does not look like a good idea. CS 2233 Discrete Mathematical Structures Order Notation and Time Complexity – 26

11 12 Example 3, Slide 2 Example 4, Slide 2 Try k = 10 and C =1/9. Want to prove Try k = 10 and C =1/2. n > 10 implies n3/8 − n2/12 − n/6 − 1 ≥ n3/9 Want to prove n > 10 implies (n − 1)3 ≥ n3/2. 3 3 2 Assume n > 10, which implies the following: We will need (n − 1) = n − 3n +3n − 1. 3 2 3 3 n /8 − n /12 − n/6 − 1 Assume n > 10. Want to show (n − 1) ≥ n /2. = (3n3 − 2n2 − 4n − 24)/24 3 2 3 2 3 2 3 2 > (3n − 2n − 4n − 2.4n)/24 n > 10 implies n − 3n +3n − 1 >n − 3n +3n − 0.1 ∗ n>n − 3n +2n. 3 2 > (3n − 2n − 7n)/24 3 2 2 n > 10 implies n3 − 3n2 +2n>n3 − 3n2. > (3n − 2n − 0.7n )/24 > (3n3 − 3n2)/24 3 2 3 3 3 n > 10 implies n − 3n >n − 0.3n =0.7n . > (3n3 − 0.3n3)/24 3 3 3 3 > (3n − n )/24 n > 10 implies 0.7n >n /2. 3 3 CS 2233 Discrete Mathematical Structures Order Notation and Time Complexity – 27 = (2n )/24 = n /12 Ended up with k = 10 and C =1/12, proving 3 2 3 Example 3, Slide 3 n > 10 implies n /8 − n /12 − n/6 − 1 ≥ n /12 CS 2233 Discrete Mathematical Structures Order Notation and Time Complexity – 30 This finishes the proof. A short version is: n > 10 implies (n − 1)3 Proving Not Big-Oh 31 = n3 − 3n2 +3n − 1 >n3 − 3n2 +2n >n3 − 3n2 How to Prove Big-Oh is False 3 > 0.7n f(n) is not O(g(n)) is equivalent to 3 >n /2 ∀C ∀k ∃n (n>k ∧ f(n) >C ∗ g(n)) CS 2233 Discrete Mathematical Structures Order Notation and Time Complexity – 28 Need to prove for all values of C and k. Proving Big-Omega: Example 4 f(n) 3 2 3 One approach is to show lim =0. Show that n /8 − n /12 − n/6 − 1 is O(n ). In this case, n→∞ g(n) f(n) = n3/8 − n2/12 − n/6 − 1 and g(n) = n3. Another approach is to show f(n) is Ω(n ∗ g(n)). n f(n) g(n) ⌈g(n)/f(n)⌉ C? [More generally, to show f(n) is Ω(h(n) ∗ g(n)), where h(n) is not O(1).] 1 −0.8 1 −1 −1 CS 2233 Discrete Mathematical Structures Order Notation and Time Complexity – 31 10 117.3 1,000 9 1/9 100 124,182.3 1,000,000 9 1/9

C = −1 is useless, so try k = 10 and C =1/9. CS 2233 Discrete Mathematical Structures Order Notation and Time Complexity – 29

13 14 Not Big-Oh Theorem Thm: If f(n) is Ω(n ∗ g(n)), then f(n) is not O(g(n)). Assume f(n) is Ω(n ∗ g(n)) and f(n) is O(g(n)). Want to show a contradiction. The approach is to find a value of n that violates the definitions. Proof: f(n) is Ω(n ∗ g(n)) implies there exists positive values C and k such that n>k implies f(n) ≥ C ∗ n ∗ g(n). f(n) is O(g(n)) implies there exists positive values C′ and k′ such that n>k′ implies f(n) ≤ C ∗ g(n). CS 2233 Discrete Mathematical Structures Order Notation and Time Complexity – 32

Not Big-Oh Theorem Let n = k + k′ + C′/C. k > 0 and k′ > 0 and C > 0 and C′ > 0 imply n>k and n>k′ and C ∗ n>C′. f(n) is Ω(n ∗ g(n)) and n>k and C ∗ n>C′ imply f(n) ≥ C ∗ n ∗ g(n) >C′ ∗ g(n). f(n) is O(g(n)) and n>k′ imply f(n) ≤ C′ ∗ g(n). f(n) >C′ ∗ g(n) contradicts f(n) ≤ C′ ∗ g(n). CS 2233 Discrete Mathematical Structures Order Notation and Time Complexity – 33

15