Design and Analysis of Algorithms Chapter Analysis of Algorithms Dr. Ying Lu August 28, RAIK 283: Data Structures & Algorithms
Design and Analysis of Algorithms Chapter b Giving credit where credit is due: Most of the lecture notes are based on the slides from the Textbook’s companion websiteMost of the lecture notes are based on the slides from the Textbook’s companion website 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
The Time Complexity of an Algorithm
Specifies how the running time depends on the size of the input
Design and Analysis of Algorithms Chapter Purpose
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.
Design and Analysis of Algorithms Chapter 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:
Design and Analysis of Algorithms Chapter 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: – second
Design and Analysis of Algorithms Chapter 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: – 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)
Design and Analysis of Algorithms Chapter 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: – second 2) If the algorithm requires 2 n such operations:2) If the algorithm requires 2 n such operations: –over 35 years!
Design and Analysis of Algorithms Chapter 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.
Design and Analysis of Algorithms Chapter Definition of Time?
Design and Analysis of Algorithms Chapter 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).
Design and Analysis of Algorithms Chapter 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
Design and Analysis of Algorithms Chapter 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
Design and Analysis of Algorithms Chapter 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
Design and Analysis of Algorithms Chapter 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
Design and Analysis of Algorithms Chapter 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
Design and Analysis of Algorithms Chapter 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
Design and Analysis of Algorithms Chapter 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
Design and Analysis of Algorithms Chapter 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?
Design and Analysis of Algorithms Chapter 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)?
Design and Analysis of Algorithms Chapter 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.
Design and Analysis of Algorithms Chapter 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
Design and Analysis of Algorithms Chapter 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?
Design and Analysis of Algorithms Chapter 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
Design and Analysis of Algorithms Chapter 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→∞
Design and Analysis of Algorithms Chapter 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)):
Design and Analysis of Algorithms Chapter 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)
Design and Analysis of Algorithms Chapter Table 2.1
Classifying Functions Giving an idea of how fast a function grows without going into too much detail.
Design and Analysis of Algorithms Chapter Which are more alike?
Design and Analysis of Algorithms Chapter Which are more alike? Mammals
Design and Analysis of Algorithms Chapter Which are more alike?
Design and Analysis of Algorithms Chapter Which are more alike? Dogs
Design and Analysis of Algorithms Chapter Classifying Animals Vertebrates BirdsMammals Reptiles Fish Dogs Giraffe
Design and Analysis of Algorithms Chapter Which are more alike? n 1000 n2n2 2n2n
Design and Analysis of Algorithms Chapter Which are more alike? Polynomials n 1000 n2n2 2n2n
Design and Analysis of Algorithms Chapter Which are more alike? 1000n 2 3n 2 2n 3
Design and Analysis of Algorithms Chapter Which are more alike? Quadratic 1000n 2 3n 2 2n 3
Design and Analysis of Algorithms Chapter Classifying Functions? Functions
Design and Analysis of Algorithms Chapter Classifying Functions Functions Constant Logarithmic Poly Logarithmic Polynomial Exponential Factorial (log n) 5 n5n5 2 5n 5 log n5 n!
Design and Analysis of Algorithms Chapter Classifying Functions? Polynomial
Design and Analysis of Algorithms Chapter Classifying Functions Polynomial LinearQuadratic Cubic ? 5n 2 5n 5n 3 5n 4
Design and Analysis of Algorithms Chapter 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
Design and Analysis of Algorithms Chapter Poly Logarithmic (log n) 5 = log 5 n
Design and Analysis of Algorithms Chapter Which grows faster? log 1000 n n 0.001
Design and Analysis of Algorithms Chapter Poly Logarithmic << Polynomial For sufficiently large n log 1000 n << n 0.001
Design and Analysis of Algorithms Chapter Which grows faster? n n 2
Design and Analysis of Algorithms Chapter Linear << Quadratic For sufficiently large n n << n 2
Design and Analysis of Algorithms Chapter Which grows faster? n n
Design and Analysis of Algorithms Chapter Polynomial << Exponential For sufficiently large n n 1000 << n
Design and Analysis of Algorithms Chapter Ordering Functions Functions Constant Logarithmic Poly Logarithmic Polynomial Exponential (log n) 5 n5n5 2 5n 5 log n5 << Factorial n!
Design and Analysis of Algorithms Chapter Which Functions are Constant? 5 1,000,000,000, sin(n)
Design and Analysis of Algorithms Chapter Which Functions are Constant? 5 1,000,000,000, sin(n) Yes No Yes
Design and Analysis of Algorithms Chapter Which Functions are “Constant”? 5 1,000,000,000, sin(n) The running time of the algorithm is a “Constant” if it does not depend significantly on the size of the input.
Design and Analysis of Algorithms Chapter Which Functions are “Constant”? 5 1,000,000,000, sin(n) Yes No Yes Lie in between 7 9 The running time of the algorithm is a “Constant” It does not depend significantly on the size of the input.
Design and Analysis of Algorithms Chapter Which Functions are Quadratic? n n n 2 5n 2 + 3n + 2log n
Design and Analysis of Algorithms Chapter Which Functions are Quadratic? n n n 2 5n 2 + 3n + 2log n Lie in between
Design and Analysis of Algorithms Chapter Which Functions are Quadratic? n n n 2 5n 2 + 3n + 2log n Ignore low-order terms Ignore multiplicative constants. Ignore "small" values of n. Write θ(n 2 ).
Design and Analysis of Algorithms Chapter Examples f(n)g(n)O(g(n))?Ω(g(n))?Θ(g(n))? 1)ln 2 nn 2)nknk cncn 3)n sinn 4)2n2n 2 n/2 5)n lgc c lgn 6)lg(n!)lg(n n )