Monday, December 30, 2013
Ex 3.20 - AofA
Solve the recurrence
|
an=3an−1−3an−2+an−3 for n>2 with a0=a1=0 and a2=1 |
|
an+3=3an+2−3an+1+ann to n+3∑n≥0an+3zn=∑n≥03an+2zn−∑n≥03an+1zn+∑n≥0anzn multiply by zn and sum over all n1z3∑n≥3anzn=1z2∑n≥23anzn−1z∑n≥13anzn+∑n≥0anznre-index1z3(∑n≥0anzn−z2)=3z2∑n≥0anzn−3z∑n≥0anzn+∑n≥0anzn1z3(a(z)−z2)=3z2a(z)−3za(z)+a(z)a(z)(1z3−3z2+3z−1)=1za(z)1−3z+3z2−z3z3=1za(z)=z2(1−z)3[zn]a(z)≡an=(n2)OGF table lookkup |
◻
Solve the same recurrence with the initial condition on a1 changed to a1=1.
|
1z3∑n≥3anzn=1z2∑n≥23anzn−1z∑n≥13anzn+∑n≥0anzn1z3(∑n≥0anzn−z−z2)=3z2(∑n≥0anzn−z)−3z∑n≥0anzn+∑n≥0anzn1z3(a(z)−z−z2)=3z2(a(z)−z)−3za(z)+a(z)a(z)(1z3−3z2+3z−1)=1z2−2za(z)(1−3z+3z2−z3)=z−2z2a(z)=z(1−z)3−2z2(1−z)3=z∑n≥0(n+22)zn−2∑n≥2(n2)znOGF expansion=∑n≥0(n+22)zn+1−2∑n≥2(n2)zn=∑n≥1(n+12)zn−2∑n≥2(n2)zn=(z+(32)z2+∑n≥3(n+12)zn)−2(z2+∑n≥3(n2)zn)=z+z2+∑n≥3((n+12)−2(n2))zn[zn]a(z)≡an=(n+12)−2(n2)for n≥3 |
◻
Source: Analysis of Algorithm
Sunday, December 29, 2013
Fibonacci via OGF
|
Fn+2=Fn+1+Fn where F0=0 and F1=1∑n≥0Fn+2zn=∑n≥0Fn+1zn+∑n≥0Fnznmultiply by zn and sum over all n1z2∑n≥2Fnzn=1z∑n≥1Fnzn+∑n≥0Fnznre-index1z2(∑n≥0Fnzn)−F1z+F0z0z2=1z∑n≥0Fnzn−F0z0z+∑n≥0FnznF(z)z2−zz2=1zF(z)+F(z)F(z)−z=zF(z)+z2F(z)F(z)=z1−z−z2OGF of Fibonacci sequence! |
Observe
|
1−z−z2=−(z+√5+12)(z−√5−12)=(√5+12+z)(√5−12−z) |
Let
So
|
z1−z−z2=z(φ+z)(ˆφ−z)A(φ+z)+B(ˆφ−z)=z(φ+z)(ˆφ−z)partial fractionsA(ˆφ−z)+B(φ+z)=zB(φ+ˆφ)=ˆφB=ˆφφ+ˆφ=√5−12√5=1√5ˆφA(ˆφ+φ)=−φA=−φˆφ+φ=−√5+12√5=−1√5φ |
Therefore
|
F(z)=z1−z−z2=−φ√5(φ+z)+ˆφ√5(ˆφ−z)=−1√5(1−(−zφ))+1√5(1−zˆφ)=1√5(1−zˆφ)−1√5(1−(−zφ))=1√5∑N≥0(ˆφ−1z)N−1√5∑N≥0(−φ−1z)Nexpand OGF=1√5∑N≥0ˆφ−NzN+1√5∑N≥0(−1)N+1φ−NzN=∑N≥01√5(ˆφ−N+(−1)N+1φ−N)zNFN≡[zN]F(z)=1√5(ˆφ−N+(−1)N+1φ−N)=1√5((2√5−1)N+(−1)N+1(2√5+1)N)=1√5((2(√5+1)4)N+(−1)N+1(2(√5−1)4)N)=1√5((2(√5+1)4)N+(−1)2N+1(2(1−√5)4)N)FN=1√5((1+√52)N−(1−√52)N) |
◻
Source: Analysis of Algorithm
Counting Leaves in random binary tree
T: | set of all binary trees. |
|t|: | the number of internal nodes of binary tree t. |
leaves(t): | number of leaves in t. |
TN: | number of binary tree of size N. |
CN: | total number of leaves in all binary trees of size N. |
We saw earlier that
|
T(z)=∑t∈Tz|t|=∑N≥0TNzN=12z(1−√1−4z)TN=1N+1(2NN) |
The key is to find the cumulative GF (generating function),
C(z), for the leaves:
|
C(z)=∑t∈Tleaves(t)z|t|=z+∑tL∈T∑tR∈T(leaves(tL)+leaves(tR))z|tL|+|tR|+1=z+z∑tL∈Tleaves(tL)z|tL|∑tR∈Tz|tR|+z∑tR∈Tleaves(tR)z|tR|∑tL∈Tz|tL|=z+2zC(z)T(z)C(z)=z1−2zT(z)=z1−2z12z(1−√1−4z)=z√1−4z=z∑N≥0(−12N)(−4z)Nbinomial theorem=z∑N≥1(−12N−1)(−4z)N−1N to N−1=∑N≥1(−12N−1)(−4)N−1zN∴ |
Therefore
|
\begin{aligned}
\frac{C_N}{T_N} &= {{2N-2 \choose N-1} \over \frac{1}{N+1} {2N \choose N}} \\
&= {\frac{(2N-2)!}{(N-1)!(2N-2-N+1)!} \over \frac{1}{N+1} \frac{(2N)!}{N!N!}} \\
&= \frac{(2N-2)!}{(N-1)!(N-1)!} \frac{(N+1)N!N!}{(2N)!} \\
&= \frac{N^2(N+1)}{2N(2N-1)}\\
&= \frac{N(N+1)}{2(2N-1)} \\
\frac{C_N}{T_N} &\thicksim \frac{N}{4} \\
\end{aligned}
|
\Box
Source: Analysis of Algorithm
Saturday, December 28, 2013
EGFs
Simplify
|
\begin{aligned}
f_n &= \sum_k {n \choose k} \frac{f_k}{2^k} \\
\end{aligned}
|
This example showcases the power of EGF,
Exponential Generating Functions.
|
\begin{aligned}
f(z) &= \sum_{n \ge 0} \sum_k {n \choose k} \frac{f_k}{2^k} \frac{z^n}{n!} & \text{multiply by } \frac{z_n}{n!} \text{ and sum over all }n \\
&= \sum_{k \ge 0} \sum_{n \ge k} {n \choose k} \frac{f_k}{2^k} \frac{z^n}{n!} & \text{reorder} \\
&= \sum_{k \ge 0} \sum_{n \ge 0} {n+k \choose k} \frac{f_k}{2^k} \frac{z^{n+k}}{(n+k)!} & \text{re-index }n \text{ to } n+k \\
&= \sum_{k \ge 0} \sum_{n \ge 0} \frac{f_k}{2^k} \frac{z^{n+k}}{n! k!} = \sum_{k \ge 0} \sum_{n \ge 0} f_k \frac{(z/2)^k}{k!} \frac{z^{n}}{n!} \\
&= \big(\sum_{k \ge 0} f_k \frac{(z/2)^k}{k!}\big) \big(\sum_{n \ge 0} \frac{z^{n}}{n!}\big) & \text{distribute} \\
&= f(z/2) e^z & \text{!}\\
\end{aligned}
|
Why ? Observe
|
\begin{aligned}
\sum_{k \ge 0} f_k \frac{(z/2)^k}{k!} &= \sum_{n \ge 0} f_n \frac{(z/2)^n}{n!} \\
&= \sum_{n \ge 0} \big(\sum_k {n \choose k} \frac{f_k}{2^k}\big) \frac{(z/2)^n}{n!} \\
&= f(z/2) \\
\end{aligned}
|
Moving on,
|
\begin{aligned}
f(z) &= f(z/2) e^z \\
&= e^z e^{z/2} e^{z/4} \cdots = e^{z + z/2 + z/4 + \cdots} & \text{telescope} \\
&= e^{z \frac{1}{1-\frac{1}{2}}} & \text{geometric series} \\
&= e^{2z} \\
f(z) &= \sum_{n \ge 0} \frac{2^n z^n}{n!} & \text{Talyor series expansion} \\
[z^n]f(z) \equiv f_n &= 2^n \\
\end{aligned}
|
\Box
Source: Analysis of Algorithm
Friday, December 27, 2013
Catalan Recurrence
Solve
|
\begin{aligned}
T_N &= \sum_{1 \le k < N} T_k T_{N-1-k} + \delta_{N0} \\
T(z) \equiv \sum_{N \ge 0} T_N z_N &= \sum_{N \ge 0} \sum_{1 \le k < N} T_k T_{N-1-k}\, z^N + 1 & \text{multiply by }z^N \text{ and sum over all }N \\
&= 1 + \sum_{k \ge 0} \sum_{N > k} T_k T_{N-1-k}\, z^N & \text{switch summation order} \\
&= 1 + \sum_{k \ge 0} \sum_{N \ge 0} T_k T_{N}\, z^{N+k+1} & \text{change }N \text{ to } N+k+1 \\
&= 1 + z\big(\sum_{k \ge 0} T_k z^k\big) \big(\sum_{N \ge 0} T_{N} z^N\big) & \text{distribute} \\
T(z) &= 1 + z T(z)^2 \\
T(z)^2 - \frac{T(z)}{z} &= -\frac{1}{z} \\
\big(T(z) - \frac{1}{2z}\big)^2 &= -\frac{1}{z} + \frac{1}{4z^2} = \frac{1-4z}{4z^2} \\
T(z) - \frac{1}{2z} &= \frac{\pm \sqrt{1-4z}}{2z} \\
\therefore zT(z) &= \frac{1}{2} ( 1 \pm \sqrt{1-4z}) \\
&= \frac{1}{2} ( 1 \pm (1-4z)^{1/2}) \\
&= \frac{1}{2} \big( 1 \pm \sum_{N \ge 0} {\frac{1}{2} \choose N} (-4z)^N \big) & \text{binomial theorem} \\
&= \frac{1}{2} \big( 1 \pm (1 + \sum_{N \ge 1} {\frac{1}{2} \choose N} (-4z)^N ) \big) \\
zT(z) &= -\frac{1}{2} \sum_{N \ge 1} {\frac{1}{2} \choose N} (-4z)^N & \text{pick the minus sign} \\
T(z) &= -\frac{1}{2} \sum_{N \ge 1} {\frac{1}{2} \choose N} (-4)^N z^{N-1} \\
&= -\frac{1}{2} \sum_{N \ge 0} {\frac{1}{2} \choose N+1} (-4)^{N+1} z^{N} & \text{re-index} \\
[z^N]T(z) \equiv T_N &= -\frac{1}{2} {\frac{1}{2} \choose N+1} (-4)^{N+1} \\
T_N &= -\frac{1}{2} \frac{\frac{1}{2} (\frac{1}{2}-1) (\frac{1}{2}-2) \cdots (\frac{1}{2}-N)}{(N+1)!} (-4)^{N+1} &\text{expand via definition} \\
&= -\frac{1}{2} \frac{\frac{1}{2} (\frac{1}{2}-1) (\frac{1}{2}-2) \cdots (\frac{1}{2}-N)}{(N+1)!} (-2)^{N+1} 2^{N+1} \\
&= \frac{(-\frac{1}{2}) (\frac{1}{2}-1) (\frac{1}{2}-2) \cdots (\frac{1}{2}-N)}{(N+1)!} (-2)^{N+1} 2^N \\
&= \frac{1 \cdot 3 \cdot 5 \cdots (2N-1)}{(N+1)!} 2^N &\text{distribute } (-2)^{N+1} \text{ among factors} \\
&= \frac{1 \cdot 3 \cdot 5 \cdots (2N-1)}{(N+1)!} \cdot \frac{2 \cdot 4 \cdot 6 \cdots 2N}{1 \cdot 2 \cdot 3 \cdots N} &\text{expand out }2^N \\
&= \frac{1}{N+1}\cdot \frac{1 \cdot 3 \cdot 5 \cdots (2N-1)}{N!} \cdot \frac{2 \cdot 4 \cdot 6 \cdots 2N}{1 \cdot 2 \cdot 3 \cdots N} \\
&= \frac{1}{N+1}\cdot \frac{2N!}{N!\, N!} \\
T_N &= \frac{1}{N+1} {2N \choose N} & \text{the Catalan number}\\
\end{aligned}
|
\Box
Source: Analysis of Algorithm
Thursday, December 26, 2013
Quicksort Recurrence with OGF
Solve
|
\begin{aligned}
C_N &= N + 1 + \frac{2}{N}\sum_{1 \le k \le N} C_{k-1} \\
\end{aligned}
|
|
\begin{aligned}
N C_N &= N(N + 1) + 2 \sum_{1 \le k \le N} C_{k-1} & \text{multiply by } N\\
\sum_{N \ge 1} N C_N z^N &= \sum_{N \ge 1} N(N + 1) z^N + 2 \sum_{N \ge 1} \sum_{1 \le k \le N} C_{k-1} z^N & \text{multiply by } z^N \text{ and sum over all N} \\
\end{aligned}
|
Let
\displaystyle C(z) = \sum_{N \ge 0} C_N z^N. So,
|
\begin{aligned}
C'(z) &= \sum_{N \ge 1} N C_N z^{N-1} \\
\color{blue}{z C'(z)} &\color{blue}{= \sum_{N \ge 1} N C_N z^N} \\
\sum_{N \ge 1} N(N + 1) z^N &= \sum_{N \ge 2} (N - 1) N z^{N-1} & \text{re-index} \\
&= \frac{1}{z} \sum_{N \ge 2} (N - 1) N z^N \\
&= \frac{2}{z} \sum_{N \ge 2} \frac{N(N - 1)}{2} z^N \\
&= \frac{2}{z} \sum_{N \ge 2} {N \choose 2} z^N \\
&= \frac{2}{z} \cdot \frac{z^2}{(1-z)^3} & \text{corresponding OGF} \\
\color{blue}{\sum_{N \ge 1} N(N + 1) z^N} &\color{blue}{= \frac{2z}{(1-z)^3}} \\
2 \sum_{N \ge 1} \sum_{1 \le k \le N} C_{k-1} z^N &= 2z \sum_{N \ge 1} \sum_{1 \le k \le N} C_{k-1} z^{N-1} \\
&= 2z \sum_{N \ge 0} \sum_{1 \le k \le N+1} C_{k-1} z^N & \text{re-index } N \\
&= 2z \sum_{N \ge 0} \sum_{0 \le k \le N} C_k z^N & \text{re-index } k \\
\color{blue}{2 \sum_{N \ge 1} \sum_{1 \le k \le N} C_{k-1} z^N} &\color{blue}{= 2z \frac{C(z)}{1-z}} & \text{convolve simple OGF} \\
\end{aligned}
|
Recall
|
\begin{aligned}
\sum_{N \ge 1} N C_N z^N &= \sum_{N \ge 1} N(N + 1) z^N + 2 \sum_{N \ge 1} \sum_{1 \le k \le N} C_{k-1} z^N \\
z C'(z) &= \frac{2z}{(1-z)^3} + 2z \frac{C(z)}{1-z} \\
C'(z) &= \frac{2}{(1-z)^3} + 2 \frac{C(z)}{1-z} \\
\end{aligned}
|
But this is a
linear 1st order differential equation, which can be readily solved! In general, if
|
\begin{aligned}
x'(t) - A x(t) = B(t) \qquad &\text{standar form} \\
\end{aligned}
|
then
|
\begin{aligned}
x(t) &= e^{\int A} \cdot \int B e^{\int -A} & \text{see PennCalc wiki} \\
\end{aligned}
|
Now
|
\begin{aligned}
C'(z) - 2 \frac{C(z)}{1-z} &= \frac{2}{(1-z)^3} &\text{rewrite into standard form} \\
A &= \frac{2}{1-z} \\
B &= \frac{2}{(1-z)^3} \\
e^{\int A} &= e^{\int\frac{2}{1-z}} = e^{-2\ln(1-z)} = \frac{1}{(1-z)^2} \\
e^{\int - A} &= (1-z)^2 \\
C(z) &= e^{\int A} \cdot \int B e^{-\int A} = \frac{1}{(1-z)^2} \int \frac{2}{(1-z)^3} (1-z)^2 \\
&= \frac{1}{(1-z)^2} \int \frac{2}{(1-z)} \\
&= \frac{1}{(1-z)^2} \ln\frac{1}{(1-z)^2} \\
C(z) &= \frac{2}{(1-z)^2} \ln\frac{1}{(1-z)} \\
\end{aligned}
|
But we saw
earlier
|
\begin{aligned}
\text{[}z^N]\frac{1}{(1-z)^2} \ln\frac{1}{(1-z)} &= (N+1)(H_{N+1} - 1) \\
[z^N]C(z) &= [z^N]\frac{2}{(1-z)^2} \ln\frac{1}{(1-z)} = 2 (N+1)(H_{N+1} - 1) \\
\therefore C_N &= 2 (N+1)(H_{N+1} - 1) \\
\end{aligned}
|
\Box
Source: Analysis of Algorithm
Wednesday, December 25, 2013
Ex 3.4 - \sum_{1 \le k \le N} H_k
Prove that
|
\begin{aligned}
\sum_{1 \le k \le N} H_k = (N+1)(H_{N+1} - 1) \\
\end{aligned}
|
This proof showcases the power of OGF, ordinary generating function. Starting with the Talyer series expansion at around zero:
|
\begin{aligned}
OGF_1 = \frac{1}{1 - z} = \sum_{N \ge 0}z^N
\end{aligned}
|
In other words, the ordinary generating function (OGF) of the sequence (
1, 1, \cdots, 1), which are the coefficients of
\displaystyle \sum_{N \ge 0}z^N, is
\displaystyle \frac{1}{1 - z}.
Differentiating both sides of OGF_1:
|
\begin{aligned}
\frac{1}{(1 - z)^2} &= \sum_{N \ge 0}N z^{N-1} = \sum_{N \ge 1}N z^{N-1}\\
\color{teal}{\frac{1}{(1 - z)^2}} &\color{teal}{= \sum_{N \ge 0}(N+1)z^N} & \text{re-index} \\
\end{aligned}
|
Integrating both sides of
OGF_1:
|
\begin{aligned}
- \ln (1-z) &= \ln \frac{1}{1 - z} = \sum_{N \ge 0} \frac{z^{N+1}}{N+1} \\
\color{teal}{\ln \frac{1}{1 - z}} &\color{teal}{= \sum_{N \ge 1} \frac{1}{N} z^N} & \text{re-index} \\
\end{aligned}
|
which means
\displaystyle \ln (\frac{1}{1 - z}) is the OGF for the harmonic sequence (
\displaystyle 1, \frac{1}{2}, \cdots, \frac{1}{N}).
Recall, in general, if
\displaystyle A(z) = \sum_{k \ge 0} a_k z^k is the OGF of (
a_0, a_1, \cdots, a_k, \cdots), then
\displaystyle \frac{1}{1-z} A(z) is the OGF of (
a_0, a_0+a_1, a_0+a_1+a_2, \cdots ). So
|
\begin{aligned}
\frac{1}{1-z} \ln \frac{1}{1 - z} &= \sum_{N \ge 1} \big( \sum_{1 \le k \le N} \frac{1}{N} \big) z^N = \sum_{N \ge 1} H_N z^N \\
\frac{1}{(1-z)^2} \ln \frac{1}{1 - z} &= \sum_{N \ge 1} \big( \color{blue}{\sum_{1 \le k \le N} H_k} \big) z^N & \text{ applying } \frac{1}{1-z} \text{ again!} \\
\color{blue}{[z^N] \frac{1}{(1-z)^2} \ln \frac{1}{1 - z}} &\color{blue}{= \sum_{1 \le k \le N} H_k} & \text{extract coefficient of } z^N \\
\end{aligned}
|
But recall from above
|
\begin{aligned}
\color{teal}{\frac{1}{(1 - z)^2} \ln \frac{1}{1 - z}} &\color{teal}{= \big(\sum_{N \ge 0}(N+1)z^N\big) \big(\sum_{k \ge 1} \frac{1}{k} z^k\big)} \\
&= \sum_{N \ge 0} \sum_{k \ge 1} (N+1) \frac{1}{k} z^{N+k} \\
&= \sum_{N \ge k} \sum_{k \ge 1} (N-k + 1) \frac{1}{k} z^{N} & \text{re-index} \\
&= \sum_{N \ge 1} \big( \sum_{1 \le k \le N} (N+1-k) \frac{1}{k} \big) z^{N} \\
&= \sum_{N \ge 1} \big( \sum_{1 \le k \le N} (\frac{1}{k}(N + 1) - 1) \big) z^{N} \\
&= \sum_{N \ge 1} \big( \color{blue}{H_N (N + 1) - N} \big) z^{N} \\
\color{blue}{[z^N] \frac{1}{(1 - z)^2} \ln \frac{1}{1 - z}} &\color{blue}{= H_N (N + 1) - N} & \text{extract coefficient of } z^N \\
\therefore \sum_{1 \le k \le N} H_k &= H_N (N + 1) - N \\
\end{aligned}
|
Now
|
\begin{aligned}
H_{N+1} &= H_N + \frac{1}{N+1} \\
H_N &= H_{N+1} - \frac{1}{N+1} \\
\sum_{1 \le k \le N} H_k &= (H_{N+1} - \frac{1}{N+1}) (N + 1) - N \\
&= (N + 1) H_{N+1} - 1 - N = (N + 1) H_{N+1} - (N+1) \\
\sum_{1 \le k \le N} H_k &= (N+1)(H_{N+1} - 1) \\
\end{aligned}
|
\Box
Source: Analysis of Algorithm
Sunday, December 22, 2013
Ex 2.17 - Fringe Analysis of 2-3 trees
Solve the recurrence
|
\begin{aligned}
A_N &= A_{N-1} - \frac{A_{N-1}}{N} + 2\big(1 - \frac{2A_{N-1}}{N} \big) & \text{for } N > 0 \text{ with } A_0 = 0.\\
\end{aligned}
|
Attempt:
|
\begin{aligned}
N A_N &= N A_{N-1} - 2A_{N-1} + 2N - 4A_{N-1} \\
&= (N - 6) A_{N-1} + 2N \\
A_N &= \frac{N - 6}{N} A_{N-1} + 2 \\
\frac{N!}{(N-6)!} A_N &= \frac{N!}{(N-6)!} \big(\frac{N - 6}{N} A_{N-1} + 2\big) & \text{divide by summation factor of }\frac{(N-6)!}{N!} \\
&= \frac{(N-1)!}{(N-7)!} A_{N-1} + 2 \frac{N!}{(N-6)!} \\
\frac{N!}{(N-6)!6!} A_N &= \frac{(N-1)!}{(N-7)!6!} A_{N-1} + 2 \frac{N!}{(N-6)!6!} & \text{divide by }6! \\
{N \choose 6} A_N &= {N-1 \choose 6} A_{N-1} + 2 {N \choose 6} & \text{note } N \text{ must be }> 6\\
&= {7 \choose 6} A_{7} + 2 {8 \choose 6} + \cdots + 2 {N \choose 6} & \text{telescope} \\
&= 7 A_{7} + 2 \sum_{k=8}^N {k \choose 6} \\
&= 7 A_{7} + 2 \sum_{k=0}^N {k \choose 6} - 2 \sum_{k=0}^7 {k \choose 6} \\
&= 7 A_{7} + 2 {N+1 \choose 6+1} - 2 {7+1 \choose 6+1} & \text{sum of binomial coefficients over upper index} \\
A_N &= \frac{1}{{N \choose 6}} \big(7 A_{7} + 2 {N+1 \choose 7} - 16 \big) \\
\end{aligned}
|
Recall
A_N = \frac{N - 6}{N} A_{N-1} + 2, and
A_0 = 0. So
|
\begin{aligned}
A_1 &= \frac{1 - 6}{1} A_0 + 2 = 2 \\
A_2 &= \frac{2 - 6}{2} A_1 + 2 = -2 \\
A_3 &= \frac{3 - 6}{3} A_2 + 2 = 4 \\
A_4 &= \frac{4 - 6}{4} A_3 + 2 = 0 \\
A_5 &= \frac{5 - 6}{5} A_4 + 2 = 2 \\
A_6 &= \frac{6 - 6}{6} A_5 + 2 = 2 \\
A_7 &= \frac{7 - 6}{7} A_6 + 2 = \frac{16}{7} \\
\therefore A_N &= \frac{1}{{N \choose 6}} \big(16 + 2 {N+1 \choose 7} - 16 \big) = \frac{2}{{N \choose 6}} {N+1 \choose 7} \\
&= \frac{2 (N+1)!}{7! (N+1-7)!}\cdot \frac{6! (N-6)!}{N!} \\
A_N &= \frac{2}{7}(N+1) & \text{!} \\
\end{aligned}
|
\Box
See:
Sum of Binomial Coefficients over Upper Index
Source: Analysis of Algorithm
Mergesort comparisions
Number of comparisons in mergesort:
|
\begin{aligned}
C_N &= C_{\lfloor N/2 \rfloor} + C_{\lceil N/2 \rceil} + N & \text{ for } N > 1 \text{ with } C_1 = 0 \\
\end{aligned}
|
How do we solve for
C_N ?
|
\begin{aligned}
C_{N+1} &= C_{\lfloor (N+1)/2 \rfloor} + C_{\lceil (N+1)/2 \rceil} + N + 1 \\
&= C_{\lceil N/2 \rceil} + C_{\lfloor N/2 \rfloor + 1} + N + 1 & \text{!}\\
C_{N+1} - C_N &= C_{\lfloor N/2 \rfloor + 1} - C_{\lfloor N/2 \rfloor} + 1 \\
\end{aligned}
|
Now, let
|
\begin{aligned}
D_N &= C_{N+1} - C_N \\
&= C_{\lfloor N/2 \rfloor + 1} - C_{\lfloor N/2 \rfloor} + 1 \\
\therefore D_N &= D_{\lfloor N/2 \rfloor} + 1 \\
\end{aligned}
|
Note
|
\begin{aligned}
D_1 &= C_{1+1} - C_1 = C_2 - C_1 = C_2 \\
C_2 &= C_{\lfloor 2/2 \rfloor} + C_{\lceil 2/2 \rceil} + 2 \\
&= C_1 + C_1 + 2 = 2 \\
\therefore D_1 &= 2 \\
D_N &= D_{\lfloor N/2 \rfloor} + 1 \\
&= D_1 + \lfloor \ln N \rfloor & \text{telescope} \\
\therefore D_N &= \lfloor \ln N \rfloor + 2 \\
\end{aligned}
|
Solving for
C_N,
|
\begin{aligned}
C_{N+1} &= C_N + D_N & \text{rearranging terms} \\
&= C_N + \lfloor \ln N \rfloor + 2\\
&= C_1 + (\lfloor \ln 1 \rfloor + 2) + \cdots + (\lfloor \ln N \rfloor + 2) & \text{telescope} \\
&= 2N + C_1 + \sum_{k=1}^N \lfloor \ln k \rfloor \\
&= 2N + \sum_{k=1}^N \lfloor \ln k \rfloor \\
C_N &= 2(N-1) + \sum_{k=1}^{N-1} \lfloor \ln k \rfloor \\
&= (N-1) + \sum_{k=1}^{N-1} \big(\lfloor \ln k \rfloor + 1\big) \\
C_N &= N - 1 + \sum_{k=1}^{N-1} \big(\lfloor \ln k \rfloor + 1\big) \\
\end{aligned}
|
Note
\lfloor \ln k \rfloor + 1 is the number if bits in the binary representation of k. Here is why.
- Let B_N be the number if bits in the binary representation of N.
- Obviously, B_1 = 1.
- removing the right most bit of N gives \lfloor N/2 \rfloor .
Therefore
B_N = B_{\lfloor N/2 \rfloor} + 1 is the number of bits in the binary representation of N, for
N > 1 with
B_1 = 1. Interestingly, this happens to be the same recurrence as binary search. Now
|
\begin{aligned}
B_N &= B_{\lfloor N/2 \rfloor} + 1 \\
&= B_1 + \lfloor \ln N \rfloor & \text{telescope} \\
\therefore B_N &= \lfloor \ln N \rfloor + 1 \\
\end{aligned}
|
Thus the theorem:
|
\begin{aligned}
C_N = N −1 + \text{ number of bits in binary representation of all numbers} < N.
\end{aligned}
|
Let
S_N be the number of bits in binary representation of all numbers
< N.
|
\begin{aligned}
S_N &= \sum_{k=1}^{N-1} B_k \\
&= \sum_{k=1}^{N-1} \big(\lfloor \ln k \rfloor + 1\big) \\
\end{aligned}
|
When the binary representations of all numbers from
0 to
N-1 is laid out, all the bits can be viewed as being contained in a rectangle of
N by
\ln \lfloor N \rfloor + 1. Subtracting all the leading zeros column by column, we get
|
\begin{aligned}
S_N &= N (\lfloor \ln N \rfloor + 1) - \sum_{k=0}^{\lfloor \ln N \rfloor} 2^k \\
&= N (\lfloor \ln N \rfloor + 1) - (2^{\lfloor \ln N \rfloor + 1} - 1) \\
&= N \lfloor \ln N \rfloor - 2^{\lfloor \ln N \rfloor + 1} + N + 1 \\
\end{aligned}
|
Now, recall
|
\begin{aligned}
C_N &= N − 1 + \text{ number of bits in binary representation of all numbers} < N \\
&= N - 1 + S_N \\
\therefore C_N &= N \lfloor \ln N \rfloor - 2^{\lfloor \ln N \rfloor + 1} + 2N & \text{!} \\
\end{aligned}
|
\Box
Source: Analysis of Algorithm
Saturday, December 21, 2013
Ex 1.14 - AofA
Solve the recurrence:
|
\begin{aligned}
A_N &= 1 + \frac{2}{N}\sum_{j=1}^N A_{j-1} &\text{ for } N > 0 \\
N A_N &= N + 2\sum_{j=1}^N A_{j-1} \\
(N-1) A_{N-1} &= (N-1) + 2\sum_{j=1}^{N-1} A_{j-1} &\text{for } N-1 \text{ instead of } N \\
NA_N - (N-1)A_{N-1} &= 1 + 2A_{N-1} &\text{subtract the last two equations}\\
NA_N &= (N+1)A_{N-1} + 1 &\text{note it's now linear time, not quadratic!}\\
\frac{A_N}{N+1} &= \frac{A_{N-1}}{N} + \frac{1}{N(N+1)} &\text{divide both sides by } N(N+1) \\
\frac{A_N}{N+1} &= \frac{A_{1}}{2} + \frac{1}{2 \cdot 3} + \cdots + \frac{1}{N(N+1)} &\text{telescope} \\
&= \frac{1}{1 \cdot 2} + \frac{1}{2 \cdot 3} + \cdots + \frac{1}{N(N+1)} &\text{note }A_1 = 1 \text{, assuming } A_0 = 0 \\
A_N &= (N+1) \sum_{k=1}^N \frac{1}{k(k+1)} \\
&= (N+1) \sum_{k=1}^N (\frac{1}{k} - \frac{1}{k+1}) & \text{partial fraction} \\
&= (N+1) (1 - \frac{1}{N+1}) & \text{telescoping series} \\
A_N &= N & \Box \\
\end{aligned}
|
Alternatively, instead of using telescoping series,
|
\begin{aligned}
A_N &= (N+1) \sum_{k=1}^N (\frac{1}{k} - \frac{1}{k+1}) \\
A_N &\thicksim N \sum_{k=1}^N (\frac{1}{k} - \frac{1}{k+1}) & f(x) \thicksim g(x) \text{ means} \lim_{x\rightarrow\infty}\frac{f(x)}{g(x)} = 1 \\
&= N \big(\sum_{k=1}^N \frac{1}{k} - \sum_{k=1}^N \frac{1}{k+1}\big) \\
\end{aligned}
|
By the definition of
Euler's constant,
|
\begin{aligned}
\gamma &= \lim_{N \rightarrow \infty} \big( \sum_{k=1}^N \frac{1}{k} - \ln N \big) \\
\therefore \lim_{N \rightarrow \infty} \big( \sum_{k=1}^N \frac{1}{k} &= \ln N + \gamma \big) \\
\gamma &= 1 + \lim_{N \rightarrow \infty} \big( \sum_{k=2}^N \frac{1}{k} - \ln N \big) & \text{pull out the 1st term} \\
&= 1 + \lim_{N \rightarrow \infty} \big( \sum_{k=1}^{N-1} \frac{1}{k+1} - \ln N \big) & \text{re-index} \\
\lim_{N \rightarrow \infty} \big( \sum_{k=1}^{N-1} \frac{1}{k+1} &= \ln N + \gamma - 1 \big) \\
\therefore \lim_{N \rightarrow \infty} \big( \sum_{k=1}^{N} \frac{1}{k+1} &= \ln (N+1) + \gamma - 1 \big) & \text{for } N+1 \text{ instead of } N \\
\end{aligned}
|
Hence
|
\begin{aligned}
A_N &\thicksim N \big(\sum_{k=1}^N \frac{1}{k} - \sum_{k=1}^N \frac{1}{k+1}\big) \\
A_N &\thicksim N \big[ \big(\ln N + \gamma\big) - \big(\ln (N+1) + \gamma - 1\big) \big] \\
A_N &\thicksim N \big(\ln N - \ln (N+1) + 1\big) \\
A_N &\thicksim N &\Box \\
\end{aligned}
|
Source:
Analysis of Algorithm
Friday, December 20, 2013
Quicksort Recurrence
Assume array of size N with entries distinct and randomly ordered. Let C_N be the expected number of compares used by quicksort. Starting with the recurrence relation:
|
\begin{aligned}
C_N &= N + 1 + \sum_{k=1}^N \frac{1}{N}(C_{k-1} + C_{N-k}) & C_0 = 0\\
&= N + 1 + \frac{2}{N}\sum_{k=1}^N C_{k-1} &\text{by symmetry} \\
NC_N &= N(N + 1) + 2\sum_{k=1}^N C_{k-1} \\
(N-1)C_{N-1} &= (N - 1)N + 2\sum_{k=1}^{N-1} C_{k-1} &\text{for } N-1 \text{ instead of } N \\
NC_N - (N-1)C_{N-1} &= 2N + 2C_{N-1} &\text{subtract the last two equations}\\
NC_N &= (N+1)C_{N-1} + 2N & \text{note it's now linear time, not quadratic!}\\
\frac{C_N}{N+1} &= \frac{C_{N-1}}{N} + \frac{2}{N+1} &\text{divide both sides by } N(N+1) \\
\frac{C_N}{N+1} &= \frac{C_{N-1}}{N} + \frac{2}{N+1} = \frac{C_{N-2}}{N-1} + \frac{2}{N} + \frac{2}{N+1} &\text{telescope} \\
\frac{C_N}{N+1} &= \frac{C_1}{2} + \frac{2}{3} + \cdots + \frac{2}{N} + \frac{2}{N+1} & \text{note } C_1 = 2 \\
C_N &= (N + 1) (1 + \frac{2}{3} + \cdots + \frac{2}{N}) + 2 \\
C_N &\thicksim N (1 + \frac{2}{3} + \cdots + \frac{2}{N}) & f(x) \thicksim g(x) \text{ means} \lim_{x\rightarrow\infty}\frac{f(x)}{g(x)} = 1\\
C_N &\thicksim N + 2N(\frac{1}{3} + \cdots + \frac{1}{N}) \\
C_N &\thicksim N + 2N(1 + \frac{1}{2} + \frac{1}{3} + \cdots + \frac{1}{N}) - 3N \\
C_N &\thicksim 2N \sum_{k=1}^N \frac{1}{k} - 2N \\
C_N &\thicksim 2N (\int_1^N\frac{1}{x}\,dx + \gamma) - 2N & \gamma = \text{Euler's constant} \approx .57721 \\
&= 2N \ln N - 2(1 - \gamma) N \qquad & \text{Isn't this amazing ?}\\
\end{aligned}
|
Source:
Analysis of Algorithm
