# Stable Marriage

We now consider the famous stable marriage problem or stable matching problem (SMP). This problem captures two aspects: allocations (matchings) and stability, two central topics in economics.

An instance of stable marriage consists of:

• ${\displaystyle n}$ men and ${\displaystyle n}$ women;
• each person associated with a strictly ordered preference list containing all the members of the opposite sex.

Formally, let ${\displaystyle M}$ be the set of ${\displaystyle n}$ men and ${\displaystyle W}$ be the set of ${\displaystyle n}$ women. Each man ${\displaystyle m\in M}$ is associated with a permutation ${\displaystyle p_{m}}$ of elemets in ${\displaystyle W}$ and each woman ${\displaystyle w\in W}$ is associated with a permutation ${\displaystyle p_{w}}$ of elements in ${\displaystyle M}$.

A matching is a one-one correspondence ${\displaystyle \phi :M\rightarrow W}$. We said a man ${\displaystyle m}$ and a woman ${\displaystyle w}$ are partners in ${\displaystyle \phi }$ if ${\displaystyle w=\phi (m)}$.

 Definition (stable matching) A pair ${\displaystyle (m,w)}$ of a man and woman is a blocking pair in a matching ${\displaystyle \phi }$ if ${\displaystyle m}$ and ${\displaystyle w}$ are not partners in ${\displaystyle \phi }$ but ${\displaystyle m}$ prefers ${\displaystyle w}$ to ${\displaystyle \phi (m)}$, and ${\displaystyle w}$ prefers ${\displaystyle m}$ to ${\displaystyle \phi (w)}$. A matching ${\displaystyle \phi }$ is stable if there is no blocking pair in it.

It is unclear from the definition itself whether stable matchings always exist, and how to efficiently find a stable matching. Both questions are answered by the following proposal algorithm due to Gale and Shapley.

 The proposal algorithm (Gale-Shapley 1962) Initially, all person are not married; in each step (called a proposal): an arbitrary unmarried man ${\displaystyle m}$ proposes to the woman ${\displaystyle w}$ who is ranked highest in his preference list ${\displaystyle p_{m}}$ among all the women who has not yet rejected ${\displaystyle m}$; if ${\displaystyle w}$ is still single then ${\displaystyle w}$ accepts the proposal and is married to ${\displaystyle m}$; if ${\displaystyle w}$ is married to another man ${\displaystyle m'}$ who is ranked lower than ${\displaystyle m}$ in her preference list ${\displaystyle p_{w}}$ then ${\displaystyle w}$ divorces ${\displaystyle m'}$ (thus ${\displaystyle m'}$ becomes single again and considers himself as rejected by ${\displaystyle w}$) and is married to ${\displaystyle m}$; if otherwise ${\displaystyle w}$ rejects ${\displaystyle m}$;

The algorithm terminates when the last single woman receives a proposal. Since for every pair ${\displaystyle (m,w)\in M\times W}$ of man and woman, ${\displaystyle m}$ proposes to ${\displaystyle w}$ at most once. The algorithm terminates in at most ${\displaystyle n^{2}}$ proposals in the worst case.

It is obvious to see that the algorithm retruns a macthing, and this matching must be stable. To see this, by contradiction suppose that the algorithm resturns a macthing ${\displaystyle \phi }$, such that two men ${\displaystyle A,B}$ are macthed to two women ${\displaystyle a,b}$ in ${\displaystyle \phi }$ respectively, but ${\displaystyle A}$ and ${\displaystyle b}$ prefers each other to their partners ${\displaystyle a}$ and ${\displaystyle B}$ respectively. By definition of the algorithm, ${\displaystyle A}$ would have proposed to ${\displaystyle b}$ before proposing to ${\displaystyle a}$, by which time ${\displaystyle b}$ must either be single or be matched to a man ranked lower than ${\displaystyle A}$ in her list (because her final partner ${\displaystyle B}$ is ranked lower than ${\displaystyle A}$), which means ${\displaystyle b}$ must have accepted ${\displaystyle A}$'s proposal, a contradiction.

We are interested in the average-case performance of this algorithm, that is, the expected number of proposals if everyone's preference list is a uniformly and independently random permutation.

The following principle of deferred decisions is quite useful in analysing performance of algorithm with random input.

 Principle of deferred decisions The decision of random choice in the random input can be deferred to the running time of the algorithm.

Apply the principle of deferred decisions, the deterministic proposal algorithm with random permutations as input is equivalent to the following random process:

• At each step, a man ${\displaystyle m}$ choose a woman ${\displaystyle w}$ uniformly and independently at random to propose, among all the women who have not rejected him yet. (sample without replacement)

We then compare the above process with the following modified process:

• The man ${\displaystyle m}$ repeatedly samples a uniform and independent woman to propose among all women, until he successfully samples a woman who has not rejected him and propose to her. (sample with replacement)

It is easy to see that the modified process (sample with replacement) is no more efficient than the original process (sample without replacement) because it simulates the original process if at each step we only count the last proposal to the woman who has not rejected the man. Such comparison of two random processes by forcing them to be related in some way is called coupling.

Note that in the modified process (sample with replacement), each proposal, no matter from which man, is going to a uniformly and independently random women. And we know that the algorithm terminated once the last single woman receives a proposal, i.e. once all ${\displaystyle n}$ women have received at least one proposal. This is the coupon collector problem with proposals as balls (cookie boxes) and women as bins (coupons). Due to our analysis of the coupon collector problem, the expected number of proposals is bounded by ${\displaystyle O(n\ln n)}$.

# Tail Inequalities

When applying probabilistic analysis, we often want a bound in form of ${\displaystyle \Pr[X\geq t]<\epsilon }$ for some random variable ${\displaystyle X}$ (think that ${\displaystyle X}$ is a cost such as running time of a randomized algorithm). We call this a tail bound, or a tail inequality.

Besides directly computing the probability ${\displaystyle \Pr[X\geq t]}$, we want to have some general way of estimating tail probabilities from some measurable information regarding the random variables.

## Markov's Inequality

One of the most natural information about a random variable is its expectation, which is the first moment of the random variable. Markov's inequality draws a tail bound for a random variable from its expectation.

 Theorem (Markov's Inequality) Let ${\displaystyle X}$ be a random variable assuming only nonnegative values. Then, for all ${\displaystyle t>0}$, {\displaystyle {\begin{aligned}\Pr[X\geq t]\leq {\frac {\mathbf {E} [X]}{t}}.\end{aligned}}}
Proof.
 Let ${\displaystyle Y}$ be the indicator such that {\displaystyle {\begin{aligned}Y&={\begin{cases}1&{\mbox{if }}X\geq t,\\0&{\mbox{otherwise.}}\end{cases}}\end{aligned}}} It holds that ${\displaystyle Y\leq {\frac {X}{t}}}$. Since ${\displaystyle Y}$ is 0-1 valued, ${\displaystyle \mathbf {E} [Y]=\Pr[Y=1]=\Pr[X\geq t]}$. Therefore, ${\displaystyle \Pr[X\geq t]=\mathbf {E} [Y]\leq \mathbf {E} \left[{\frac {X}{t}}\right]={\frac {\mathbf {E} [X]}{t}}.}$
${\displaystyle \square }$

### Example (from Las Vegas to Monte Carlo)

Let ${\displaystyle A}$ be a Las Vegas randomized algorithm for a decision problem ${\displaystyle f}$, whose expected running time is within ${\displaystyle T(n)}$ on any input of size ${\displaystyle n}$. We transform ${\displaystyle A}$ to a Monte Carlo randomized algorithm ${\displaystyle B}$ with bounded one-sided error as follows:

${\displaystyle B(x)}$:
• Run ${\displaystyle A(x)}$ for ${\displaystyle 2T(n)}$ long where ${\displaystyle n}$ is the size of ${\displaystyle x}$.
• If ${\displaystyle A(x)}$ returned within ${\displaystyle 2T(n)}$ time, then return what ${\displaystyle A(x)}$ just returned, else return 1.

Since ${\displaystyle A}$ is Las Vegas, its output is always correct, thus ${\displaystyle B(x)}$ only errs when it returns 1, thus the error is one-sided. The error probability is bounded by the probability that ${\displaystyle A(x)}$ runs longer than ${\displaystyle 2T(n)}$. Since the expected running time of ${\displaystyle A(x)}$ is at most ${\displaystyle T(n)}$, due to Markov's inequality,

${\displaystyle \Pr[{\mbox{the running time of }}A(x)\geq 2T(n)]\leq {\frac {\mathbf {E} [{\mbox{running time of }}A(x)]}{2T(n)}}\leq {\frac {1}{2}},}$

thus the error probability is bounded.

### Generalization

For any random variable ${\displaystyle X}$, for an arbitrary non-negative real function ${\displaystyle h}$, the ${\displaystyle h(X)}$ is a non-negative random variable. Applying Markov's inequality, we directly have that

${\displaystyle \Pr[h(X)\geq t]\leq {\frac {\mathbf {E} [h(X)]}{t}}.}$

This trivial application of Markov's inequality gives us a powerful tool for proving tail inequalities. With the function ${\displaystyle h}$ which extracts more information about the random variable, we can prove sharper tail inequalities.

## Variance

 Definition (variance) The variance of a random variable ${\displaystyle X}$ is defined as {\displaystyle {\begin{aligned}\mathbf {Var} [X]=\mathbf {E} \left[(X-\mathbf {E} [X])^{2}\right]=\mathbf {E} \left[X^{2}\right]-(\mathbf {E} [X])^{2}.\end{aligned}}} The standard deviation of random variable ${\displaystyle X}$ is ${\displaystyle \delta [X]={\sqrt {\mathbf {Var} [X]}}.}$

We have seen that due to the linearity of expectations, the expectation of the sum of variable is the sum of the expectations of the variables. It is natural to ask whether this is true for variances. We find that the variance of sum has an extra term called covariance.

 Definition (covariance) The covariance of two random variables ${\displaystyle X}$ and ${\displaystyle Y}$ is {\displaystyle {\begin{aligned}\mathbf {Cov} (X,Y)=\mathbf {E} \left[(X-\mathbf {E} [X])(Y-\mathbf {E} [Y])\right].\end{aligned}}}

We have the following theorem for the variance of sum.

 Theorem For any two random variables ${\displaystyle X}$ and ${\displaystyle Y}$, {\displaystyle {\begin{aligned}\mathbf {Var} [X+Y]=\mathbf {Var} [X]+\mathbf {Var} [Y]+2\mathbf {Cov} (X,Y).\end{aligned}}} Generally, for any random variables ${\displaystyle X_{1},X_{2},\ldots ,X_{n}}$, {\displaystyle {\begin{aligned}\mathbf {Var} \left[\sum _{i=1}^{n}X_{i}\right]=\sum _{i=1}^{n}\mathbf {Var} [X_{i}]+\sum _{i\neq j}\mathbf {Cov} (X_{i},X_{j}).\end{aligned}}}
Proof.
 The equation for two variables is directly due to the definition of variance and covariance. The equation for ${\displaystyle n}$ variables can be deduced from the equation for two variables.
${\displaystyle \square }$

We will see that when random variables are independent, the variance of sum is equal to the sum of variances. To prove this, we first establish a very useful result regarding the expectation of multiplicity.

 Theorem For any two independent random variables ${\displaystyle X}$ and ${\displaystyle Y}$, {\displaystyle {\begin{aligned}\mathbf {E} [X\cdot Y]=\mathbf {E} [X]\cdot \mathbf {E} [Y].\end{aligned}}}
Proof.
 {\displaystyle {\begin{aligned}\mathbf {E} [X\cdot Y]&=\sum _{x,y}xy\Pr[X=x\wedge Y=y]\\&=\sum _{x,y}xy\Pr[X=x]\Pr[Y=y]\\&=\sum _{x}x\Pr[X=x]\sum _{y}y\Pr[Y=y]\\&=\mathbf {E} [X]\cdot \mathbf {E} [Y].\end{aligned}}}
${\displaystyle \square }$

With the above theorem, we can show that the covariance of two independent variables is always zero.

 Theorem For any two independent random variables ${\displaystyle X}$ and ${\displaystyle Y}$, {\displaystyle {\begin{aligned}\mathbf {Cov} (X,Y)=0.\end{aligned}}}
Proof.
 {\displaystyle {\begin{aligned}\mathbf {Cov} (X,Y)&=\mathbf {E} \left[(X-\mathbf {E} [X])(Y-\mathbf {E} [Y])\right]\\&=\mathbf {E} \left[X-\mathbf {E} [X]\right]\mathbf {E} \left[Y-\mathbf {E} [Y]\right]&\qquad ({\mbox{Independence}})\\&=0.\end{aligned}}}
${\displaystyle \square }$

We then have the following theorem for the variance of the sum of pairwise independent random variables.

 Theorem For pairwise independent random variables ${\displaystyle X_{1},X_{2},\ldots ,X_{n}}$, {\displaystyle {\begin{aligned}\mathbf {Var} \left[\sum _{i=1}^{n}X_{i}\right]=\sum _{i=1}^{n}\mathbf {Var} [X_{i}].\end{aligned}}}
Remark
The theorem holds for pairwise independent random variables, a much weaker independence requirement than the mutual independence. This makes the variance-based probability tools work even for weakly random cases. We will see what it exactly means in the future lectures.

### Variance of binomial distribution

For a Bernoulli trial with parameter ${\displaystyle p}$.

${\displaystyle X={\begin{cases}1&{\mbox{with probability }}p\\0&{\mbox{with probability }}1-p\end{cases}}}$

The variance is

${\displaystyle \mathbf {Var} [X]=\mathbf {E} [X^{2}]-(\mathbf {E} [X])^{2}=\mathbf {E} [X]-(\mathbf {E} [X])^{2}=p-p^{2}=p(1-p).}$

Let ${\displaystyle Y}$ be a binomial random variable with parameter ${\displaystyle n}$ and ${\displaystyle p}$, i.e. ${\displaystyle Y=\sum _{i=1}^{n}Y_{i}}$, where ${\displaystyle Y_{i}}$'s are i.i.d. Bernoulli trials with parameter ${\displaystyle p}$. The variance is

{\displaystyle {\begin{aligned}\mathbf {Var} [Y]&=\mathbf {Var} \left[\sum _{i=1}^{n}Y_{i}\right]\\&=\sum _{i=1}^{n}\mathbf {Var} \left[Y_{i}\right]&\qquad ({\mbox{Independence}})\\&=\sum _{i=1}^{n}p(1-p)&\qquad ({\mbox{Bernoulli}})\\&=p(1-p)n.\end{aligned}}}

## Chebyshev's inequality

With the information of the expectation and variance of a random variable, one can derive a stronger tail bound known as Chebyshev's Inequality.

 Theorem (Chebyshev's Inequality) For any ${\displaystyle t>0}$, {\displaystyle {\begin{aligned}\Pr \left[|X-\mathbf {E} [X]|\geq t\right]\leq {\frac {\mathbf {Var} [X]}{t^{2}}}.\end{aligned}}}
Proof.
 Observe that ${\displaystyle \Pr[|X-\mathbf {E} [X]|\geq t]=\Pr[(X-\mathbf {E} [X])^{2}\geq t^{2}].}$ Since ${\displaystyle (X-\mathbf {E} [X])^{2}}$ is a nonnegative random variable, we can apply Markov's inequality, such that ${\displaystyle \Pr[(X-\mathbf {E} [X])^{2}\geq t^{2}]\leq {\frac {\mathbf {E} [(X-\mathbf {E} [X])^{2}]}{t^{2}}}={\frac {\mathbf {Var} [X]}{t^{2}}}.}$
${\displaystyle \square }$

# 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.

## The LazySelect 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.