Analyzing Algorithms: CS-EE-310 Algorithms Analysis
Analyzing Algorithms: CS-EE-310 Algorithms Analysis
Basic principles
Random-access machine
(RAM) model
Random-access machine
(RAM) model
Random-access machine
(RAM) model
Analyzing an algorithms
running time
The time taken by an algorithm
depends on the input:
Analyzing an algorithms
running time
Input size depends on the problem
being considered:
Analyzing an algorithms
running time
Finally, the running time is the
number of primitive operations (steps or
pseudocode lines) executed on a
particular input.
Analyzing an algorithms
running time: Fundamentals
11
(cost of the j
j
th
statement) x
c s
j j
12
Running time
InsertionSort(A, n)
for j 2 to n
c1n
do {key A[j]
c2(n-1)
comment line
c3=0
i j - 1;
c4(n-1)
while (i > 0) and (A[i] > key)
c5 T
do {A[i+1] A[i]
c6(T-(n-1))
i i - 1
c7(T-(n-1))
A[i+1] key
c8(T-(n-1))
}
}
T = t2 + t3 + + tn where tj is number of while expression evaluations for the jth
for loop iteration
13
st
1
Insertion Sort
algorithm:
the Running time
n
T (n) = c1n + c2 (n 1) + c4 (n 1) + c5 t j +
j =2
+c6 ( t j 1) + c7 ( t j 1) + c8 ( t j 1)
n
=j 2 =j 2=j 2
14
st
1
Insertion Sort
algorithm:
the Running time. Best case
Best case -inner loop body never executed (the array is already sorted)
n
T (n) = c1n + c2 (n 1) + c4 (n 1) + c5 t j +
j =2
+ c6 ( t j 1) + c7 ( t j 1) + c8 ( t j 1) =
=j 2 =j 2=j 2
= c1n + c2 (n 1) + c4 (n 1) + c5 ( n 1)
T(n) is a linear function
15
st
1
Insertion Sort
algorithm:
the Running time. Worst case
Worst case -- inner loop body executed for all previous elements (the
array is initially sorted in the reverse order)
n
T (n) = c1n + c2 (n 1) + c4 (n 1) + c5 j +
j =2
+c6 ( j 1) + c7 ( j 1) + c8 ( j 1) =
=j 2 =j 2=j 2
n(n + 1)
= c1n + c2 (n 1) + c4 (n 1) + c5
1 +
2
n(n 1)
n(n 1)
n(n 1)
+ c6
+ c7
+ c8
2
2
2
T(n) is a quadratic function
16
Statement
Running time
InsertionSort(A, n)
for j 2 to n
c1n
do { key A[j]
c2(n-1)
comment line
c3=0
i j - 1;
c4(n-1)
while (i > 0) and (A[i] > key)
c5 T
do {A[i+1] A[i]
c6(T-(n-1))
i i - 1
c7(T-(n-1))
}
A[i+1] key
c8(n-1)
}
T = t2 + t3 + + tn where tj is number of while expression evaluations for the jth
for loop iteration
18
st-a
1
Insertion Sort
algorithm:
the Running time
n
T (n) = c1n + c2 (n 1) + c4 (n 1) + c5 t j +
j =2
+c6 ( t j 1) + c7 ( t j 1) + c8 (n 1)
n
=j 2=j 2
19
st-a
1
Insertion Sort
algorithm:
the Running time. Best case
Best case -inner loop body never executed (the array is already sorted)
n
T (n) = c1n + c2 (n 1) + c4 (n 1) + c5 t j +
j =2
+ c6 ( t j 1) + c7 ( t j 1) + c8 (n 1) =
=j 2=j 2
= c1n + c2 (n 1) + c4 (n 1) + c5 (n 1) + c8 (n 1)
T(n) is a linear function
20
st-a
1
Insertion Sort
algorithm:
the Running time. Worst case
Worst case -- inner loop body executed for all previous elements (the
array is initially sorted in the reverse order)
n
T (n) = c1n + c2 (n 1) + c4 (n 1) + c5 j +
j =2
+c6 ( j 1) + c7 ( j 1) + c8 (n 1) =
=j 2=j 2
n(n + 1)
= c1n + c2 (n 1) + c4 (n 1) + c5
1 +
2
n(n 1)
n(n 1)
+ c6
+ c7
+ c8 (n 1)
2
2
T(n) is a quadratic function
21
st-a
1
Insertion Sort
algorithm:
the Running time. Worst case
Worst case -- inner loop body executed for all previous elements (the
array is initially sorted in the reverse order)
n(n + 1)
T (n) = c1n + c2 (n 1) + c4 (n 1) + c5
1 +
2
n(n 1)
n(n 1)
+c6
+ c7
+ c8 (n 1) =
2
2
c5 c6 c7
c5 c6 c7 2
+ + n + c1 + c2 + c4 + + c8 n
2 2 2
2 2 2
( c2 + c4 + c5 + c8 )
T(n) is a quadratic function
22
Best case?
Worst case?
Average case?
Some of them?
All?
23
Running time
26
Algorithm Efficiency
Big-O Notation
28
f (n) = efficiency
29
Linear Loops
f ( n) = n
32
Logarithmic Loops
Divide loop
f (n) = log n
33
34
The outer loop in this example adds, while the inner loop multiplies
A total number of iterations in the linear logarithmic nested loop is
equal to the product of the numbers of iterations for the external and
inner loops, respectively (10log10 in our example).
For the linear logarithmic nested loop the efficiency is determined
by the following formula:
f (n) = n log n
35
f ( n) = n
36
The number of iterations of the inner loop depends on the outer loop.
It is equal to the sum of the first n members of an arithmetic
progression: n(n+1)/2
n +1
f ( n) = n
37
Big-O notation
Big-O notation
2
2
2
n +n
2
O ( f ( n) ) = O ( n 2 )
39
40
41