Presentation is loading. Please wait.

Presentation is loading. Please wait.

G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY

Similar presentations


Presentation on theme: "G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY"— Presentation transcript:

1 G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
DATA STRUCTURES By B VENKATESWARLU, CSE Dept.

2 UNIT – I ASYMPTOTIC NOTATION

3 Asymptotic Complexity
Running time of an algorithm as a function of input size n for large n. Expressed using only the highest-order term in the expression for the exact running time. Instead of exact running time, say Q(n2). Describes behavior of function in the limit. Written using Asymptotic Notation.

4 Asymptotic Notation Q, O, W, o, w
Defined for functions over the natural numbers. Ex: f(n) = Q(n2). Describes how f(n) grows in comparison to n2. Define a set of functions; in practice used to compare two function sizes. The notations describe different rate-of-growth relations between the defining function and the defined set of functions.

5 -notation For function g(n), we define (g(n)), big-Theta of n, as the set: (g(n)) = {f(n) :  positive constants c1, c2, and n0, such that n  n0, we have 0  c1g(n)  f(n)  c2g(n)} Intuitively: Set of all functions that have the same rate of growth as g(n). g(n) is an asymptotically tight bound for f(n).

6 -notation For function g(n), we define (g(n)), big-Theta of n, as the set: (g(n)) = {f(n) :  positive constants c1, c2, and n0, such that n  n0, we have 0  c1g(n)  f(n)  c2g(n) } Technically, f(n)  (g(n)). Older usage, f(n) = (g(n)). I’ll accept either… f(n) and g(n) are nonnegative, for large n.

7 Example (g(n)) = {f(n) :  positive constants c1, c2, and n0, such that n  n0, 0  c1g(n)  f(n)  c2g(n)} 10n2 - 3n = Q(n2) What constants for n0, c1, and c2 will work? Make c1 a little smaller than the leading coefficient, and c2 a little bigger. To compare orders of growth, look at the leading term. Exercise: Prove that n2/2-3n= Q(n2)

8 Example (g(n)) = {f(n) :  positive constants c1, c2, and n0, such that n  n0, 0  c1g(n)  f(n)  c2g(n)} Is 3n3  Q(n4) ?? How about 22n Q(2n)??

9 O-notation For function g(n), we define O(g(n)), big-O of n, as the set: O(g(n)) = {f(n) :  positive constants c and n0, such that n  n0, we have 0  f(n)  cg(n) } Intuitively: Set of all functions whose rate of growth is the same as or lower than that of g(n). g(n) is an asymptotic upper bound for f(n). f(n) = (g(n))  f(n) = O(g(n)). (g(n))  O(g(n)).

10 Examples O(g(n)) = {f(n) :  positive constants c and n0, such that n  n0, we have 0  f(n)  cg(n) } Any linear function an + b is in O(n2). How? Show that 3n3=O(n4) for appropriate c and n0.

11  -notation For function g(n), we define (g(n)), big-Omega of n, as the set: (g(n)) = {f(n) :  positive constants c and n0, such that n  n0, we have 0  cg(n)  f(n)} Intuitively: Set of all functions whose rate of growth is the same as or higher than that of g(n). g(n) is an asymptotic lower bound for f(n). f(n) = (g(n))  f(n) = (g(n)). (g(n))  (g(n)).

12 Example (g(n)) = {f(n) :  positive constants c and n0, such that n  n0, we have 0  cg(n)  f(n)} n = (lg n). Choose c and n0.

13 Relations Between Q, O, W

14

15

16

17

18 Simple Sorting

19 Sorting

20 Selection Sort Reduces number of swaps to O(N). Start at the left end.
Mark the leftmost item, say as min. Compare this item with the next item, the shortest among two now becomes min. Keep comparing till the end, the final min item is swapped at placed at position 0. Then start with position 1 and repeat the process. The number of comparisons are more but the number of swaps is considerably reduced. What situation is this sort good for?

21 Selection Sort Efficiency:
-- Number of comparisons is same, N*(N-1)/2. -- runs in O(N2) time. How do you compare it to Bubble sort?

22 Insertion Sort

23 Insertion Sort Assume that the array is half sorted.
The item on the right of the sorted array is “marked”. Compare this item with each of the items in the sorted array (on the right of marked item) shifting the elements in the sorted array one position to the right if the “marked” item is smaller. This process is repeated till the appropriate position of the item is found. This continues till all the unsorted items are inserted.

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43


Download ppt "G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY"

Similar presentations


Ads by Google