5-2 Searching an unsorted array
The problem examines three algorithms for searching for a value in an unsorted array consisting for elements.
Consider the following randomized strategy: pick a random index into . If , then we terminate; otherwise, we continue the search by picking a new random index into . We continue picking random indices into until we find an index such that or until we have checked every element of . Note that we pick from the whole set of indices each time, so that we may examine a given element more than once.
a. Write pseudocode for a procedure to implement the strategy above. Be sure that your algorithm terminates when all indices into have been picked.
b. Suppose that there is exactly one index such that . What is the expected number of indices into that we must pick before we find and terminates?
c. Generalizing your solution to part (b), suppose that there are indices such that . What is the expected number of indices into that we must pick before we find and terminates? Your answer should be a function of and .
d. Suppose that there are no indices such that . What is the expected number of indices into that we must pick before we have checked all elements of and terminates?
Now consider a deterministic linear search algorithm, which we refer to as . Specifically, the algorithm searches for in order, considering until either it finds or it reaches the end of the array. Assume that possible permutations of the input array are equally likely.
e. Suppose that there is exactly one index such that . What is the average-case running time of ? What is the worst-case running time of ?
f. Generalizing your solution to part (e), suppose that there are indices such that . What is the average-case running time of ? What is the worst-case running time of ? Your answer should be a function of and .
g. Suppose that there are no indices such that . What is the average-case running time of ? What is the worst-case running time of ?
Finally, consider a randomized algorithm that works by first randomly permuting the input array and then running the deterministic linear search given above on the resulting permuting array.
h. Letting be the number of indices such that , give the worst-case and expected running time of for the cases in which and . Generalizing your solution to handle the case in which .
i. Which of the three searching algorithms would you use? Explain your answer.
a.
RANDOM-SEARCH(x, A, n)
v = Ø
while |Ø| != n
i = RANDOM(1, n)
if A[i] = x
return i
else
v = v ∩ i
return NIL
can be implemented in multiple ways: a hash table, a tree or a bitmap. The last one would probabily perform best and consume the least space.
b. is well-modelled by Bernoulli trials. The expected number of picks is .
c. In similar fashion, the expected number of picks is .
d. This is modelled by the balls and bins problem, explored in section 5.4.2. The answer is .
e. The worst-case running time is . The average-case is (obviously).
f. The worst-case running time is . The average-case running time is . Let be an indicator random variable that the th element is a match. . Let be an indicator random variable that we have found a match after the first elements (). Thus,
g. Both the worst-case and average case is .
h. It's the same as , only we replace "average-case" with "expected".
i. Definitelly . gives better expected results, but for the cost of randomly permuting the array, which is a linear operation. In the same time we could have scanned the full array and reported a result.