Escolar Documentos
Profissional Documentos
Cultura Documentos
Algorithm
An
An
An
Once
Algorithmic Performance
There are two aspects of algorithmic performance:
Time
Instructions
How
take time.
What
Space
Data
What
How
Analysis of Algorithms
Analysis of Algorithms is the area of computer science that
provides tools to analyze the efficiency of different methods of
solutions.
How do we compare the time efficiency of two algorithms that
solve the same problem?
Nave Approach: implement these algorithms in a programming
language (C++), and run them to compare their time
requirements. Comparing the programs (instead of algorithms)
has difficulties.
Analysis of Algorithms
When
To analyze
algorithms:
First,
count = count + 1;
A sequence of operations:
count = count + 1;
Cost: c1
Cost: c2
Total Cost = c1 + c2
Dr. Fares Saab
Times
c1
absval = -n
1
c2
else
absval = n; c3
c1
sum = 0;
1
c2
while (i <= n) {
i = i + 1;
c4
sum = sum + i;
c3
n+1
n
c5
}
Total Cost = c1 + c2 + (n+1)*c3 + n*c4 + n*c5
The time required for this algorithm is proportional to n
Dr. Fares Saab
Times
i=1;
c1
sum = 0;
1
c2
while (i <= n) {
j=1;
c4
c3
n+1
while (j <= n) {
c5
n*(n+1)
sum = sum + i;
c6
n*n
j = j + 1;
c7
n*n
}
i = i +1;
c8
}
Total Cost = c1 + c2 + (n+1)*c3 + n*c4 + n*(n+1)*c5+n*n*c6+n*n*c7+n*c8
Dr. Fares
The Saab
time required for this algorithm is proportional9 to n 2
10
11
12
Figure 6.1
Running times for small inputs
14
Figure 6.2
Running times for moderate inputs
15
Since the capital O is used in the notation, this notation is called the Big O
notation.
16
Definition:
Algorithm A is order f(n) denoted as O(f(n))
if constants k and n0 exist such that A requires
no more than k*f(n) time units to solve a
problem
of size n n0.
17
Order of an Algorithm
for all n n0 .
18
19
A Comparison of Growth-Rate
Functions
20
21
Growth-Rate Functions
O(1)
O(log2n)
Time requirement for a linear algorithm increases directly with the size
of the problem.
O(n*log2n) Time requirement for a n*log2n algorithm increases more rapidly than
a linear algorithm.
O(n2)
O(n3)
Time requirement for a cubic algorithm increases more rapidly with the
size of the problem than the time requirement for a quadratic algorithm.
O(2n)
22
Growth-Rate Functions
T(n) = 1 second
O(n*log2n)
O(n2)
O(n3)
O(2n)
23
Properties of Growth-Rate
Functions
1.
2.
3.
24
Growth-Rate Functions
Example1
Cost
Times
i = 1; c1
sum = 0;
c2
while (i <= n) { c3
i = i + 1;
sum = sum + i;
c4
n+1
n
c5
}
T(n)
= (c3+c4+c5)*n + (c1+c2+c3)
= a*n + b
So, the growth-rate function for this algorithm is O(n)
Dr. Fares Saab
25
Growth-Rate Functions
Example2
Cost
Times
i=1;
c1
sum = 0;
1
c2
while (i <= n) {
j=1;
c4
c3
n+1
while (j <= n) {
c5
sum = sum + i;
j = j + 1;
n*(n+1)
c6
c7
n*n
n*n
}
i = i +1;
c8
}
T(n)
26
27
c1
c2
cout << "Move top disk from pole " << source
c3
c4
} }
To find the growth-rate function for a recursive algorithm, we have to solve its
recurrence relation.
Dr. Fares Saab
28
Growth-Rate Functions
Hanoi Towers
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
29
There are many methods to solve recurrence equations, but we will use a simple
method known as repeated substitutions.
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-1 th times
= 2i * T(n-i) + (2i-1+ ... +21+20)*c
when i=n
= 2n * T(0) + (2n-1+ ... +21+20)*c
= 2n * c1 + (
n 1
2
)*c
i
i 0
n
So, the growth rate
30 function is O(2 )
What to Analyze
We have to look at all possible data organizations of a given size n, and their
distribution probabilities of these organizations.
What is Important?
That is, there is no need for coding tricks if the gain is not
too much.
33
Sequential Search
int sequentialSearch(const int a[], int item, int n){
for (int i = 0; i < n && a[i]!= item; i++);
if (i == n)
return 1;
return i;
}
O(n)
Dr. Fares Saab
34
Binary Search
35
36
37
38
O(log2n)
16
64
256
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
39