Things about Algorithm Concepts

1. For an arbitrary sequence ann\, a_{nn} of certain elements numbered by two indices, the following holds.

i=1nj=1iaij=j=1ni=jnaij\begin{aligned} \sum_{i = 1}^{n} \sum_{j = 1}^{i} a_{ij} = \sum_{j = 1}^{n} \sum_{i = j}^{n} a_{ij} \end{aligned}

This arises from the following facts. This logic process is useful and can be applied to other similar problems. 1in and 1ji  1jin  1jn and jin\begin{aligned} &1 \leq i \leq n \text{ and } 1 \leq j \leq i \\ \Longleftrightarrow \; &1 \leq j \leq i \leq n \\ \Longleftrightarrow \; &1 \leq j \leq n \text{ and } j \leq i \leq n \end{aligned}

2. Approximation of Harmonic Number

Harmonic number HnH_n is defined by Hn=1+12+13++1n=k=1n1k,n0\begin{aligned} H_n = 1 + \frac{1}{2} + \frac{1}{3} + \cdots + \frac{1}{n} = \sum_{k = 1}^n \frac{1}{k}, \quad n \geq 0 \end{aligned}

The approximate size of this harmonic number is a well-known quantity as follows. Hn=lnn+γ+12n112n2+1120n4ϵ,0<ϵ<1252n6\begin{aligned} H_n = \ln n + \gamma + \frac{1}{2n} - \frac{1}{12n^2} + \frac{1}{120n^4} - \epsilon, \quad 0 < \epsilon < \frac{1}{252n^6} \end{aligned}

where γ=0.5772156649\gamma = 0.5772156649\dots is Euler’s constant. Thus HnH_n is reasonably close to the natural logarithm of nn.

3. n!n! is divisible by pμ\, p^\mu where n\, n is a natural number and p\, p is a prime number, and μ\, \mu is defined by

μ=k>0npk\begin{aligned} \mu = \sum_{k > 0} \left \lfloor \frac{n}{p^k} \right \rfloor \end{aligned}

That is, n!n! is divided by pp μ\mu times. In other words, μ\mu can be summed from the number of natural numbers having pp-factor and the number of ones having p2p^2-factor and so on. For example, when n=1000n = 1000 and p=3p = 3, μ=10003+10009+100027+100081+1000243+1000729+0+=333+111+37+12+4+1=498\begin{aligned} \mu &= \left \lfloor \frac{1000}{3} \right \rfloor + \left \lfloor \frac{1000}{9} \right \rfloor + \left \lfloor \frac{1000}{27} \right \rfloor + \left \lfloor \frac{1000}{81} \right \rfloor + \left \lfloor \frac{1000}{243} \right \rfloor + \left \lfloor \frac{1000}{729} \right \rfloor + 0 + \cdots \\\\ &= 333 + 111 + 37 + 12 + 4 + 1 = 498 \end{aligned}

It means that 1000!1000! is divisible by 34983^{498} and is not by 34993^{499}. n/pk+1=n/pk/p\lfloor n / p^{k + 1} \rfloor = \lfloor \lfloor n / p^k \rfloor / p \rfloor is useful for more simple calculation. With a similar logic to this note, this can be proved as follows. m=npk+1mnpk+1<m+1mpnpk<(m+1)pmpnpk<(m+1)pmnpk/p<m+1npk/p=m\begin{aligned} &\quad m = \left \lfloor \frac{n}{p^{k + 1}} \right \rfloor \Longleftrightarrow m \leq \frac{n}{p^{k + 1}} < m + 1 \Longleftrightarrow mp \leq \frac{n}{p^{k}} < (m + 1)p \Longleftrightarrow \\\\ &mp \leq \left \lfloor \frac{n}{p^k} \right \rfloor < (m + 1)p \Longleftrightarrow m \leq \left \lfloor \frac{n}{p^k} \right \rfloor / p < m + 1 \Longleftrightarrow \left \lfloor \left \lfloor \frac{n}{p^k} \right \rfloor / p \right \rfloor = m \end{aligned}

So, the kk-th step can be used for the (k+1)(k + 1)-th step.

4. Chebyshev’s Inequality

For data XX, let mm be the mean of XX and VV be the variance, then standard deviation σ=V\sigma = \sqrt{V}. σ\sigma is important from the fact that the probability of xim>rσ\vert x_i - m \vert > r \sigma is <1/r2< 1/r^2 for any real number r1r \geq 1 and xiXx_i \in X. Let this probability be pp and pi=P[X=i]p_i = P[X = i].

Chebyshev

If p>0p > 0, V=i(xim)2pi=xim>rσ(xim)2pi+ximrσ(xim)2pi>xim>rσ(rσ)2pi+0=(rσ)2xim>rσpi=(rσ)2p=pr2Vpr2V<V  p<1r2\begin{aligned} V &= \sum_{i} (x_i - m)^2 p_i = \sum_{\vert x_i - m \vert > r \sigma} (x_i - m)^2 p_i + \sum_{\vert x_i - m \vert \leq r \sigma} (x_i - m)^2 p_i \\\\ &> \sum_{\vert x_i - m \vert > r \sigma} (r \sigma)^2 p_i + 0 = (r \sigma)^2 \sum_{\vert x_i - m \vert > r \sigma} p_i = (r \sigma)^2 p = pr^2 V \\\\ \Longrightarrow \quad &pr^2 V < V \Longrightarrow \; p < \frac{1}{r^2} \end{aligned}

5. The mean and variance are easily from the generating function.

Generating function GG can be used whenever to obtain information about a sequence of numbers ana_n. By obtaining information from GG, information from ana_n can be obtained. G(z)=a0+a1z+a2z2+=n0zn\begin{aligned} G(z) = a_0 + a_1 z + a_2 z^2 + \cdots = \sum_{n \geq 0} z^n \end{aligned}

Consider the probabilities pk=P[X=k]p_k = P[X = k] as a sequence and generating function GG of them. Then, the mean mm and variance VV of GG can be defined as follows. G(z)=p0+p1z+p2z2+,m=kkpk,V=kk2pkm2\begin{aligned} G(z) = p_0 &+ p_1 z + p_2 z^2 + \cdots, \\\\ m = \sum_{k}k p_k, &\qquad V = \sum_{k}k^2 p_k - m^2 \end{aligned}

Note that G(1)=p0+p1+p2+=1G(1) = p_0 + p_1 + p_2 + \cdots = 1. Considering G(z)G'(z), mm can be represented as G(z)=p1+2p2z+=kkpkzk1,m=kkpk=G(1)\begin{aligned} G'(z) &= p_1 + 2 p_2 z + \cdots = \sum_{k} k p_k z^{k - 1}, \\\\ \Longrightarrow \quad & m = \sum_{k}k p_k = G'(1) \end{aligned}

Similarily, calculating G(z)G''(z), G(z)=kk(k1)pkzk2=k(k2k)pkzk2,kk2pk=k(k2k)pk+kkpk=G(1)+G(1),V=kk2pkm2=G(1)+G(1)G(1)2\begin{aligned} G''(z) &= \sum_{k} k (k - 1) p_k z^{k - 2} = \sum_{k} (k^2 - k) p_k z^{k - 2}, \\\\ \sum_{k}k^2 p_k &= \sum_{k} (k^2 - k) p_k + \sum_{k} k p_k = G''(1) + G'(1), \\\\ \Longrightarrow \quad V &= \sum_{k}k^2 p_k - m^2 = G''(1) + G'(1) - G'(1)^2 \end{aligned}

6. The mean and variance of two generating functions are summations of each one of them.

Assume that GG and HH are generating functions for some probabilities, which means that G(1)=H(1)=1G(1) = H(1) = 1. Let m(X)m(X) and V(X)V(X) be the mean and variance of probabilities represented by a generating function XX. Then, the following holds. m(GH)=m(G)+m(H),V(GH)=V(G)+V(H)\begin{aligned} m(GH) = m(G) + m(H), \qquad V(GH) = V(G) + V(H) \end{aligned}

7. When running into the 0/00/0-form, consider Taylor’s theorem as a workaround.

Consider a uniform distribution whose probability is 1/n1/n of obtaining the values 11, 22, \cdots, nn. The generating function of this is G(z)=1nz+1nz2++1nzn=1nzn+1zz1,G(1)=1n++1n=nn=1,G(z)=nzn+1(n+1)zn+1n(z1)2,G(z)=n(n1)zn+12(n+1)(n1)zn+n(n+1)zn12n(z1)3\begin{aligned} G(z) &= \frac{1}{n} z + \frac{1}{n} z^2 + \cdots + \frac{1}{n} z^n = \frac{1}{n} \frac{z^{n + 1} - z}{z - 1}, \\\\ G(1) &= \frac{1}{n} + \cdots + \frac{1}{n} = \frac{n}{n} = 1, \\\\ G'(z) &= \frac{nz^{n + 1} - (n + 1)z^n + 1}{n(z - 1)^2}, \\\\ G''(z) &= \frac{n(n - 1)z^{n + 1} - 2(n + 1)(n - 1)z^n + n(n + 1)z^{n - 1} - 2}{n(z - 1)^3} \end{aligned}

For finding the mean and variance, G(1)G'(1) and G(1)G''(1) are required but this makes the equations 0/00/0 when z=1z = 1. There exists a workaround. By Taylor’s theorem, G(1+z)=G(1)+G(1)z+G(1)2!z2+=1+G(1)z+G(1)2!z2+\begin{aligned} G(1 + z) = G(1) + G'(1)z + \frac{G''(1)}{2!} z^2 + \cdots = 1 + G'(1)z + \frac{G''(1)}{2!} z^2 + \cdots \end{aligned}

Replacing zz by z+1z + 1 in the original GG function and connecting this Taylor’s series, G(1+z)=1n(1+z)n+11zz=1+G(1)z+G(1)2!z2+\begin{aligned} G(1 + z) = \frac{1}{n} \frac{(1 + z)^{n + 1} - 1 - z}{z} = 1 + G'(1)z + \frac{G''(1)}{2!} z^2 + \cdots \end{aligned}

Since the coefficient of zz is related to the coefficient of z2z^2 in (1+z)n+1(1 + z)^{n + 1} and the coefficient of z2z^2 is related to the coefficient of z3z^3 in (1+z)n+1(1 + z)^{n + 1}, G(1)=1n(n+12)=1n(n+1)!(n1)!2!=1n(n+1)n2=n+12G(1)2!=1n(n+13)=1n(n+1)!(n2)!3!=1n(n+1)n(n1)6=(n+1)(n1)6\begin{aligned} G'(1) &= \frac{1}{n} \left(\begin{array}{c} n + 1 \\ 2 \end{array}\right) = \frac{1}{n} \frac{(n + 1)!}{(n - 1)!\,2!} = \frac{1}{n} \frac{(n + 1)n}{2} = \frac{n + 1}{2} \\\\ \frac{G''(1)}{2!} &= \frac{1}{n} \left(\begin{array}{c} n + 1 \\ 3 \end{array}\right) = \frac{1}{n} \frac{(n + 1)!}{(n - 2)!\,3!} = \frac{1}{n} \frac{(n + 1)n(n - 1)}{6} = \frac{(n + 1)(n - 1)}{6} \end{aligned}

Therefore, the mean mm and variance VV are as follows. m=G(1)=n+12,V=G(1)+G(1)G(1)2=(n+1)(n1)3+n+12(n+1)24=(n+1)(n13+12n+14)=(n+1)n112\begin{aligned} m &= G'(1) = \frac{n + 1}{2}, \\\\ V &= G''(1) + G'(1) - G'(1)^2 = \frac{(n + 1)(n - 1)}{3} + \frac{n + 1}{2} - \frac{(n + 1)^2}{4} \\\\ &= (n + 1)\left( \frac{n - 1}{3} + \frac{1}{2} - \frac{n + 1}{4} \right) = (n + 1)\frac{n - 1}{12} \end{aligned}

This is unstable situation. Generally, a distribution is called stable when the standard deviation is proportional to n\sqrt{n} and the difference between maximum and minimum is proportional to nn.

8. Any forest to a binary tree

There is a natural way to represent any forest as a binary tree.

  1. Linking together the children of each family
  2. Removing vertical links except from a parent to a first child

The final binary tree can be obtained after rotating the resulting tree. Note that any binary tree can be transformed into a unique forest by reversing the process.

Bintree

9. Huffman algorithm to find a tree with minimum weighted path length

Given mm weights w1w_1, w2w_2, \cdots, wmw_m that are all real numbers, the problem is to find a binary tree and to associate the weights with its nodes in such a way that the sum wjlj\sum w_j l_j is minimized, where ljl_j is the length of path from the root. The sum wjlj\sum w_j l_j is also called weighted path lengths. Huffman algorithm is an elegant algorithm for this problem. First find the two ww’s of lowest value, say w1w_1 and w2w_2. Then solve the problem for m1m - 1 weights w1+w2w_1 + w_2, w3w_3, \cdots, wmw_m. Every time this construction combines two weights, they are at least as big as the weights previously combined, if the given wiw_i were nonnegative. This means that there is a way to find Huffman’s tree, proveided that the given wieghts have been sorted into nondecreasing order. Using two queues, one containing the original wegiths and the other containing the combined wegiths, at each step the smallest unused weight will appear at the front of one of the queues. That is, there is no need to search for it. In general, there are many trees that minimize wjlj\sum w_j l_j. Furthermore, Huffman’s algorithm can be generalized to tt-ary trees as well as binary trees.

Reference

[1] Donald E. Knuth, The Art of Computer Programming, Volume 1: Fundamental Algorithms


© 2024. All rights reserved.