Sub topic 15
Expected time when the algorithm makes random choices
This chapter assumes you know best, worst, and average case, and the idea of an expected value from the earlier chapters. You should also be comfortable with Python loops and random.
Where the randomness comes from
The average case averages over inputs. It needs an assumption such as "every ordering is equally likely", and if real inputs don't follow that assumption, the result doesn't apply.
A randomized algorithm moves the randomness inside. The input is fixed, and the algorithm flips its own coins. Its expected running time is the average cost over those coins, for one specific input. The guarantee then holds for every input, because the algorithm created the randomness and no assumption about the data is needed.
| Row | Average case | Expected time of a randomized algorithm |
|---|---|---|
| Random thing | The input | The algorithm's own choices |
| Needs an input distribution | Yes | No |
| Bound covers | Typical inputs, if the assumption holds | Every input |
A worked example: random probing
Suppose a list holds 100 items, and half of them are "a". We want any one "a". The algorithm probes random positions until it hits one.
import random
def find_a(items, rng):
probes = 0
while True:
probes += 1
if items[rng.randrange(len(items))] == "a":
return probes
rng = random.Random(1)
trials = 100_000
front = ["a"] * 50 + ["b"] * 50
alternating = ["a", "b"] * 50
for items in (front, alternating):
mean = sum(find_a(items, rng) for _ in range(trials)) / trials
print(round(mean, 2))Both lines print a value close to 2.0. The arrangement of the list does not matter, because each probe succeeds with probability p = 1/2 whatever the layout.
Deriving the expected count
Let X be the number of probes. The chance that the first success comes on probe k is (1 - p)^(k-1) · p. The expected value is the sum of k times that chance, which works out to 1 / p. With p = 1/2, E[X] = 2.
A shortcut gives the same result. Let E be the expected number of probes. The first probe succeeds with probability p and costs 1. Otherwise it fails, and we are back where we started:
E = 1 + (1 - p) · E
Solving gives E = 1 / p.
Reading the result correctly
- The expectation is over the algorithm's coin flips, so you can state it for any fixed input.
- A bad input cannot be chosen by an adversary to force the expected cost up, because the adversary doesn't know the coin flips.
- A single run can still cost much more than the expectation. Expected time describes the long-run mean of many runs, not what one run will do.
- If a randomized algorithm has a rare slow outcome, the worst case can stay large while the expected time stays small. The next chapter shows this with quicksort and a random pivot.
Exercise
A list contains a fraction p = 1/4 of items equal to "a". Using random probing as above, what is the expected number of probes to find an "a", and does it depend on where the "a" items are placed?
Show answer
The expected number of probes is 1 / p = 4. It does not depend on placement, because each probe succeeds with probability 1/4 regardless of the arrangement. The expectation is taken over the algorithm's random choices, not over inputs.
Exercise
Why does the expected time of a randomized algorithm not require an assumption about the input distribution, while the average case does?
Show answer
In the average case the randomness is in the input, so you must assume a distribution over inputs. In a randomized algorithm the randomness is in the algorithm's own choices, which the algorithm controls. The expectation is computed for each fixed input, so it holds for all inputs.