Create Presentation
Download Presentation

Download Presentation
## CSCE 310J: Data Structures & Algorithms

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -

**CSCE 310J: Data Structures & Algorithms**Fundamentals of Algorithm Analysis Dr. Steve Goddard goddard@cse.unl.edu http://www.cse.unl.edu/~goddard/Courses/CSCE310J**Most of the slides in this lecture were originally created**by Jeff Edmonds from York University**Thinking about Algorithms Abstractly**Relevant Mathematics Classifying Functions The Time Complexity of an Algorithm Adding Made Easy Recurrence Relations Jeff Edmonds York University**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**Assumed Knowledge**• Logarithms and Exponentials • Existential and Universal Quantifiers, g "b Loves(b,g)"b g Loves(b,g)**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”**Classifying Functions**Giving an idea of how fast a function grows without going into too much detail.**Which are more alike?**Mammals**Classifying Animals**Vertebrates Fish Reptiles Mammals Birds Dogs Giraffe**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.**Which are more alike?**n1000 n2 2n**Which are more alike?**n1000 n2 2n Polynomials**Which are more alike?**1000n2 3n2 2n3**Which are more alike?**1000n2 3n2 2n3 Quadratic**Classifying Functions?**Functions**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**Classifying Functions?**Polynomial**Classifying Functions**Polynomial ? Cubic Quadratic Linear 5n4 5n3 5n 5n2**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.**Poly Logarithmic**(log n)5 = log5 n**Logarithmic << Polynomial**For sufficiently large n log1000 n << n0.001**Linear << Quadratic**For sufficiently large n 10000 n << 0.0001 n2**Polynomial << Exponential**For sufficiently large n n1000 << 20.001 n**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 << << << << << <<**Yes**Yes Yes Yes Yes No Which Functions are Constant? • 5 • 1,000,000,000,000 • 0.0000000000001 • -5 • 0 • 8 + sin(n)**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**Which Functions are Quadratic?**• n2 • … ?**Which Functions are Quadratic?**• n2 • 0.001 n2 • 1000 n2 Some constant times n2.**Which Functions are Quadratic?**• n2 • 0.001 n2 • 1000 n2 • 5n2 + 3n + 2log n**Which Functions are Quadratic?**• n2 • 0.001 n2 • 1000 n2 • 5n2 + 3n + 2log n Lie in between**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).**Which Functions are Polynomial?**• n5 • … ?**Which Functions are Polynomial?**• nc • n0.0001 • n10000 n to some constant power.**Which Functions are Polynomial?**• nc • n0.0001 • n10000 • 5n2 + 8n + 2log n • 5n2 log n • 5n2.5**Which Functions are Polynomial?**• nc • n0.0001 • n10000 • 5n2 + 8n + 2log n • 5n2 log n • 5n2.5 Lie in between**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)**Which Functions are Exponential?**• 2n • … ?**Which Functions are Exponential?**• 2n • 20.0001 n • 210000 n n times some constant power raises to the power of 2.**Which Functions are Exponential?**• 2n • 20.0001 n • 210000 n • 8n • 2n / n100 • 2n· n100 too small? too big?**Which Functions are Exponential?**• 2n • 20.0001 n • 210000 n • 8n • 2n / n100 • 2n· n100 Lie in between = 23n > 20.5n < 22n**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**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)**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)**Definition of Theta**f(n) = θ(g(n))**Definition of Theta**f(n) = θ(g(n)) f(n) is sandwiched between c1g(n)and c2g(n)**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)