# 算法week4---algorithms-divide-conquer--part1(RSelect)

1. Randomized Selection
1.1 R Select (array A, length n, order statistic i)
(0): if n =1 return A[1]
(1):choose pivot p from A uniformly at random.
(2):partition A around p;
let j = new index of p
(3): if j=i, return p;
(4): if j > i, return RSelect (1st part of A, j-1, i)
(5):if j > i , return RSelect(1st part of A, j-1, i)
(6):if j < i: return RSelect(2nd part of A, n-j, i-j)
2. Running Time of RSelect
2.1 RSelect Theorem: for every input array of length n, the average running time of RSelect is $O\left(n\right)$$O(n)$.
2.2 Tracking Progress via Phases.
Note: RSelect uses $<=cn$$<= cn$ operations outside of the recursive call [for some constant c > 0]
Notation: RSelect is in phase j if current array size between $\left(\frac{3}{4}{\right)}^{j+1}n$$(\frac{3}{4})^{j+1}n$ and $\left(\frac{3}{4}{\right)}^{j}n$$(\frac{3}{4})^j n$.
${X}_{j}=$$X_j =$ numbers of recursive calls during phase j.
Note: running time of RSelect $<=\sum _{phasej}{X}_{j}\cdot c\cdot \left(\frac{3}{4}{\right)}^{j}n$$<=\sum_{phase j} X_j \cdot c \cdot (\frac{3}{4})^j n$
(1) $\left(\frac{3}{4}{\right)}^{j}n$$(\frac{3}{4})^jn$ : <= array size during phase $j$$j$
(2) $c\cdot \left(\frac{3}{4}{\right)}^{j}n$$c\cdot (\frac{3}{4})^jn$: work per phase j subproblem.
(3)${X}_{j}$$X_j$:of phase -j subproblems.
2.3 Proof II: Reduction to Coin Flipping
${X}_{j}$$X_j$ :# of recursive calls during phase $j$$j$
Note: if RSelect choose a pivot giving a $25\mathrm{%}-75\mathrm{%}$$25\%-75\%$ spltit (or better): then current phase ends!(new subarray length at most 75% of old length).
Recall: probability of $25\mathrm{%}-75\mathrm{%}$$25\%-75\%$split or better is $50\mathrm{%}$$50\%$.
So: $E\left[{x}_{j}\right]<=$$E[x_j] <=$expected number of times you need to flip a fair coin to get one “head”.
($heads\approx goodpivot,tails\approx badpivot$$heads \approx good pivot, tails \approx bad pivot$)
2.4 Proof III: Coin Flipping Analysis
Let $N=$$N=$ number of coin flips until you get heads
(a “geometric random varible”)
Note:$E\left[N\right]=1+\frac{1}{\alpha }\cdot E\left[N\right]$$E[N] = 1 + \frac{1}{\alpha} \cdot E[N]$
$1$$1$: 1st coin flip.
$\frac{1}{\alpha }$$\frac{1}{\alpha}$: probablity of tails
$E\left[N\right]:$$E[N]:$ # of further coin flips needed in this case.
Solution: $E\left[N\right]=\alpha$$E[N] = \alpha$ (Recall $E\left[{x}_{j}\right]<=E\left[N\right]$$E[x_j] <= E[N]$)
expected running time of RSelect:
$\begin{array}{}\text{(2)}& \le E\left[cn\sum _{phas{e}_{j}}\left(\frac{3}{4}{\right)}^{j}{X}_{j}\right]\phantom{\rule{0ex}{0ex}}\le cn\sum _{phasej}\left(\frac{3}{4}{\right)}^{j}E\left[{X}_{j}\right]\phantom{\rule{0ex}{0ex}}\le 2\cdot cn\sum _{phasej}\left(\frac{3}{4}{\right)}^{j}\phantom{\rule{0ex}{0ex}}\le 8\cdot cn=O\left(n\right)\end{array}$

其中： $E\left(ofcoinflipsN\right)=2$$E(of coin flips N)=2$
$\sum _{phasej}\left(\frac{3}{4}{\right)}^{j}\le \frac{1}{1-\frac{3}{4}}=4$$\sum_{phasej}(\frac{3}{4})^j \leq \frac{1}{1-\frac{3}{4}}=4$