Download presentation
Presentation is loading. Please wait.
Published byFlorence Hamilton Modified over 9 years ago
1
Design and Analysis of Algorithms Chapter 2.1 1 Analysis of Algorithms Dr. Ying Lu ylu@cse.unl.edu August 28, 2012 http://www.cse.unl.edu/~ylu/raik283/ RAIK 283: Data Structures & Algorithms
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 Textbook’s companion websiteMost of the lecture notes are based on the slides from the Textbook’s 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
3
The Time Complexity of an Algorithm
4
Specifies how the running time depends on the size of the input
5
Design and Analysis of Algorithms Chapter 2.1 5 Purpose
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.
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:
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
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)
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!
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.
12
Design and Analysis of Algorithms Chapter 2.1 12 Definition of Time?
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).
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
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
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
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
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
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
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
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?
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)?
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.
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
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?
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
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→∞
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)):
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)
30
Design and Analysis of Algorithms Chapter 2.1 30 Table 2.1
31
Classifying Functions Giving an idea of how fast a function grows without going into too much detail.
32
Design and Analysis of Algorithms Chapter 2.1 32 Which are more alike?
33
Design and Analysis of Algorithms Chapter 2.1 33 Which are more alike? Mammals
34
Design and Analysis of Algorithms Chapter 2.1 34 Which are more alike?
35
Design and Analysis of Algorithms Chapter 2.1 35 Which are more alike? Dogs
36
Design and Analysis of Algorithms Chapter 2.1 36 Classifying Animals Vertebrates BirdsMammals Reptiles Fish Dogs Giraffe
37
Design and Analysis of Algorithms Chapter 2.1 37 Which are more alike? n 1000 n2n2 2n2n
38
Design and Analysis of Algorithms Chapter 2.1 38 Which are more alike? Polynomials n 1000 n2n2 2n2n
39
Design and Analysis of Algorithms Chapter 2.1 39 Which are more alike? 1000n 2 3n 2 2n 3
40
Design and Analysis of Algorithms Chapter 2.1 40 Which are more alike? Quadratic 1000n 2 3n 2 2n 3
41
Design and Analysis of Algorithms Chapter 2.1 41 Classifying Functions? Functions
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!
43
Design and Analysis of Algorithms Chapter 2.1 43 Classifying Functions? Polynomial
44
Design and Analysis of Algorithms Chapter 2.1 44 Classifying Functions Polynomial LinearQuadratic Cubic ? 5n 2 5n 5n 3 5n 4
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
46
Design and Analysis of Algorithms Chapter 2.1 46 Poly Logarithmic (log n) 5 = log 5 n
47
Design and Analysis of Algorithms Chapter 2.1 47 Which grows faster? log 1000 n n 0.001
48
Design and Analysis of Algorithms Chapter 2.1 48 Poly Logarithmic << Polynomial For sufficiently large n log 1000 n << n 0.001
49
Design and Analysis of Algorithms Chapter 2.1 49 Which grows faster? 10000 n 0.0001 n 2
50
Design and Analysis of Algorithms Chapter 2.1 50 Linear << Quadratic For sufficiently large n 10000 n << 0.0001 n 2
51
Design and Analysis of Algorithms Chapter 2.1 51 Which grows faster? n 1000 2 0.001 n
52
Design and Analysis of Algorithms Chapter 2.1 52 Polynomial << Exponential For sufficiently large n n 1000 << 2 0.001 n
53
Design and Analysis of Algorithms Chapter 2.1 53 Ordering Functions Functions Constant Logarithmic Poly Logarithmic Polynomial Exponential (log n) 5 n5n5 2 5n 5 log n5 << Factorial n!
54
Design and Analysis of Algorithms Chapter 2.1 54 Which Functions are Constant? 5 1,000,000,000,000 0.0000000000001 -5 0 8 + sin(n)
55
Design and Analysis of Algorithms Chapter 2.1 55 Which Functions are Constant? 5 1,000,000,000,000 0.0000000000001 -5 0 8 + sin(n) Yes No Yes
56
Design and Analysis of Algorithms Chapter 2.1 56 Which Functions are “Constant”? 5 1,000,000,000,000 0.0000000000001 -5 0 8 + sin(n) The running time of the algorithm is a “Constant” if it does not depend significantly on the size of the input.
57
Design and Analysis of Algorithms Chapter 2.1 57 Which Functions are “Constant”? 5 1,000,000,000,000 0.0000000000001 -5 0 8 + 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.
58
Design and Analysis of Algorithms Chapter 2.1 58 Which Functions are Quadratic? n 2 0.001 n 2 1000 n 2 5n 2 + 3n + 2log n
59
Design and Analysis of Algorithms Chapter 2.1 59 Which Functions are Quadratic? n 2 0.001 n 2 1000 n 2 5n 2 + 3n + 2log n Lie in between
60
Design and Analysis of Algorithms Chapter 2.1 60 Which Functions are Quadratic? n 2 0.001 n 2 1000 n 2 5n 2 + 3n + 2log n Ignore low-order terms Ignore multiplicative constants. Ignore "small" values of n. Write θ(n 2 ).
61
Design and Analysis of Algorithms Chapter 2.1 61 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 )
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.