Presentation is loading. Please wait.

Presentation is loading. Please wait.

Time Complexity. Solving a computational program Describing the general steps of the solution –Algorithm’s course Use abstract data types and pseudo code.

Similar presentations


Presentation on theme: "Time Complexity. Solving a computational program Describing the general steps of the solution –Algorithm’s course Use abstract data types and pseudo code."— Presentation transcript:

1 Time Complexity

2 Solving a computational program Describing the general steps of the solution –Algorithm’s course Use abstract data types and pseudo code –Data structures course Implement the solution using a programming language and concrete data structures –Introduction to computer science

3 Pseudo Code Pseudo code is a meta language, used to describe an algorithm for a computer program. We use a notation similar to C or Pascal programming language. Unlike real code pseudo code uses a free syntax to describe a given problem

4 Pseudo Code Pseudo code does not deal with problems regarding a specific programming language, as data abstraction and error checking Use indentation to distinguish between blocks of code (loop and conditional statements) Accessing values of an array is expressed with brackets

5 Pseudo code Accessing an attribute of an object is expressed by the attribute name followed by the object in brackets (length[a]) More on pseudo code style in the text book

6 What is the running time of these methods ? Proc2(A[1..n]) i  1; j  1; s  0 repeat if A[i] < A[j] s  s + 1 if j=n j=i + 1; i  i+1 else j  j+1 until i+j > 2n Proc1(A[1..n]) s  0 for i = 1.. n do for j = (i +1).. n do if A[i] < A[j] s  s + 1

7 Analysis of Bubble sort Bubble sort (A) –1. n  length[A] –2. for j  n-1 to 1 –3. for i  0 to j – 1 –4.if A[i] > A[i + 1] –5. Swap (A[i], A[i +1])

8 Time Analysis Best case – the array is already sorted and therefore no swap operations are required

9 Time Analysis Worst case – the array is sorted in descending order and therefore all swap operations will be executed For both inputs the solution requires time

10 Asymptotic Notation Considering two algorithms, A and B, and the running time for each algorithm for a problem of size n is T A (n) and T B (n) respectively It should be a fairly simple matter to compare the two functions T A (n) and T B (n) and determine which algorithm is the best!

11 Asymptotic Notation Suppose the problem size is n 0 and that T A (n 0 ) < T B (n 0 ) Then clearly algorithm A is better than algorithm B for problem size n 0 What happens if we do not know the problem size in advance ? If we can show that T A (n) < T B (n) regardless of n then algorithm A is better then algorithm B regardless of the problem size Since we don’t know size in advance we tend to compare the asymptotic behavior of the two.

12 Asymptotic Upper Bound - O O(g(n)) is the group of all functions f(n) which are non-negative for all integers, if there exists an integer n 0 and a constant c>0 such that for all integers

13 Big O notation f(n) cg(n)

14 Example

15 Asymptotic lower Bound - is the group of all functions f(n) which are non-negative for all integers and if there exists an integer n 0 and a constant c>0 such that for all integers

16 Asymptotic lower Bound - cg(n) f(n)

17 Example

18 Asymptotic tight bound - is the group of all functions f(n) which are non-negative for all integers and if there exists an integer n 0 and two constants such that for all integers

19 Asymptotic tight Bound - cg(n) f(n) dg(n)

20 Example

21 Asymptotic Notation When we use the term f = O(n) we mean that the function f O(n) When we write we mean that the aside from the function the sum includes an additional function from O(n) which we have no interest of stating explicitly

22 Example Show that the function f(n)=8n+128 =O(n 2 ) –lets choose c = 1, then

23 Conventions for using Asymptotic notation drop all but the most significant terms –Instead of we write drop constant coefficients –Instead of we use

24 Back to improved bubble sort We improve the sorting algorithm so that if in a complete iteration over the array no swap operations were performed, the execution stops Best case – Worst case – Average case –

25 Comparing functions

26 Example Compare the functions

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

28 Example Compare the functions

29 Properties of asymptotic notation Transitivity:

30 Properties of asymptotic notation Symmetry

31 Properties of asymptotic notation Reflexivity:

32 Example Give a proof or a counter example to the following statements:

33 Example

34 Example

35 Example

36 Example Show that –1. –2.

37

38

39 Example Is it true that for any two functions f,g either f=O(g) or g=O(f) ?


Download ppt "Time Complexity. Solving a computational program Describing the general steps of the solution –Algorithm’s course Use abstract data types and pseudo code."

Similar presentations


Ads by Google