Professional Documents
Culture Documents
Algorithm Analysis Design Lecture2 PowerPoint Presentation
Algorithm Analysis Design Lecture2 PowerPoint Presentation
Algorithm Analysis Design Lecture2 PowerPoint Presentation
of Algorithm Efficiency
Analysis of Algorithms
11/13/2014
Analysis Framework
11/13/2014
11/13/2014
11/13/2014
input size
11/13/2014
execution time
for the basic operation
Orders of Growth
Orders of Growth
Exponential-growth functions
11/13/2014
700
600
500
n*n*n
400
n*n
n log(n)
n
300
log(n)
200
100
0
1
11/13/2014
10
11
ALGORITHM SequentialSearch(A[0..n-1], K)
//Searches for a given value in a given array by sequential search
//Input: An array A[0..n-1] and a search key K
//Output: Returns the index of the first element of A that matches K
or 1 if there are no matching elements
i 0
while i < n and A[i] K do
i i+ 1
if i < n
//A[I] = K
return i
else
return -1
11/13/2014
12
Best case
Average case:
13
11/13/2014
14
11/13/2014
15
O-notation
11/13/2014
16
O-notation
Formal definition
Exercises:
10n2 O(n2)
100n + 5 O(n2)
5n+20 O(n)
11/13/2014
17
-notation
11/13/2014
18
-notation
Formal definition
Exercises:
10n2 (n2)
10n2 + 2n (n2)
10n3 (n2)
11/13/2014
19
-notation
11/13/2014
20
-notation
Formal definition
Exercises:
10n2 (n2)
10n2 + 2n (n2)
(1/2)n(n-1) (n2)
11/13/2014
21
>=
(g(n)), functions that grow at least as fast as g(n)
g(n)
=
(g(n)), functions that grow at the same rate as g(n)
<=
O(g(n)), functions that grow no faster than g(n)
11/13/2014
22
Theorem
For example,
11/13/2014
23
limn T(n)/g(n) =
<
c>0
Examples:
10n
vs.
2n2
n(n+1)/2
vs.
n2
logb n
vs.
logc n
11/13/2014
=
>
24
LHpitals rule
If
Then
lim
n
f(n)
g(n) =
lim
n
f (n)
g (n)
25
LHpitals rule
11/13/2014
26
1
log n
n
n log n
n2
n3
2n
n!
slow
constant
logarithmic
linear
n log n
quadratic
cubic
exponential
factorial
The time
efficiencies of a
large number
of algorithms
fall into only a
few classes.
High time efficiency
11/13/2014
27
11/13/2014
28
11/13/2014
29
30
31
10
32
Mathematical Analysis of
Recursive Algorithms
Recursive evaluation of n!
Recursive solution to the number of
binary digits problem
Recursive solution to the Tower of
Hanoi Puzzle
Irregular change to the loops variable
11/13/2014
33
Iterative Definition
F(n) = 1
n * (n-1) * (n-2) 3 * 2 * 1
if n = 0
if n > 0
Recursive definition
F(n) = 1
n * F(n-1)
Algorithm F(n)
if n=0
else
11/13/2014
if n = 0
if n > 0
return 1
//base case
return F (n -1) * n
//general case
34
11
Two Recurrences
11/13/2014
35
36
11/13/2014
37
12
Smoothness Rule
f(n)=
11/13/2014
38
Smoothness Rule
(2)
Smoothness rule
let T(n) be an eventually nondecreasing function and
f(n) be a smooth function. If
11/13/2014
39
13
Decrease-by-one recurrences
Decrease-by-a-constant-factor recurrences
11/13/2014
41
Decrease-by-a-constant-factor
recurrences The Master Theorem
T(n) = aT(n/b) + f (n),
1.
2.
3.
a < bd
a = bd
a > bd
T(n) (nd)
T(n) (ndlog n )
T(n) (nlog b a)
Examples:
T(n) = T(n/2) + 1
T(n) = 2T(n/2) + n
T(n) = 3 T(n/2) + n
11/13/2014
(logn)
(nlogn)
(nlog23)
42
14