61
Design and Analysis of Algorithms Chapter 2.1 1 Analysis of Algorithms Dr. Ying Lu [email protected] August 28, 2012 http://www.cse.unl.edu/~ylu/raik283/ RAIK 283: Data Structures & RAIK 283: Data Structures & Algorithms Algorithms

Design and Analysis of Algorithms Chapter 2.1 1 Analysis of Algorithms Dr. Ying Lu [email protected] August 28, 2012 ylu/raik283

Embed Size (px)

Citation preview

  • Slide 1
  • Design and Analysis of Algorithms Chapter 2.1 1 Analysis of Algorithms Dr. Ying Lu [email protected] August 28, 2012 http://www.cse.unl.edu/~ylu/raik283/ RAIK 283: Data Structures & Algorithms
  • Slide 2
  • Design and Analysis of Algorithms Chapter 2.1 2 b Giving credit where credit is due: Most of the lecture notes are based on the slides from the Textbooks companion websiteMost of the lecture notes are based on the slides from the Textbooks companion website http://www.aw-bc.com/info/levitin http://www.aw-bc.com/info/levitin Several slides are from Jeff Edmonds of the York UniversitySeveral slides are from Jeff Edmonds of the York University I have modified them and added new slidesI have modified them and added new slides RAIK 283: Data Structures & Algorithms
  • Slide 3
  • The Time Complexity of an Algorithm
  • Slide 4
  • Specifies how the running time depends on the size of the input
  • Slide 5
  • Design and Analysis of Algorithms Chapter 2.1 5 Purpose
  • Slide 6
  • 6 Purpose b To estimate how long a program will run. b To estimate the largest input that can reasonably be given to the program. b To compare the efficiency of different algorithms. b To help focus on the parts of code that are executed the largest number of times. b To choose an algorithm for an application.
  • Slide 7
  • Design and Analysis of Algorithms Chapter 2.1 7 Purpose (Example) b Suppose a machine that performs a million floating- point operations per second (10 6 FLOPS), then how long an algorithm will run for an input of size n=50? 1) If the algorithm requires n 2 such operations:1) If the algorithm requires n 2 such operations:
  • Slide 8
  • Design and Analysis of Algorithms Chapter 2.1 8 Purpose (Example) b Suppose a machine that performs a million floating- point operations per second (10 6 FLOPS), then how long an algorithm will run for an input of size n=50? 1) If the algorithm requires n 2 such operations:1) If the algorithm requires n 2 such operations: 0.0025 second
  • Slide 9
  • Design and Analysis of Algorithms Chapter 2.1 9 Purpose (Example) b Suppose a machine that performs a million floating- point operations per second (10 6 FLOPS), then how long an algorithm will run for an input of size n=50? 1) If the algorithm requires n 2 such operations:1) If the algorithm requires n 2 such operations: 0.0025 second 2) If the algorithm requires 2 n such operations:2) If the algorithm requires 2 n such operations: A) Takes a similar amount of time (t < 1 sec) B) Takes a little bit longer time (1 sec < t < 1 year) C) Takes a much longer time (1 year < t)
  • Slide 10
  • Design and Analysis of Algorithms Chapter 2.1 10 Purpose (Example) b Suppose a machine that performs a million floating- point operations per second (10 6 FLOPS), then how long an algorithm will run for an input of size n=50? 1) If the algorithm requires n 2 such operations:1) If the algorithm requires n 2 such operations: 0.0025 second 2) If the algorithm requires 2 n such operations:2) If the algorithm requires 2 n such operations: over 35 years!
  • Slide 11
  • Design and Analysis of Algorithms Chapter 2.1 11 Time Complexity Is a Function Specifies how the running time depends on the size of the input. A function mapping size of input time T(n) executed.
  • Slide 12
  • Design and Analysis of Algorithms Chapter 2.1 12 Definition of Time?
  • Slide 13
  • Design and Analysis of Algorithms Chapter 2.1 13 Definition of Time b # of seconds (machine, implementation dependent). b # lines of code executed. b # of times a specific operation is performed (e.g., addition).
  • Slide 14
  • Design and Analysis of Algorithms Chapter 2.1 14 Theoretical analysis of time efficiency Time efficiency is analyzed by determining the number of repetitions of the basic operation as a function of input size b Basic operation: the operation that contributes most towards the running time of the algorithm. T(n) c op C(n) T(n) c op C(n) running time execution time for basic operation Number of times basic operation is executed input size
  • Slide 15
  • Design and Analysis of Algorithms Chapter 2.1 15 Input size and basic operation examples Problem Input size measure Basic operation Search for key in a list of n items Multiply two matrices of floating point numbers Compute a n Graph problem
  • Slide 16
  • Design and Analysis of Algorithms Chapter 2.1 16 Input size and basic operation examples Problem Input size measure Basic operation Search for key in a list of n items Number of items in the list: n Key comparison Multiply two matrices of floating point numbers Compute a n Graph problem
  • Slide 17
  • Design and Analysis of Algorithms Chapter 2.1 17 Input size and basic operation examples Problem Input size measure Basic operation Search for key in a list of n items Number of items in the list: n Key comparison Multiply two matrices of floating point numbers Dimensions of matrices Floating point multiplication Compute a n Graph problem
  • Slide 18
  • Design and Analysis of Algorithms Chapter 2.1 18 Input size and basic operation examples Problem Input size measure Basic operation Search for key in list of n items Number of items in list n Key comparison Multiply two matrices of floating point numbers Dimensions of matrices Floating point multiplication Compute a n n Floating point multiplication Graph problem
  • Slide 19
  • Design and Analysis of Algorithms Chapter 2.1 19 Input size and basic operation examples Problem Input size measure Basic operation Search for key in list of n items Number of items in list n Key comparison Multiply two matrices of floating point numbers Dimensions of matrices Floating point multiplication Compute a n n Floating point multiplication Graph problem #vertices and/or edges Visiting a vertex or traversing an edge
  • Slide 20
  • Design and Analysis of Algorithms Chapter 2.1 20 Theoretical analysis of time efficiency Time efficiency is analyzed by determining the number of repetitions of the basic operation as a function of input size
  • Slide 21
  • Design and Analysis of Algorithms Chapter 2.1 21 Which Input of size n? Efficiency also depends on the particular input b For instance: search a key in a list of n letters Problem input: a list of n lettersProblem input: a list of n letters How many different inputs?How many different inputs?
  • Slide 22
  • Design and Analysis of Algorithms Chapter 2.1 22 Which Input of size n? Efficiency also depends on the particular input For instance: search a key in a list of n letters There are 26 n inputs of size n. Which do we consider for the time efficiency C(n)?
  • Slide 23
  • Design and Analysis of Algorithms Chapter 2.1 23 Best-case, average-case, worst-case b Worst case: W(n) maximum over inputs of size n b Best case: B(n) minimum over inputs of size n b Average case: A(n) average over inputs of size n NOT the average of worst and best caseNOT the average of worst and best case Under some assumption about the probability distribution of all possible inputs of size n, calculate the weighted sum of expected C(n) (numbers of basic operation repetitions) over all possible inputs of size n.Under some assumption about the probability distribution of all possible inputs of size n, calculate the weighted sum of expected C(n) (numbers of basic operation repetitions) over all possible inputs of size n.
  • Slide 24
  • Design and Analysis of Algorithms Chapter 2.1 24 Example: Sequential search b Problem: Given a list of n elements and a search key K, find an element equal to K, if any. b Algorithm: Scan the list and compare its successive elements with K until either a matching element is found (successful search) or the list is exhausted (unsuccessful search) b Worst case b Best case b Average case
  • Slide 25
  • Design and Analysis of Algorithms Chapter 2.1 25 An example b Compute gcd(m, n) by applying the algorithm based on checking consecutive integers from min(m, n) down to gcd(m, n) b Input size? b Best case? b Worst case? b Average case?
  • Slide 26
  • Design and Analysis of Algorithms Chapter 2.1 26 Types of formulas for basic operation count b Exact formula e.g., C(n) = n(n-1)/2 e.g., C(n) = n(n-1)/2 b Formula indicating order of growth with specific multiplicative constant e.g., C(n) 0.5 n 2 e.g., C(n) 0.5 n 2 b Formula indicating order of growth with unknown multiplicative constant e.g., C(n) cn 2 e.g., C(n) cn 2
  • Slide 27
  • Design and Analysis of Algorithms Chapter 2.1 27 Types of formulas for basic operation count b Exact formula e.g., C(n) = n(n-1)/2 e.g., C(n) = n(n-1)/2 b Formula indicating order of growth with specific multiplicative constant e.g., C(n) 0.5 n 2 e.g., C(n) 0.5 n 2 b Formula indicating order of growth with unknown multiplicative constant e.g., C(n) cn 2 e.g., C(n) cn 2 b Most important: Order of growth within a constant multiple as n
  • Slide 28
  • Design and Analysis of Algorithms Chapter 2.1 28 Asymptotic growth rate b A way of comparing functions that ignores constant factors and small input sizes b O(g(n)): b (g(n)): b (g(n)):
  • Slide 29
  • Design and Analysis of Algorithms Chapter 2.1 29 Asymptotic growth rate b A way of comparing functions that ignores constant factors and small input sizes b O(g(n)): class of functions f(n) that grow no faster than g(n) b (g(n)): class of functions f(n) that grow at same rate as g(n) b (g(n)): class of functions f(n) that grow at least as fast as g(n)
  • Slide 30
  • Design and Analysis of Algorithms Chapter 2.1 30 Table 2.1
  • Slide 31
  • Classifying Functions Giving an idea of how fast a function grows without going into too much detail.
  • Slide 32
  • Design and Analysis of Algorithms Chapter 2.1 32 Which are more alike?
  • Slide 33
  • Design and Analysis of Algorithms Chapter 2.1 33 Which are more alike? Mammals
  • Slide 34
  • Design and Analysis of Algorithms Chapter 2.1 34 Which are more alike?
  • Slide 35
  • Design and Analysis of Algorithms Chapter 2.1 35 Which are more alike? Dogs
  • Slide 36
  • Design and Analysis of Algorithms Chapter 2.1 36 Classifying Animals Vertebrates BirdsMammals Reptiles Fish Dogs Giraffe
  • Slide 37
  • Design and Analysis of Algorithms Chapter 2.1 37 Which are more alike? n 1000 n2n2 2n2n
  • Slide 38
  • Design and Analysis of Algorithms Chapter 2.1 38 Which are more alike? Polynomials n 1000 n2n2 2n2n
  • Slide 39
  • Design and Analysis of Algorithms Chapter 2.1 39 Which are more alike? 1000n 2 3n 2 2n 3
  • Slide 40
  • Design and Analysis of Algorithms Chapter 2.1 40 Which are more alike? Quadratic 1000n 2 3n 2 2n 3
  • Slide 41
  • Design and Analysis of Algorithms Chapter 2.1 41 Classifying Functions? Functions
  • Slide 42
  • Design and Analysis of Algorithms Chapter 2.1 42 Classifying Functions Functions Constant Logarithmic Poly Logarithmic Polynomial Exponential Factorial (log n) 5 n5n5 2 5n 5 log n5 n!
  • Slide 43
  • Design and Analysis of Algorithms Chapter 2.1 43 Classifying Functions? Polynomial
  • Slide 44
  • Design and Analysis of Algorithms Chapter 2.1 44 Classifying Functions Polynomial LinearQuadratic Cubic ? 5n 2 5n 5n 3 5n 4
  • Slide 45
  • Design and Analysis of Algorithms Chapter 2.1 45 Logarithmic b log 10 n = # digits to write n b log 2 n = # bits to write n = 3.32 log 10 n b log(n 1000 ) = 1000 log(n) Differ only by a multiplicative constant
  • Slide 46
  • Design and Analysis of Algorithms Chapter 2.1 46 Poly Logarithmic (log n) 5 = log 5 n
  • Slide 47
  • Design and Analysis of Algorithms Chapter 2.1 47 Which grows faster? log 1000 n n 0.001
  • Slide 48
  • Design and Analysis of Algorithms Chapter 2.1 48 Poly Logarithmic