(Hopcroft [1981]) Suppose that L is a collection of bit strings of length n. Suppose that A is an algorithm which determines, given a bit string of length n, whether or not it is in L. Suppose that A always takes 2n seconds to provide an answer. Then A has the same worst-case and average-case computational complexity, 2n. Suppose that ˆL consists of all bit strings of the form
x1x2...xnx1x2...xn,
where x1x2...xn is in L. For instance, if L={00,10}, then ˆL={0000,1010}. Consider the following Algorithm B for determining, given a bit string y=y1y2...y2n of length 2n, whether or not it is in ˆL. First, determine if y is of the form x1x2...xnx1x2...xn. This is easy to check. Assume for the sake of discussion that it takes essentially 0 seconds to answer this question. If y is not of the proper form, stop and say that y is not in ˆL. If y is of the proper form, check if the first n digits of y form a bit string in L.
- Compute the worst-case complexity of Algorithm B.
It is when the string is in ˆL when the worst case happens and its complexity is equal to 2n (assuming the subalgorithm of B used to check whether the first n digits are in L is A)
- Compute the average-case complexity of Algorithm B.
There are a total of 22n bit strings of length 2n. There are 2n strings in the proper form and 22n−2n strings not in the proper form.
We can compute the average case complexity as the sum of the product of the number of properly formed strings times the (constant) complexity of A and the product of the number of strings not in the proper form times 0 (also constant), divided by total number of all possible strings:
2n⋅2n+(22n−2n)⋅022n=22n22n=1
- Do your answers suggest that average-case complexity might not be a good measure? Why?
The average case complexity is a constant. This is because there are many more strings not in the proper form, which take 0 seconds to check, bringing the average way down. This can be misleading if stated as such, without giving details on how it was calculated, as it is very different than the average-case complexity of the subalgorithm (A). Also, if most of the input strings are in ˆL, then the complexity will be not a constant but 2n.