Analysis of Algorithms

advertisement
Analysis of Algorithms
Lecture 2
Basic Operations
A basic operation is an operation whose execution time is bounded above by a constant.
We are only concerned with execution time of an algorithm defined within a
multiplicative constant, thus it is the number of basic operations executed that matters in
the analysis, and not the exact time required.
Thus,
Total number of basic operations  Total number of operations
(proportional)
In general, we must choose a basic operation(s) that "makes sense", that is, the algorithm
successful completion should depend on this operation(s).
For example, if we are trying to sort an array of integers, the only way we can sort this
array is by comparing elements of the array. Therefore, the "comparison" is the basic
operation:
A4:
Void Sort (int A[ ], int n) // A[ ] is an array and n is its size
{ int i,j;
for (i=0; i < n-1; i++) {
for (j=i+1; j < n;j++) {
(1) if (A[j] < A[i]) {
swap(A[i],A[j]);
}
}
}
In this case (1) (i.e., the if statement) is the basic operation. In this case, we have that for
each iteration of the inner loop, we have to count only one basic operation.
Outer loop
Inner loop
# of basic operations
i=0
j=1….n-1
(n-1)
i=1
j=2….n-1
(n-2)
..
………..
…….
i=n-2
j=n-1
1
______________________________________________
Total
(n2 - n)/2
Consider the problem that given an array of n integers and an integer x, we must
determine the position of the first occurrence of x in the array, and x does not appear, the
algorithm must return 0:
A5: int Find (int x, int A[ ], int n) // array of size n
{ int j;
for (j=0; j < n; j++) {
(1) if (x = = A[j]) {
return (j+1); //the position is 1 more than the index
}
}
return 0; // x is not an element of the array
}
Once again, the comparison (operation (1)) of x with an element of the array is the basic
operation. Please note that since there is only one basic operation, the total number of
basic operations is the number of iterations of the loop, or, equivalently, the index plus 1,
where x appear for the fist time in the array, thus the complexity depends on the input.
We can then consider the worst-case scenario, that is, x will appear in the last position
(n), or x does not appear at all. Thus, we say that the worst-case is n.
Average and Worst-case analysis
The amount of work done depends on the size of the input.
Problem
Size of the input
Find x in a list of integers (A5)
the number of integers in the array.
Sort an array of integers (A4)
the number of integers in the array.
A graph problem
the number of nodes and/or edges.
nodes
edges
Let Dn be the set of all inputs of size n (possible infinite). Let I be an input of size n of Dn
, and t(I) be the number of basic operations performed by the algorithm on input I.
I
Algorithm
t(I)
The worst-case W(n) is
W(n) = max {t(I) : I  Dn }
In other words, the worst-case is the maximum number of basic operation the algorithm
can perform for all the possible inputs. For example for algorithm A5, W(n)=n, an it is
obtained when we have an input composed of an array of n integers and integer x, where
x appears in the last position, or x does not appear at all.
Please note the difference between algorithms A4 (sort) and A5, since for A4, for any
input I, we will obtained the same complexity, thus W(n)= (n2 - n)/2.
Similarly, an obvious concept is the best-case B(n).
B(n) = min {t(I) : I  Dn }
The best-case scenario is the minimum number of basic operations the algorithm can
perform for all possible inputs. For A5, B(n) = 1, an it is obtained when we have an input
composed of an array of n integers and an integer x, and x appears in the first position,
thus we make only one comparison and we return 1.
As we note previously, for A4, independently of the input, the algorithm performs the
same amount of basic operations, thus B(n)=W(n)= (n2 - n)/2.
In general for any algorithm B(n)W(n). Also, as we will define next, the average-case
A(n) is the number of operations performed on the average, thus
B(n) A(n) W(n).
Clearly, we don't have to find the average-case for A4, since
B(n)=W(n), thus A(n) = (n2 - n)/2.
Download