Chapter 2
Chapter 2
Fundamental of the
Analysis of Alg.
Efficiency
Gedamu A.
[email protected]
Image processing and Computer vision SIG
Some of the sides are exported from different sources to clarify the topic
Issues:
Correctness
space efficiency
time efficiency
optimality
Approaches:
Theoretical analysis
Empirical analysis
T(n) ≈ copC(n)
running time execution time Number of times
basic operation
for basic operation
is executed
or cost
Note: Different basic operations may cost differently!
Visiting a vertex or
Typical graph problem #vertices and/or edges
traversing an edge
C(n) = ½ n (n-1) = ½ n2 – ½ n ≈ ½ n2
How much longer if the algorithm doubles its input
size? 1
( 2n) 2
T (2n) COP C (2n) 2
T (n)
4
COP C (n) 1 2
Increasing input size increases the complexity ( n)
2
We tend to omit the constants because they have no effect with large inputs
Everything is based on estimation
p p p p
C avg (n) [1. 2. 3. ... n ] n.(1 p )
n n n n
p
[1 2 3 ...n] n.(1 p )
n
p n(n 1) p (n 1)
. n.(1 p ) n(1 p )
n 2 2
The average Case is more difficult than the Best and Worst cases
p p p p
C avg (n) [1. 2. 3. ... n ] n.(1 p )
n n n n
p
[1 2 3 ...n] n.(1 p )
n
p n(n 1) p (n 1)
. n.(1 p ) n(1 p )
n 2 2
Mathematical Background
Mathematical Background
Logarithms
Which Base ?
Order of growth
Order of growth
Of greater concern is the rate of increase in operations for an
algorithm to solve a problem as the size of the problem
increases.
Classification of Growth
Classification of Growth: Asymptotic order of growth
an 2 bn (n 2 )
n 3 n 2 for all n 0
c 2 and n0 1
>=
(g(n)), functions that grow at least as fast as g(n)
=
(g(n)), functions that grow at the same rate as g(n)
g(n)
<=
O(g(n)), functions that grow no faster than g(n)
1 constant
log n logarithmic
n linear
n log n n-log-n
n2 quadratic
n3 cubic
2n exponential
n! factorial
Set up a sum for the number of times the basic operation is executed
1 u l 1
i l
n2
n 1 n 1 n 1
T(n) = 1 =
i 0 j 0 k 0
( n3 ) multiplications
Ignored addition for simplicity
Size: N
Basic operation: Multiplication
Recurrence relation: M(n) = M(n-1) (for F(n-1) ) + 1 (for the n * F(n-1))
M(0) = 0
M(n) = M(n-1) + 1
= (M(n-2) + 1) + 1 = M(n-2) + 2
= (M(n-3) + 1) + 2 = M(n-3) + 3
…
= M(n-i) + i
= M(0) + n
=n
The method is called backward
substitution.
• The number of discs can vary, but there are only three
towers.
• The goal is to transfer the discs from one tower another tower.
However you can move only one disk at a time and you can
never place a bigger disc over a smaller disk. It is also
understood that you can only take the top most disc from any
given tower.
Computer Science and Engineering Program:DAA
Gedamu Alemu
Example 2: The Tower of Hanoi Puzzle
7
Computer Science and Engineering Program:DAA
Gedamu Alemu
The Tower of Hanoi Puzzle
Here's how to find the number of moves needed to transfer larger
numbers of disks from post A to post C, when M = the number of
moves needed to transfer n-1 disks from post A to post C:
for 1 disk it takes 1 move to transfer 1 disk from post A to post C;
for 2 disks, it will take 3 moves: 2M + 1 = 2(1) + 1 = 3
for 3 disks, it will take 7 moves: 2M + 1 = 2(3) + 1 = 7
for 4 disks, it will take 15 moves: 2M + 1 = 2(7) + 1 = 15
for 5 disks, it will take 31 moves: 2M + 1 = 2(15) + 1 = 31
for 6 disks... ?
2 n- 1
M(n) = 2M(n-1) + 1
= 2(2M(n-2) + 1) + 1 = 2^2*M(n-2) + 2^1 + 2^0
= 2^2*(2M(n-3) + 1) + 2^1 + 2^0
= 2^3*M(n-3) + 2^2 + 2^1 + 2^0
=…
= 2^(n-1)*M(1) + 2^(n-2) + … + 2^1 + 2^0
= 2^(n-1) + 2^(n-2) + … + 2^1 + 2^0
= 2^n - 1