# 随机算法 (Fall 2011)/Median Selection

The selection problem is the problem of finding the ${\displaystyle k}$th smallest element in a set ${\displaystyle S}$. A typical case of selection problem is finding the median.

 Definition The median of a set ${\displaystyle S}$ is the ${\displaystyle (\lceil n/2\rceil )}$th element in the sorted order of ${\displaystyle S}$.

The median can be found in ${\displaystyle O(n\log n)}$ time by sorting. There is a linear-time deterministic algorithm, "median of medians" algorithm, which is quite sophisticated. Here we introduce a much simpler randomized algorithm which also runs in linear time.

# Randomized median selection algorithm

We introduce a randomized median selection algorithm called LazySelect, which is a variant on a randomized algorithm due to Floyd and Rivest

The idea of this algorithm is random sampling. For a set ${\displaystyle S}$, let ${\displaystyle m\in S}$ denote the median. We observe that if we can find two elements ${\displaystyle d,u\in S}$ satisfying the following properties:

1. The median is between ${\displaystyle d}$ and ${\displaystyle u}$ in the sorted order, i.e. ${\displaystyle d\leq m\leq u}$;
2. The total number of elements between ${\displaystyle d}$ and ${\displaystyle u}$ is small, specially for ${\displaystyle C=\{x\in S\mid d\leq x\leq u\}}$, ${\displaystyle |C|=o(n/\log n)}$.

Provided ${\displaystyle d}$ and ${\displaystyle u}$ with these two properties, within linear time, we can compute the ranks of ${\displaystyle d}$ in ${\displaystyle S}$, construct ${\displaystyle C}$, and sort ${\displaystyle C}$. Therefore, the median ${\displaystyle m}$ of ${\displaystyle S}$ can be picked from ${\displaystyle C}$ in linear time.

So how can we select such elements ${\displaystyle d}$ and ${\displaystyle u}$ from ${\displaystyle S}$? Certainly sorting ${\displaystyle S}$ would give us the elements, but isn't that exactly what we want to avoid in the first place?

Observe that ${\displaystyle d}$ and ${\displaystyle u}$ are only asked to roughly satisfy some constraints. This hints us maybe we can construct a sketch of ${\displaystyle S}$ which is small enough to sort cheaply and roughly represents ${\displaystyle S}$, and then pick ${\displaystyle d}$ and ${\displaystyle u}$ from this sketch. We construct the sketch by randomly sampling a relatively small number of elements from ${\displaystyle S}$. Then the strategy of algorithm is outlined by:

• Sample a set ${\displaystyle R}$ of elements from ${\displaystyle S}$.
• Sort ${\displaystyle R}$ and choose ${\displaystyle d}$ and ${\displaystyle u}$ somewhere around the median of ${\displaystyle R}$.
• If ${\displaystyle d}$ and ${\displaystyle u}$ have the desirable properties, we can compute the median in linear time, or otherwise the algorithm fails.

The parameters to be fixed are: the size of ${\displaystyle R}$ (small enough to sort in linear time and large enough to contain sufficient information of ${\displaystyle S}$); and the order of ${\displaystyle d}$ and ${\displaystyle u}$ in ${\displaystyle R}$ (not too close to have ${\displaystyle m}$ between them, and not too far away to have ${\displaystyle C}$ sortable in linear time).

We choose the size of ${\displaystyle R}$ as ${\displaystyle n^{3/4}}$, and ${\displaystyle d}$ and ${\displaystyle u}$ are within ${\displaystyle {\sqrt {n}}}$ range around the median of ${\displaystyle R}$.

 LazySelect Input: a set ${\displaystyle S}$ of ${\displaystyle n}$ elements over totally ordered domain. Pick a multi-set ${\displaystyle R}$ of ${\displaystyle \left\lceil n^{3/4}\right\rceil }$ elements in ${\displaystyle S}$, chosen independently and uniformly at random with replacement, and sort ${\displaystyle R}$. Let ${\displaystyle d}$ be the ${\displaystyle \left\lfloor {\frac {1}{2}}n^{3/4}-{\sqrt {n}}\right\rfloor }$-th smallest element in ${\displaystyle R}$, and let ${\displaystyle u}$ be the ${\displaystyle \left\lceil {\frac {1}{2}}n^{3/4}+{\sqrt {n}}\right\rceil }$-th smallest element in ${\displaystyle R}$. Construct ${\displaystyle C=\{x\in S\mid d\leq x\leq u\}}$ and compute the ranks ${\displaystyle r_{d}=|\{x\in S\mid x and ${\displaystyle r_{u}=|\{x\in S\mid x. If ${\displaystyle r_{d}>{\frac {n}{2}}}$ or ${\displaystyle r_{u}<{\frac {n}{2}}}$ or ${\displaystyle |C|>4n^{3/4}}$ then return FAIL. Sort ${\displaystyle C}$ and return the ${\displaystyle \left(\left\lfloor {\frac {n}{2}}\right\rfloor -r_{d}+1\right)}$th element in the sorted order of ${\displaystyle C}$.

"Sample with replacement" (有放回采样) means that after sampling an element, we put the element back to the set. In this way, each sampled element is independently and identically distributed (i.i.d) (独立同分布). In the above algorithm, this is for our convenience of analysis.

# Analysis

The algorithm always terminates in linear time because each line of the algorithm costs at most linear time. The last three line guarantees that the algorithm returns the correct median if it does not fail.

We then only need to bound the probability that the algorithm returns a FAIL. Let ${\displaystyle m\in S}$ be the median of ${\displaystyle S}$. By Line 4, we know that the algorithm returns a FAIL if and only if at least one of the following events occurs:

• ${\displaystyle {\mathcal {E}}_{1}:Y=|\{x\in R\mid x\leq m\}|<{\frac {1}{2}}n^{3/4}-{\sqrt {n}}}$;
• ${\displaystyle {\mathcal {E}}_{2}:Z=|\{x\in R\mid x\geq m\}|<{\frac {1}{2}}n^{3/4}-{\sqrt {n}}}$;
• ${\displaystyle {\mathcal {E}}_{3}:|C|>4n^{3/4}}$.

${\displaystyle {\mathcal {E}}_{3}}$ directly follows the third condition in Line 4. ${\displaystyle {\mathcal {E}}_{1}}$ and ${\displaystyle {\mathcal {E}}_{2}}$ are a bit tricky. The first condition in Line 4 is that ${\displaystyle r_{d}>{\frac {n}{2}}}$, which looks not exactly the same as ${\displaystyle {\mathcal {E}}_{1}}$, but both ${\displaystyle {\mathcal {E}}_{1}}$ and that ${\displaystyle r_{d}>{\frac {n}{2}}}$ are equivalent to the same event: the ${\displaystyle \left\lfloor {\frac {1}{2}}n^{3/4}-{\sqrt {n}}\right\rfloor }$-th smallest element in ${\displaystyle R}$ is greater than ${\displaystyle m}$, thus they are actually equivalent. Similarly, ${\displaystyle {\mathcal {E}}_{2}}$ is equivalent to the second condition of Line 4.

We now bound the probabilities of these events one by one.

 Lemma 1 ${\displaystyle \Pr[{\mathcal {E}}_{1}]\leq {\frac {1}{4}}n^{-1/4}}$.
Proof.
 Let ${\displaystyle X_{i}}$ be the ${\displaystyle i}$th sampled element in Line 1 of the algorithm. Let ${\displaystyle Y_{i}}$ be a indicator random variable such that ${\displaystyle Y_{i}={\begin{cases}1&{\mbox{if }}X_{i}\leq m,\\0&{\mbox{otherwise.}}\end{cases}}}$ It is obvious that ${\displaystyle Y=\sum _{i=1}^{n^{3/4}}Y_{i}}$, where ${\displaystyle Y}$ is as defined in ${\displaystyle {\mathcal {E}}_{1}}$. For every ${\displaystyle X_{i}}$, there are ${\displaystyle \left\lceil {\frac {n}{2}}\right\rceil }$ elements in ${\displaystyle S}$ that are less than or equal to the median. The probability that ${\displaystyle Y_{i}=1}$ is ${\displaystyle p=\Pr[Y_{i}=1]=\Pr[X_{i}\leq m]={\frac {1}{n}}\left\lceil {\frac {n}{2}}\right\rceil ,}$ which is within the range of ${\displaystyle \left[{\frac {1}{2}},{\frac {1}{2}}+{\frac {1}{2n}}\right]}$. Thus ${\displaystyle \mathbf {E} [Y]=n^{3/4}p\geq {\frac {1}{2}}n^{3/4}.}$ The event ${\displaystyle {\mathcal {E}}_{1}}$ is defined as that ${\displaystyle Y<{\frac {1}{2}}n^{3/4}-{\sqrt {n}}}$. Note that ${\displaystyle Y_{i}}$'s are Bernoulli trials, and ${\displaystyle Y}$ is the sum of ${\displaystyle n^{3/4}}$ Bernoulli trials, which follows binomial distribution with parameters ${\displaystyle n^{3/4}}$ and ${\displaystyle p}$. Thus, the variance is ${\displaystyle \mathbf {Var} [Y]=n^{3/4}p(1-p)\leq {\frac {1}{4}}n^{3/4}.}$ Applying Chebyshev's inequality, {\displaystyle {\begin{aligned}\Pr[{\mathcal {E}}_{1}]&=\Pr \left[Y<{\frac {1}{2}}n^{3/4}-{\sqrt {n}}\right]\\&\leq \Pr \left[|Y-\mathbf {E} [Y]|>{\sqrt {n}}\right]\\&\leq {\frac {\mathbf {Var} [Y]}{n}}\\&\leq {\frac {1}{4}}n^{-1/4}.\end{aligned}}}
${\displaystyle \square }$

By a similar analysis, we can obtain the following bound for the event ${\displaystyle {\mathcal {E}}_{2}}$.

 Lemma 2 ${\displaystyle \Pr[{\mathcal {E}}_{2}]\leq {\frac {1}{4}}n^{-1/4}}$.

We now bound the probability of the event ${\displaystyle {\mathcal {E}}_{3}}$.

 Lemma 3 ${\displaystyle \Pr[{\mathcal {E}}_{3}]\leq {\frac {1}{2}}n^{-1/4}}$.
Proof.
 The event ${\displaystyle {\mathcal {E}}_{3}}$ is defined as that ${\displaystyle |C|>4n^{3/4}}$, which by the Pigeonhole Principle, implies that at leas one of the following must be true: ${\displaystyle {\mathcal {E}}_{3}'}$: at least ${\displaystyle 2n^{3/4}}$ elements of ${\displaystyle C}$ is greater than ${\displaystyle m}$; ${\displaystyle {\mathcal {E}}_{3}''}$: at least ${\displaystyle 2n^{3/4}}$ elements of ${\displaystyle C}$ is smaller than ${\displaystyle m}$. We bound the probability that ${\displaystyle {\mathcal {E}}_{3}'}$ occurs; the second will have the same bound by symmetry. Recall that ${\displaystyle C}$ is the region in ${\displaystyle S}$ between ${\displaystyle d}$ and ${\displaystyle u}$. If there are at least ${\displaystyle 2n^{3/4}}$ elements of ${\displaystyle C}$ greater than the median ${\displaystyle m}$ of ${\displaystyle S}$, then the rank of ${\displaystyle u}$ in the sorted order of ${\displaystyle S}$ must be at least ${\displaystyle {\frac {1}{2}}n+2n^{3/4}}$ and thus ${\displaystyle R}$ has at least ${\displaystyle {\frac {1}{2}}n^{3/4}-{\sqrt {n}}}$ samples among the ${\displaystyle {\frac {1}{2}}n-2n^{3/4}}$ largest elements in ${\displaystyle S}$. Let ${\displaystyle X_{i}\in \{0,1\}}$ indicate whether the ${\displaystyle i}$th sample is among the ${\displaystyle {\frac {1}{2}}n-2n^{3/4}}$ largest elements in ${\displaystyle S}$. Let ${\displaystyle X=\sum _{i=1}^{n^{3/4}}X_{i}}$ be the number of samples in ${\displaystyle R}$ among the ${\displaystyle {\frac {1}{2}}n-2n^{3/4}}$ largest elements in ${\displaystyle S}$. It holds that ${\displaystyle p=\Pr[X_{i}=1]={\frac {{\frac {1}{2}}n-2n^{3/4}}{n}}={\frac {1}{2}}-2n^{-1/4}}$. ${\displaystyle X}$ is a binomial random variable with ${\displaystyle \mathbf {E} [X]=n^{3/4}p={\frac {1}{2}}n^{3/4}-2{\sqrt {n}},}$ and ${\displaystyle \mathbf {Var} [X]=n^{3/4}p(1-p)={\frac {1}{4}}n^{3/4}-4n^{1/4}<{\frac {1}{4}}n^{3/4}.}$ Applying Chebyshev's inequality, {\displaystyle {\begin{aligned}\Pr[{\mathcal {E}}_{3}']&=\Pr \left[X\geq {\frac {1}{2}}n^{3/4}-{\sqrt {n}}\right]\\&\leq \Pr \left[|X-\mathbf {E} [X]|\geq {\sqrt {n}}\right]\\&\leq {\frac {\mathbf {Var} [X]}{n}}\\&\leq {\frac {1}{4}}n^{-1/4}.\end{aligned}}} Symmetrically, we have that ${\displaystyle \Pr[{\mathcal {E}}_{3}'']\leq {\frac {1}{4}}n^{-1/4}}$. Applying the union bound ${\displaystyle \Pr[{\mathcal {E}}_{3}]\leq \Pr[{\mathcal {E}}_{3}']+\Pr[{\mathcal {E}}_{3}'']\leq {\frac {1}{2}}n^{-1/4}.}$
${\displaystyle \square }$

Combining the three bounds. Applying the union bound to them, the probability that the algorithm returns a FAIL is at most

${\displaystyle \Pr[{\mathcal {E}}_{1}]+\Pr[{\mathcal {E}}_{2}]+\Pr[{\mathcal {E}}_{3}]\leq n^{-1/4}.}$

Therefore the algorithm always terminates in linear time and returns the correct median with high probability.