Published on

The Algorithm Design Manual (3rd) Exercise Solution 'E2-32'

Table of Contents

Introduction

This series is my solutions for algorithm exercises in'The Algorithm Design Manual' 3rd edition.
It's my solutions, not model answers, so if you find some mistakes or more sophisticated solutions, please post in comment area below.

Repository

Here

Exercise 2-32

Question

For each of the following expressions f(n)f(n) find a simple g(n)g(n) such that f(n)=Θ(g(n))f(n) = Θ(g(n)).

(a)f(n)=i=1n1i(b)f(n)=i=1n1i(c)f(n)=i=1nlogi(d)f(n)=logn!\begin{align*} \text{(a)}\quad&f(n)=\sum_{i=1}^n\frac{1}{i}\\ \text{(b)}\quad&f(n)=\sum_{i=1}^n\left\lceil\frac{1}{i}\right\rceil\\ \text{(c)}\quad&f(n)=\sum_{i=1}^n\log{i}\\ \text{(d)}\quad&f(n)=\log{n!} \end{align*}

Solution

Solution for (a)

As a graph below shows, the summation can be regarded as an uppersum of a function y=1xy=\frac{1}{x} and bound by two reciprocal functions.

two function bindings

And the next inequation holds:

1n1xdxi=1n1i2n+11x1dx+1\begin{equation} \int_1^n\frac{1}{x}dx \le \sum_{i=1}^n\frac{1}{i} \le \int_2^{n+1}\frac{1}{x-1}dx+1 \end{equation}

Think of solving Left Hand Side and Right hand Side of the above formula.

1n1xdx=[lnx]1n=lnn\begin{align} \int_1^n\frac{1}{x}dx &= \left[\ln{|x|}\right]_1^n\\ &=\ln{n} \end{align} 2n+11x1dx+1=[lnx1]2n+1+1=lnn+1\begin{align} \int_2^{n+1}\frac{1}{x-1}dx+1 &= \left[\ln{|x-1|}\right]_2^{n+1}+1\\ &=\ln{n}+1 \end{align}

So, f(n)f(n)'s boundation and function g(n)g(n) is:

f(n)=Θ(logn)\begin{equation} f(n)=\Theta(\log{n}) \end{equation} g(n)=logn\begin{equation} g(n)=\log{n} \end{equation}

Solution for (b)

o<1i<1for{iN}\begin{equation} o \lt \frac{1}{i} \lt 1 \quad \text{for} \{i\in\mathbb{N}\} \end{equation}

So,

1i=1f(n)=i=1n1i=n\begin{align} \left\lceil \frac{1}{i} \right\rceil&=1\\ f(n)&=\sum_{i=1}^n\left\lceil\frac{1}{i}\right\rceil=n \end{align}

So function g(n)g(n) is:

g(n)=n\begin{equation} g(n)=n \end{equation}

Solution for (c)

From similar analysis as promlem(a),

1nlogxdx<i=1nlogi<2n+1log(x1)dx+1\begin{equation} \int_1^n\log{x}dx \lt \sum_{i=1}^n\log{i} \lt \int_2^{n+1}\log{(x-1)}dx+1 \end{equation}

Think of LHS and RHS.
For simplicity, we think abount lnx\ln{x} rather than logx\log{x}(the asymptotical order is same).

LHS=1nlnxdx=[xlnx]1n1n1dx=nlnn+Θ(n)=Θ(nlogn)\begin{align} LHS&=\int_1^n\ln{x}dx\\ &=\left[x\ln{x}\right]_1^n-\int_1^n1dx\\ &=n\ln{n}+\Theta(n)\\ &=\Theta(n\log{n}) \end{align} RHS=2n+1ln(x1)dx+1=[xln(x1)]2n+12n+1(11n1)dx+1=Θ(nlnn)+Θ(n)+Θ(lnn)+Θ(1)=Θ(nlogn)\begin{align} RHS&=\int_2^{n+1}\ln{(x-1)}dx+1\\ &=\left[x\ln{(x-1)}\right]_2^{n+1}-\int_2^{n+1}(1-\frac{1}{n-1})dx+1\\ &=\Theta(n\ln{n})+\Theta(n)+\Theta(\ln{n})+\Theta(1)\\ &=\Theta(n\log{n}) \end{align}

So,

g(n)=nlogn\begin{equation} g(n)=n\log{n} \end{equation}

Solution for (d)

The product as an anti-logarithm can be distributed into the summation of logarithm.

f(n)=logn!=i=1nlogi\begin{align} f(n)&=\log{n!}\\ &=\sum_{i=1}^n\log{i} \end{align}

The shape of the formula is exactly the same as that in the problem (c).
So the result is also the same.