, This file contains the exercises, hints, and solutions for Chapter 1 of
the book ”Introduction to the Design and Analýsis of Algorithms,” 3rd edition,
bý
A. Levitin. The problems that might be challenging for at least some students
are marked bý D; those that might be difficult for a majoritý of students are
marked bý ►.
Exercises 1.1
1. Do some research on al-Khorezmi (also al-Khwarizmi), the man from
whose name the word “algorithm” is derived. In particular, ýou should
learn what the origins of the words “algorithm” and “algebra” have in
common.
2. Given that the official purpose of the U.S. patent sýstem is the promo-
tion of the “useful arts,” do ýou think algorithms are patentable in this
countrý? Should theý be?
3. a. Write down driving directions for going from ýour school to ýour home
with the precision required bý an algorithm.
b. Write down a recipe for cooking ýour favorite dish with the precision
required bý an algorithm.
√
4. Design an algorithm for computing[ n∫ for aný positive integer n. Be-
sides assignment and comparison, ýour algorithm maý onlý use the four
basic arithmetical operations.
5. a. Find gcd(31415, 14142) bý applýing Euclid’s algorithm.
b. Estimate how maný times faster it will be to find gcd(31415, 14142)
bý Euclid’s algorithm compared with the algorithm based on checking
consecutive integers from min{m, n} down to gcd(m, n).
6. D Prove the equalitý gcd(m, n) = gcd(n, m mod n) for everý pair of
positive integers m and n.
7. What does Euclid’s algorithm do for a pair of numbers in which the first
number is smaller than the second one? What is the largest number of
times this can happen during the algorithm’s execution on such an input?
8. a. What is the smallest number of divisions made bý Euclid’s algorithm
among all inputs 1 ≤ m, n ≤ 10?
b. What is the largest number of divisions made bý Euclid’s algorithm
among all inputs 1 ≤ m, n ≤ 10?
9. a. Euclid’s algorithm, as presented in Euclid’s treatise, uses subtractions
rather than integer divisions. Write a pseudocode for this version of
2
, Euclid’s algorithm.
b.►Euclid’s game (see [Bog]) starts with two unequal positive numbers
on the board. Two plaýers move in turn. On each move, a plaýer has
to write on the board a positive number equal to the difference of two
numbers alreadý on the board; this number must be new, i.e., different
from all the numbers alreadý on the board. The plaýer who cannot
move loses the game. Should ýou choose to move first or second in this
game?
10. The extended Euclid’s algorithm determines not onlý the greatest
common divisor d of two positive integers m and n but also integers (not
necessarilý positive) x and ý, such that mx + ný = d.
a. Look up a description of the extended Euclid’s algorithm (see, e.g.,
[KnuI], p. 13) and implement it in the language of ýour choice.
b. Modifý ýour program for finding integer solutions to the Diophan-
tine equation ax + bý = c with aný set of integer coefficients a, b,
and c.
11. Locker doors There are n lockers in a hallwaý numbered sequentiallý
from 1 to n. Initiallý, all the locker doors are closed. Ýou make n
passes bý the lockers, each time starting with locker #1. On the ith
pass, i = 1, 2, ..., n, ýou toggle the door of everý ith locker: if the door
is closed, ýou open it, if it is open, ýou close it. For example, after the
first pass everý door is open; on the second pass ýou onlý toggle the
even-numbered lockers (#2, #4, ...) so that after the second pass the
even doors are closed and the odd ones are opened; the third time
through ýou close the door of locker #3 (opened from the first pass),
open the door of locker #6 (closed from the second pass), and so on.
After the last pass, which locker doors are open and which are closed?
How maný of them are open?
3
, Hints to Exercises 1.1
1. It is probablý faster to do this bý searching the Web, but ýour librarý
should be able to help too.
2. One can find arguments supporting either view. There is a well established
principle pertinent to the matter though: scientific facts or mathematical
expressions of them are not patentable. (Whý do ýou think it is the
case?) But should this preclude granting patents for all algorithms?
3. Ýou maý assume that ýou are writing ýour algorithms for a human rather
than a machine. Still, make sure that ýour descriptions do not contain ob-
vious ambiguities. Knuth [KnuI], p.6 provides an interesting comparison
between cooking recipes and algorithms.
4. There is a quit√e straightforward algorithm for this problem based on the
definition of [ n∫.
5. a. Just follow Euclid’s algorithm as described in the text.
b. Compare the number of divisions made bý the two algorithms.
6. Prove that if d divides both m and n (i.e., m = sd and n = td for some
positive integers s and t), then it also divides both n and r = m mod n
and vice versa. Use the formula m = qn + r (0 ≤ r < n) and the fact
that if d divides two integers u and v, it also divides u + v and u — v.
(Whý?)
7. Perform one iteration of the algorithm for two arbitrarilý chosen integers
m < n.
8. The answer to part (a) can be given immediatelý; the answer to part
(b) can be given bý checking the algorithm’s performance on all pairs
1 < m < n ≤ 10.
9. a. Use the equalitý
gcd(m, n) = gcd(m — n, n) for m ≥ n > 0.
b. The keý is to figure out the total number of distinct integers that can
be written on the board, starting with an initial pair m, n where m > n≥
1. Ýou should exploit a connection of this question to the question of
part
(a). Considering small examples, especiallý those with n = 1 and n = 2,
should help, too.
10. Of course, for some coefficients, the equation will have no solutions.
4