Average Cost Needs an Input Distribution

An expected operation count depends on which inputs or outcomes are likely, so state the distribution before computing an average.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Use uniform match positions
  2. Include absent inputs separately
  3. Shift the probability mass without changing the extremes
  4. Decide which users or requests the model represents

Use uniform match positions

When one match is equally likely at any of four positions, a forward scan makes 1, 2, 3, or 4 comparisons with equal probability. The expectation is \(2.5\). Best and worst cases remain one and four under these assumptions.

Include absent inputs separately

If half of requests are absent and the other half use that distribution, the expectation becomes \(0.5\cdot4+0.5\cdot2.5=3.25\). Select representative testable inputs for absence as well as early success.[1]

Shift the probability mass without changing the extremes

A linear scan over four positions checks one entry for a hit at the first and four for a hit at the last. Suppose the first is used on three quarters of requests, and the last on one quarter. The average count is 1.75 despite the same best and worst costs. See expectation as a weighted average.

Position         1       4
Probability      .75     .25
Comparisons      1       4
Mean = .75×1 + .25×4 = 1.75

Decide which users or requests the model represents

Batch retries and popular records can make actual requests highly nonuniform. Measure the distribution or state it as a hypothetical model. Include absent requests when they are possible because they also cost a full scan.

References

  1. Jeff Erickson, Algorithms ↩