Algorithms
Algorithms
• To analyze algorithms:
– First, we start to count the number of significant
operations in a particular solution to assess its
efficiency.
– Then, we will express the efficiency of algorithms
using growth functions.
A sequence of operations:
Total Cost = c1 + c2
n * ( n 1) * ( 2 n 1)
n 3
n
i 1 4 ... n
2 2
i 1 6 3
n 1
n 1
2 0 1 2 ... 2 2 1
i n
i0
x=x+1; c4 k
j 1 k 1
n n j
j
( j 1) ) + c3* ( ( k 1) ) + c4*( k )
n
when n=0
T(0) = c1
when n>0
T(n) = c1 + c2 + T(n-1) + c3 + c4 + T(n-1)
= 2*T(n-1) + (c1+c2+c3+c4)
= 2*T(n-1) + c recurrence equation for the growth-rate
function of hanoi-towers algorithm
T(n) = 2*T(n-1) + c
= 2 * (2*T(n-2)+c) + c
= 2 * (2* (2*T(n-3)+c) + c) + c
= 23 * T(n-3) + (22+21+20)*c (assuming n>2)
when substitution repeated i-1th times
= 2i * T(n-i) + (2i-1+ ... +21+20)*c
when i=n
= 2n * T(0) + (2n-1+ ... +21+20)*c
n 1
= 2 * c1 + ( 2 i)*c
n
i0
Successful Search:
Best-Case: item is in the first location of the array O(1)
Worst-Case: item is in the last location of the array O(n)
Average-Case: The number of key comparisons 1, 2, ..., n
n
i (n n) / 2
2
i 1
O(n)
n n
n O(log2n)
16 4
64 6
256 8
1024 (1KB) 10
16,384 14
131,072 17
262,144 18
524,288 19
1,048,576 (1MB) 20
1,073,741,824 (1GB) 30