Escolar Documentos
Profissional Documentos
Cultura Documentos
T(n) copC(n)
running time
execution time
for basic operation
Number of times
basic operation is
executed
Basic operation
Key comparison
Multiplication of two
matrices
Matrix dimensions or
total number of elements
Multiplication of two
numbers
Checking primality of
a given integer n
Visiting a vertex or
traversing an edge
3
Best case:
Worst case
n key comparisons
Best case
Average case
1 comparisons
(n+1)/2, assuming K is in A
5
Order of growth
Most important: Order of growth within a constant multiple
as n
Example:
How much faster will algorithm run on computer that is twice
as fast?
Notations:
O (big oh)
(big omega), and
(big theta).
Assume
t (n) and g(n) can be any nonnegative functions defined on the set of
natural numbers.
t(n) will be an algorithms running time (usually indicated by its
basic operation count C(n)), and g(n) will be some simple function
to compare the count with.
Big-oh
Afunctiont(n)issaidtobeinO(g(n)),denotedt(n)O(g(n)),
ift(n)isboundedabovebysomeconstantmultipleofg(n)forall
largen,i.e.,ifthereexistsomepositiveconstantcandsome
nonnegativeintegern0suchthatt(n)cg(n)forallnn0.
Big-omega
Afunctiont(n)issaidtobein(g(n)),denotedt(n)(g(n)),if
t(n)isboundedbelowbysomepositiveconstantmultipleofg(n)
for all large n, i.e., if there exist some positive constant c and
somenonnegativeintegern0suchthatt(n)cg(n)forallnn0.
10
Big-theta
Afunctiont(n)issaidtobein(g(n)),denotedt(n) (g(n)),if
t(n)isboundedbothaboveandbelowbysomepositiveconstant
multiplesofg(n)foralllargen,i.e.,ifthereexistsomepositive
constants c1 and c2 and some nonnegative integer n0 such that
c2g(n)t(n)c1g(n)forallnn0.
11
Examples
Compare the orders of growth of 1/2 n(n 1) and n2.
13
constant
log n
logarithmic
linear
n log n
n-log-n or linearithmic
n2
quadratic
n3
cubic
2n
exponential
n!
factorial
14
15
16
17
BasicOperation:
multiplicationasthebasicoperation
18
19
20
Analysis
21
22
M(n1)moves
M(n1)moves
M(1)move
Recurrenceformoves: M(n)=M(n1)+1+M(n1)=2M(n1)+1
M(1)=1
23
24
Tower of Hanoi
25
Fibonacci numbers
The Fibonacci numbers:
0, 1, 1, 2, 3, 5, 8, 13, 21,
The Fibonacci recurrence:
F(n) = F(n-1) + F(n-2)
F(0) = 0
F(1) = 1
General 2nd order linear homogeneous recurrence with
constant coefficients:
aX(n) + bX(n-1) + cX(n-2) = 0
26
X(n) = rn + nr n
27
28
29