• Home
  • Textbooks
  • Algorithms
  • Prologue

Algorithms

Sanjoy Dasgupta, Christos Papadimitriou, Umesh Vazirani

Chapter 0

Prologue - all with Video Answers

Educators


Chapter Questions

03:07

Problem 1

In each of the following situations, indicate whether $f=O(g),$ or $f=\Omega(g),$ or both (in which case $f=\Theta(g))$

Gabriel Eduok
Gabriel Eduok
Numerade Educator
02:10

Problem 2

Show that, if $c$ is a positive real number, then $g(n)=1+c+c^{2}+\dots+c^{n}$ is:
(a) $\Theta(1)$ if $c<1$
(b) $\Theta(n)$ if $c=1$
(c) $\Theta\left(c^{n}\right)$ if $c>1$
The moral: in big- - $\Theta$ terms, the sum of a geometric series is simply the first term if the series is strictly decreasing, the last term if the series is strictly increasing, or the number of terms if the series is unchanging.

Gabriel Eduok
Gabriel Eduok
Numerade Educator
05:13

Problem 3

The Fibonacci numbers $F_{0}, F_{1}, F_{2}, \ldots,$ are defined by the rule
\[
F_{0}=0, F_{1}=1, F_{n}=F_{n-1}+F_{n-2}
\]
In this problem we will confirm that this sequence grows exponentially fast and obtain some bounds on its growth.
(a) Use induction to prove that $F_{n} \geq 2^{0.5 n}$ for $n \geq 6$
(b) Find a constant $c<1$ such that $F_{n} \leq 2^{c n}$ for all $n \geq 0 .$ Show that your answer is correct.
(c) What is the largest $c$ you can find for which $F_{n}=\Omega\left(2^{\circ n}\right) ?$

Gabriel Eduok
Gabriel Eduok
Numerade Educator
05:55

Problem 4

Is there a faster way to compute the nth Fibonacci number than by fib2 (page 13 )? One idea involves matrices.
We start by writing the equations $F_{1}=F_{1}$ and $F_{2}=F_{0}+F_{1}$ in matrix notation:
\[
\left(\begin{array}{l}
F_{1} \\
F_{2}
\end{array}\right)=\left(\begin{array}{ll}
0 & 1 \\
1 & 1
\end{array}\right) \cdot\left(\begin{array}{l}
F_{0} \\
F_{1}
\end{array}\right)
\]
Similarly,
\[
\left(\begin{array}{l}
F_{2} \\
F_{3}
\end{array}\right)=\left(\begin{array}{ll}
0 & 1 \\
1 & 1
\end{array}\right) \cdot\left(\begin{array}{l}
F_{1} \\
F_{2}
\end{array}\right)=\left(\begin{array}{ll}
0 & 1 \\
1 & 1
\end{array}\right)^{2} \cdot\left(\begin{array}{l}
F_{0} \\
F_{1}
\end{array}\right)
\]
and in general
\[
\left(\begin{array}{c}
F_{n} \\
F_{n+1}
\end{array}\right)=\left(\begin{array}{cc}
0 & 1 \\
1 & 1
\end{array}\right)^{n} \cdot\left(\begin{array}{c}
F_{0} \\
F_{1}
\end{array}\right)
\]
So, in order to compute $F_{n},$ it suffices to raise this $2 \times 2$ matrix, call it $X,$ to the $n$ th power.
(a) Show that two $2 \times 2$ matrices can be multiplied using 4 additions and 8 multiplications.
But how many matrix multiplications does it take to compute $X^{n} ?$
(b) Show that $O(\log n)$ matrix multiplications suffice for computing $X^{n}$. (Hint:Think about computing $X^{8}$, )

Thus the number of arithmetic operations needed by our matrix-based algorithm, call it $\underline{f}$ ib a is just $O(\log n),$ as compared to $O(n)$ for $f$ ib 2 . Have we broken another exponential barrier?

The catch is that our new algorithm involves multiplication, not just addition; and multiplications of large numbers are slower than additions. We have already seen that, when the complexity of arithmetic operations is taken into account, the running time of $\mathrm{fib} 2$ becomes $O\left(n^{2}\right)$
(c) Show that all intermediate results of $f$ ib 3 are $O(n)$ bits long.
(d) Let $M(n)$ be the running time of an algorithm for multiplying $n$ -bit numbers, and assume that $M(n)=O\left(n^{2}\right)$ (the school method for multiplication, recalled in Chapter 1 , achieves this). Prove that the running time of $f$ ib 3 is $O(M(n) \log n$ ).
(e) Can you prove that the running time of $\mathrm{fib} 3$ is $\mathrm{O}(\mathrm{M}(n)) ?$ (Hint: The lengths of the numbers being multiplied get doubled with every squaring.)

In conclusion, whether fib3 is faster than $\mathrm{fib} 2$ depends on whether we can multiply $n$ -bit integers faster than $O\left(n^{2}\right) .$ Do you think this is possible? (The answer is in Chapter $2 .$ ) Finally, there is a formula for the Fibonacci numbers:
\[
F_{n}=\frac{1}{\sqrt{5}}\left(\frac{1+\sqrt{5}}{2}\right)^{n}-\frac{1}{\sqrt{5}}\left(\frac{1-\sqrt{5}}{2}\right)^{n}
\]
So, it would appear that we only need to raise a couple of numbers to the $n$ th power in order to compute $F_{n} .$ The problem is that these numbers are irrational, and computing them to sufficient accuracy is nontrivial. In fact, our matrix method fib3 can be seen as a roundabout way of raising these irrational numbers to the $n$ th power. If you know your linear algebra, you should see why. (Hint: What are the eigenvalues of the matrix $X ?$ )

Gabriel Eduok
Gabriel Eduok
Numerade Educator