Algorithm Analysis
Algorithm Analysis
Algorithms
Analysis of Algorithms
Dr. Ying Lu
ylu@cse.unl.edu
http://www.cse.unl.edu/~ylu/raik283/
1
Design and Analysis of Algorithms Chapter 2.1
RAIK 283: Data Structures &
Algorithms
2
Design and Analysis of Algorithms Chapter 2.1
The Time Complexity of an
Algorithm
The Time Complexity of an
Algorithm
5
Design and Analysis of Algorithms Chapter 2.1
Purpose
6
Design and Analysis of Algorithms Chapter 2.1
Purpose (Example)
7
Design and Analysis of Algorithms Chapter 2.1
Purpose (Example)
Suppose a machine that performs a million floating-
point 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
8
Design and Analysis of Algorithms Chapter 2.1
Purpose (Example)
9
Design and Analysis of Algorithms Chapter 2.1
Purpose (Example)
10
Design and Analysis of Algorithms Chapter 2.1
Time Complexity Is a Function
Specifies how the running time depends on the size of the
input.
A function mapping
“size” of input
11
Design and Analysis of Algorithms Chapter 2.1
Definition of Time?
12
Design and Analysis of Algorithms Chapter 2.1
Definition of Time
13
Design and Analysis of Algorithms Chapter 2.1
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
T(n) ≈ copC(n)
running time execution time Number of times
for basic operation basic operation is
executed
14
Design and Analysis of Algorithms Chapter 2.1
Input size and basic operation
examples
Compute an
Graph problem
15
Design and Analysis of Algorithms Chapter 2.1
Input size and basic operation
examples
Compute an
Graph problem
16
Design and Analysis of Algorithms Chapter 2.1
Input size and basic operation
examples
Compute an
Graph problem
17
Design and Analysis of Algorithms Chapter 2.1
Input size and basic operation
examples
Floating point
Compute an n
multiplication
Graph problem
18
Design and Analysis of Algorithms Chapter 2.1
Input size and basic operation
examples
Floating point
Compute an n
multiplication
Visiting a vertex or
Graph problem #vertices and/or edges
traversing an edge
19
Design and Analysis of Algorithms Chapter 2.1
Theoretical analysis of time
efficiency
20
Design and Analysis of Algorithms Chapter 2.1
Which Input of size n?
21
Design and Analysis of Algorithms Chapter 2.1
Which Input of size n?
22
Design and Analysis of Algorithms Chapter 2.1
Best-case, average-case, worst-
case
23
Design and Analysis of Algorithms Chapter 2.1
Example: Sequential search
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
24
Design and Analysis of Algorithms Chapter 2.1
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
Design and Analysis of Algorithms Chapter 2.1
Types of formulas for basic
operation count
Exact formula
e.g., C(n) = n(n-1)/2
26
Design and Analysis of Algorithms Chapter 2.1
Types of formulas for basic
operation count
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
27
Design and Analysis of Algorithms Chapter 2.1
Asymptotic growth rate
A way of comparing functions that ignores constant factors
and small input sizes
O(g(n)):
Θ (g(n)):
Ω(g(n)):
28
Design and Analysis of Algorithms Chapter 2.1
Asymptotic growth rate
A way of comparing functions that ignores constant factors
and small input sizes
29
Design and Analysis of Algorithms Chapter 2.1
Table 2.1
30
Design and Analysis of Algorithms Chapter 2.1
Classifying Functions
32
Design and Analysis of Algorithms Chapter 2.1
Which are more alike?
Mammals
33
Design and Analysis of Algorithms Chapter 2.1
Which are more alike?
34
Design and Analysis of Algorithms Chapter 2.1
Which are more alike?
Dogs
35
Design and Analysis of Algorithms Chapter 2.1
Classifying Animals
Vertebrates
Fish
Birds
Mammals
Reptiles
Dogs
Giraffe
36
Design and Analysis of Algorithms Chapter 2.1
Which are more alike?
n1000 n2 2n
37
Design and Analysis of Algorithms Chapter 2.1
Which are more alike?
n1000 n2 2n
Polynomials
38
Design and Analysis of Algorithms Chapter 2.1
Which are more alike?
39
Design and Analysis of Algorithms Chapter 2.1
Which are more alike?
Quadratic
40
Design and Analysis of Algorithms Chapter 2.1
Classifying Functions?
Functions
41
Design and Analysis of Algorithms Chapter 2.1
Classifying Functions
Functions
Poly Logarithmic
Exponential
Constant
Logarithmic
Polynomial
Factorial
5 5 log n (log n)5 n5 25n n!
42
Design and Analysis of Algorithms Chapter 2.1
Classifying Functions?
Polynomial
43
Design and Analysis of Algorithms Chapter 2.1
Classifying Functions
Polynomial
Linear
Quadratic
?
Cubic
5n 5n2 5n3 5n4
44
Design and Analysis of Algorithms Chapter 2.1
Logarithmic
45
Design and Analysis of Algorithms Chapter 2.1
Poly Logarithmic
46
Design and Analysis of Algorithms Chapter 2.1
Which grows faster?
log1000 n n0.001
47
Design and Analysis of Algorithms Chapter 2.1
Poly Logarithmic <<
Polynomial
48
Design and Analysis of Algorithms Chapter 2.1
Which grows faster?
10000 n 0.0001 n2
49
Design and Analysis of Algorithms Chapter 2.1
Linear << Quadratic
50
Design and Analysis of Algorithms Chapter 2.1
Which grows faster?
n1000 20.001 n
51
Design and Analysis of Algorithms Chapter 2.1
Polynomial << Exponential
52
Design and Analysis of Algorithms Chapter 2.1
Ordering Functions
Functions
Poly Logarithmic
Exponential
Constant
Logarithmic
Polynomial
Factorial
<< << << << <<
• 5
• 1,000,000,000,000
• 0.0000000000001
• -5
• 0
• 8 + sin(n)
54
Design and Analysis of Algorithms Chapter 2.1
Which Functions are Constant?
Yes • 5
Yes • 1,000,000,000,000
Yes • 0.0000000000001
Yes • -5
Yes • 0
No • 8 + sin(n)
55
Design and Analysis of Algorithms Chapter 2.1
Which Functions are “Constant”?
• n2
• 0.001 n2
• 1000 n2
• 5n2 + 3n + 2log n
58
Design and Analysis of Algorithms Chapter 2.1
Which Functions are Quadratic?
• n2
• 0.001 n2
• 1000 n2 Lie in between
• 5n2 + 3n + 2log n
59
Design and Analysis of Algorithms Chapter 2.1
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).
60
Design and Analysis of Algorithms Chapter 2.1
Examples
61
Design and Analysis of Algorithms Chapter 2.1