CSCE 310J: Data Structures & Algorithms

# CSCE 310J: Data Structures & Algorithms

## CSCE 310J: Data Structures & Algorithms

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. CSCE 310J: Data Structures & Algorithms Fundamentals of Algorithm Analysis Dr. Steve Goddard goddard@cse.unl.edu http://www.cse.unl.edu/~goddard/Courses/CSCE310J

2. Most of the slides in this lecture were originally created by Jeff Edmonds from York University

3. Thinking about Algorithms Abstractly Relevant Mathematics Classifying Functions The Time Complexity of an Algorithm Adding Made Easy Recurrence Relations Jeff Edmonds York University

4. Classifying Functions Time Complexity t(n) = Q(n2) f(i) = nQ(n) Time Input Size Recurrence Relations Adding Made Easy T(n) = a T(n/b) + f(n) ∑i=1 f(i). Start With Some Math

5. Assumed Knowledge • Logarithms and Exponentials • Existential and Universal Quantifiers, g "b Loves(b,g)"b g Loves(b,g)

6. Quantifiers: all, some • “for all x” x P(x) is true iff P(x) is true for all x • universal quantifier (universe of discourse) • “there exist x” x P(x) is true iff P(x) is true for some value of x • existential quantifier • x A(x) is logically equivalent to  x(A(x)) • x A(x) is logically equivalent to x(A(x)) • x (A(x)  B(x)) “For all x such that if A(x) holds then B(x) holds”

7. Classifying Functions Giving an idea of how fast a function grows without going into too much detail.

8. Which are more alike?

9. Which are more alike? Mammals

10. Which are more alike?

11. Classifying Animals Vertebrates Fish Reptiles Mammals Birds Dogs Giraffe

12. T(n) 10 100 1,000 10,000 log n 3 6 9 13 amoeba n1/2 3 10 31 100 bird 10 100 1,000 10,000 human n log n 30 600 9,000 130,000 my father n2 100 10,000 106 108 elephant n3 1,000 106 109 1012 dinosaur 2n 1,024 1030 10300 103000 the universe Classifying Functions n Note: The universe contains approximately 1050 particles.

13. Which are more alike? n1000 n2 2n

14. Which are more alike? n1000 n2 2n Polynomials

15. Which are more alike? 1000n2 3n2 2n3

16. Which are more alike? 1000n2 3n2 2n3 Quadratic

17. Classifying Functions? Functions

18. 25n n5 2 2 Classifying Functions Functions Exp Constant Polynomial Double Exp Exponential Logarithmic Poly Logarithmic 5 5 log n (log n)5 n5 25n

19. Classifying Functions? Polynomial

20. Classifying Functions Polynomial ? Cubic Quadratic Linear 5n4 5n3 5n 5n2

21. Logarithmic • log10n = # digits to write n • log2n = # bits to write n = 3.32 log10n • log(n1000) = 1000 log(n) Differ only by a multiplicative constant.

22. Poly Logarithmic (log n)5 = log5 n

23. Logarithmic << Polynomial For sufficiently large n log1000 n << n0.001

24. Linear << Quadratic For sufficiently large n 10000 n << 0.0001 n2

25. Polynomial << Exponential For sufficiently large n n1000 << 20.001 n

26. 25n n5 2 2 Ordering Functions Functions Exp Constant Polynomial << << << << << << Double Exp Exponential Logarithmic Poly Logarithmic 5 5 log n (log n)5 n5 25n << << << << << <<

27. Yes Yes Yes Yes Yes No Which Functions are Constant? • 5 • 1,000,000,000,000 • 0.0000000000001 • -5 • 0 • 8 + sin(n)

28. 9 Lie in between 7 Yes Which Functions are “Constant”? The running time of the algorithm is a “Constant” It does not depend significantly on the size of the input. Yes • 5 • 1,000,000,000,000 • 0.0000000000001 • -5 • 0 • 8 + sin(n) Yes Yes No No

29. Which Functions are Quadratic? • n2 • … ?

30. Which Functions are Quadratic? • n2 • 0.001 n2 • 1000 n2 Some constant times n2.

31. Which Functions are Quadratic? • n2 • 0.001 n2 • 1000 n2 • 5n2 + 3n + 2log n

32. Which Functions are Quadratic? • n2 • 0.001 n2 • 1000 n2 • 5n2 + 3n + 2log n Lie in between

33. Which Functions are Quadratic? • n2 • 0.001 n2 • 1000 n2 • 5n2 + 3n + 2log n Ignore low-order terms Ignore multiplicative constants. Ignore "small" values of n. Write θ(n2).

34. Which Functions are Polynomial? • n5 • … ?

35. Which Functions are Polynomial? • nc • n0.0001 • n10000 n to some constant power.

36. Which Functions are Polynomial? • nc • n0.0001 • n10000 • 5n2 + 8n + 2log n • 5n2 log n • 5n2.5

37. Which Functions are Polynomial? • nc • n0.0001 • n10000 • 5n2 + 8n + 2log n • 5n2 log n • 5n2.5 Lie in between

38. Which Functions are Polynomials? • nc • n0.0001 • n10000 • 5n2 + 8n + 2log n • 5n2 log n • 5n2.5 Ignore low-order terms Ignore power constant. Ignore "small" values of n. Write nθ(1)

39. Which Functions are Exponential? • 2n • … ?

40. Which Functions are Exponential? • 2n • 20.0001 n • 210000 n n times some constant power raises to the power of 2.

41. Which Functions are Exponential? • 2n • 20.0001 n • 210000 n • 8n • 2n / n100 • 2n· n100 too small? too big?

42. Which Functions are Exponential? • 2n • 20.0001 n • 210000 n • 8n • 2n / n100 • 2n· n100 Lie in between = 23n > 20.5n < 22n

43. Which Functions are Exponential? • 2n • 20.0001 n • 210000 n • 8n • 2n / n100 • 2n· n100 = 23n > 20.5n < 22n 20.5n > n100 2n = 20.5n· 20.5n > n100· 20.5n 2n / n100 > n0.5n

44. Which Functions are Exponential? • 2n • 20.0001 n • 210000 n • 8n • 2n / n100 • 2n· n100 Ignore low-order terms Ignore base. Ignore "small" values of n. Ignore polynomial factors. Write 2θ(n)

45. 2θ(n) nθ(1) 2 2 Classifying Functions Functions Exp Constant Polynomial Double Exp Exponential Logarithmic Poly Logarithmic θ(1) θ(log n) (log n)θ(1) nθ(1) 2θ(n)

46. Notations

47. Definition of Theta f(n) = θ(g(n))

48. Definition of Theta f(n) = θ(g(n)) f(n) is sandwiched between c1g(n)and c2g(n)

49. Definition of Theta f(n) = θ(g(n)) f(n) is sandwiched between c1g(n)and c2g(n) for some sufficiently small c1 (= 0.0001) for some sufficiently large c2 (= 1000)