Presentation is loading. Please wait.

Presentation is loading. Please wait.

Growth of Functions. 2 Analysis of Bubble Sort 3 Time Analysis – Best Case The array is already sorted – no swap operations are required.

Similar presentations


Presentation on theme: "Growth of Functions. 2 Analysis of Bubble Sort 3 Time Analysis – Best Case The array is already sorted – no swap operations are required."— Presentation transcript:

1 Growth of Functions

2 2 Analysis of Bubble Sort

3 3 Time Analysis – Best Case The array is already sorted – no swap operations are required

4 4 Time Analysis – Worst Case Array is sorted in descending order – all swap operations will be executed In both cases – O(n 2 )

5 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 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 7 Asymptotic Upper Bound – O For a given function g(n), we define the group O(g(n)) as:

8 8 Asymptotic Upper Bound – O f(n) cg(n)

9 9 Example

10 10 Asymptotic Lower Bound – Ω For a given function g(n), we define the group Ω (g(n)) as:

11 11 Asymptotic Lower Bound – Ω cg(n) f(n)

12 12 Example

13 13 Asymptotic Tight Bound – Θ For a given function g(n), we define the group Θ (g(n)) as:

14 14 Asymptotic Tight Bound – Θ cg(n) f(n) dg(n)

15 15 Example

16 16 Asymptotic Bounds - Summary

17 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 18 Example Prove: f(n) = 8n + 128 = O(n 2 ) Let us choose c = 1, then:

19 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 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 21 Asymptotic Notation and Polynomials For any constant k : If then:

22 22 Asymptotic Notation and Polynomials – Proof If then: : First, we mark: Now: By choosing c=b·d, we get:

23 23 Asymptotic Notation and Polynomials – Proof If then: : By choosing c=a d, we get:

24 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 25 Example: Comparing the Functions and We prove that : Can we prove that ? Assume, by way of contradiction, that:

26 26 Example: Comparing the Functions and For any n such that : Or: From the assumption: Contradiction:

27 27 Example Compare the functions The logarithmic base does not change the order of magnitude

28 28 Properties of Asymptotic Notation – Transitivity Prove:

29 29 Properties of Asymptotic Notation – Symmetry Prove:

30 30 Properties of Asymptotic Notation – Reflexivity Prove:

31 31 Properties of Asymptotic Notation – Multiplication If and then: By multiplying the equations, we get:

32 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 33 The claim is false: Cannot find a constant c such that: Exercises

34 34 Exercises Show that We prove the claim in two steps:

35 35 Exercises Prove:

36 36 Exercises Prove:

37 37 Exercises Left to show: We choose: c = ¼


Download ppt "Growth of Functions. 2 Analysis of Bubble Sort 3 Time Analysis – Best Case The array is already sorted – no swap operations are required."

Similar presentations


Ads by Google