Algorithm Analysis
Algorithm Analysis
Analysis of Algorithms
Dr. Ying Lu ylu@cse.unl.edu
August 28, 2012
http://www.cse.unl.edu/~ylu/raik283/
Giving
Most of the lecture notes are based on the slides from the Textbooks companion website http://www.aw-bc.com/info/levitin Several slides are from Jeff Edmonds of the York University I have modified them and added new slides
Specifies how the running time depends on the size of the input
Purpose
Purpose
To estimate how long a program will run. To estimate the largest input that can reasonably be given to the program. To compare the efficiency of different algorithms. To help focus on the parts of code that are executed the largest number of times. To choose an algorithm for an application.
Purpose (Example)
Suppose a machine that performs a million floatingpoint operations per second (106 FLOPS), then how long an algorithm will run for an input of size n=50?
1) If the algorithm requires n2 such operations:
Purpose (Example)
Suppose a machine that performs a million floatingpoint operations per second (106 FLOPS), then how long an algorithm will run for an input of size n=50?
1) If the algorithm requires n2 such operations:
0.0025 second
Purpose (Example)
Suppose a machine that performs a million floatingpoint operations per second (106 FLOPS), then how long an algorithm will run for an input of size n=50?
1) If the algorithm requires n2 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)
Purpose (Example)
Suppose a machine that performs a million floatingpoint operations per second (106 FLOPS), then how long an algorithm will run for an input of size n=50?
1) If the algorithm requires n2 such operations:
10
11
Definition of Time?
12
Definition of Time
# of seconds (machine, implementation dependent). # lines of code executed. # of times a specific operation is performed addition). (e.g.,
13
Basic operation: the operation that contributes most towards the running time of the algorithm.
input size
T(n) copC(n)
running time execution time for basic operation Number of times basic operation is executed
14
Basic operation
Graph problem
Design and Analysis of Algorithms Chapter 2.1
15
Search for key in a list of Number of items in the n items list: n Multiply two matrices of floating point numbers Compute an
Graph problem
Design and Analysis of Algorithms Chapter 2.1
16
Search for key in a list of Number of items in the n items list: n Multiply two matrices of Dimensions of matrices floating point numbers Compute an
Graph problem
Design and Analysis of Algorithms Chapter 2.1
17
Search for key in list of n Number of items in list n Key comparison items Multiply two matrices of Dimensions of matrices floating point numbers Compute an Floating point multiplication
Graph problem
Design and Analysis of Algorithms Chapter 2.1
18
Search for key in list of n Number of items in list n Key comparison items Multiply two matrices of Dimensions of matrices floating point numbers Compute an Floating point multiplication
Graph problem
Time efficiency is analyzed by determining the number of repetitions of the basic operation as a function of input size
20
21
For instance: search a key in a list of n letters There are 26n inputs of size n. Which do we consider for the time efficiency C(n)?
22
W(n) maximum over inputs of size n B(n) minimum over inputs of size n
23
Problem: Given a list of n elements and a search key K, find an element equal to K, if any. 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) Worst case
Best case
Average case
Design and Analysis of Algorithms Chapter 2.1
24
An example
Compute gcd(m, n) by applying the algorithm based on checking consecutive integers from min(m, n) down to gcd(m, n) Input size? Best case? Worst case? Average case?
25
Exact formula e.g., C(n) = n(n-1)/2 Formula indicating order of growth with specific multiplicative constant e.g., C(n) 0.5 n2 Formula indicating order of growth with unknown multiplicative constant e.g., C(n) cn2
26
Exact formula e.g., C(n) = n(n-1)/2 Formula indicating order of growth with specific multiplicative constant e.g., C(n) 0.5 n2 Formula indicating order of growth with unknown multiplicative constant e.g., C(n) cn2 Most important: Order of growth within a constant multiple as n
Design and Analysis of Algorithms Chapter 2.1
27
A way of comparing functions that ignores constant factors and small input sizes O(g(n)): (g(n)): (g(n)):
28
A way of comparing functions that ignores constant factors and small input sizes O(g(n)): class of functions f(n) that grow no faster than g(n) (g(n)): class of functions f(n) that grow at same rate as g(n) (g(n)): class of functions f(n) that grow at least as fast as g(n)
29
Table 2.1
30
Classifying Functions
Giving an idea of how fast a function grows without going into too much detail.
32
Mammals
33
34
Dogs
35
Classifying Animals
Vertebrates Fish Birds
Mammals Dogs
Giraffe
Reptiles
36
n1000
n2
2n
37
n1000
n2
Polynomials
2n
38
1000n2
3n2
2n3
39
1000n2
Quadratic
3n2
2n3
40
Classifying Functions?
Functions
41
Classifying Functions
Functions
Constant
Poly Logarithmic
(log n)5
Exponential
5 log n
Logarithmic
Polynomial
n5
Factorial
n!
42
25n
Classifying Functions?
Polynomial
43
Classifying Functions
Polynomial Linear
5n
5n2
Quadratic
5n3
Cubic
5n4
44
Logarithmic
log10n
= # digits to write n log2n = # bits to write n = 3.32 log10n log(n1000) = 1000 log(n)
45
Poly Logarithmic
46
log1000 n
n0.001
47
48
10000 n
0.0001 n2
49
50
n1000
20.001 n
51
52
Ordering Functions
Functions
Constant
Poly Logarithmic
Exponential
Logarithmic
Polynomial
Factorial
<<
<<
<<
<<
<<
<<
25n <<
n!
53
54
55
58
59
Examples
f(n) 1) 2) 3) 4) ln2n nk g(n) ne cn nsinn 2n/2 O(g(n))? (g(n))? (g(n))?
n
2n
5)
6)
nlgc
clgn
lg(n!) lg(nn)
61