Growth of Functions
2 Analysis of Bubble Sort
3 Time Analysis – Best Case The array is already sorted – no swap operations are required
4 Time Analysis – Worst Case Array is sorted in descending order – all swap operations will be executed In both cases – O(n 2 )
5 Asymptotic Notation We are given: Two algorithms, A and B The problem size: n The running time for both algorithms: T A (n) and T B (n) Need a way of comparing the functions T A (n) and T B (n), to determine which algorithm is better (more efficient)
6 Asymptotic Notation For a given problem size k, if T A (k) < T B (k) then algorithm A is better than algorithm B To choose between the two algorithms, we need to find the one that is (almost) always better Want to show that T A (n) < T B (n) for all n Solution: compare asymptotic behavior
7 Asymptotic Upper Bound – O For a given function g(n), we define the group O(g(n)) as:
8 Asymptotic Upper Bound – O f(n) cg(n)
9 Example
10 Asymptotic Lower Bound – Ω For a given function g(n), we define the group Ω (g(n)) as:
11 Asymptotic Lower Bound – Ω cg(n) f(n)
12 Example
13 Asymptotic Tight Bound – Θ For a given function g(n), we define the group Θ (g(n)) as:
14 Asymptotic Tight Bound – Θ cg(n) f(n) dg(n)
15 Example
16 Asymptotic Bounds - Summary
17 Asymptotic Notation When we write f(n) = O(n) we actually mean: f(n) O(n) When we write we mean that aside from the function, the expression includes an additional function from O(n) which we have no interest of stating explicitly
18 Example Prove: f(n) = 8n = O(n 2 ) Let us choose c = 1, then:
19 Conventions for Using Asymptotic Notation Drop all but the most significant terms Instead of: we write: Drop constant coefficients Instead of: we write:
20 Asymptotic Notation and Polynomials Consider the following degree- d polynomial in n, where : Use the definitions of asymptotic notation to prove the following properties:
21 Asymptotic Notation and Polynomials For any constant k : If then:
22 Asymptotic Notation and Polynomials – Proof If then: : First, we mark: Now: By choosing c=b·d, we get:
23 Asymptotic Notation and Polynomials – Proof If then: : By choosing c=a d, we get:
24 Asymptotic Notation and Polynomials – Proof If then: : Since k = d, also k ≤ d and k d So, from the previous sections: and From this we get:
25 Example: Comparing the Functions and We prove that : Can we prove that ? Assume, by way of contradiction, that:
26 Example: Comparing the Functions and For any n such that : Or: From the assumption: Contradiction:
27 Example Compare the functions The logarithmic base does not change the order of magnitude
28 Properties of Asymptotic Notation – Transitivity Prove:
29 Properties of Asymptotic Notation – Symmetry Prove:
30 Properties of Asymptotic Notation – Reflexivity Prove:
31 Properties of Asymptotic Notation – Multiplication If and then: By multiplying the equations, we get:
32 Exercises For each of the following statements, prove it, or explain why it is false: The claim is true We choose c = 2, and get:
33 The claim is false: Cannot find a constant c such that: Exercises
34 Exercises Show that We prove the claim in two steps:
35 Exercises Prove:
36 Exercises Prove:
37 Exercises Left to show: We choose: c = ¼