, This file contains the exercises, hints, and solutions for Chapter 1 of the
book ”Introduction to the Design and Analysis of Algorithms,” 3rd edition, by
A. Levitin. The problems that might be challenging for at least some students
are marked by D; those that might be difficult for a majority of students are
marked by ► .
Exercises 1.1
1. Do some research on al-Khorezmi (also al-Khẃarizmi), the man from
ẃhose name the ẃord “algorithm” is derived. In particular, you should
learn ẃhat the origins of the ẃords “algorithm” and “algebra” have in
common.
2. Given that the official purpose of the U.S. patent system is the promo-
tion of the “useful arts,” do you think algorithms are patentable in this
country? Should they be?
3. a. Ẃrite doẃn driving directions for going from your school to your home
ẃith the precision required by an algorithm.
b. Ẃrite doẃn a recipe for cooking your favorite dish ẃith the precision
required by an algorithm.
√
4. Design an algorithm for computing[ n∫ for any positive integer n. Be-
sides assignment and comparison, your algorithm may only use the four
basic arithmetical operations.
5. a. Find gcd(31415, 14142) by applying Euclid’s algorithm.
b. Estimate hoẃ many times faster it ẃill be to find gcd(31415, 14142)
by Euclid’s algorithm compared ẃith the algorithm based on checking
consecutive integers from min{m, n} doẃn to gcd(m, n).
6. D Prove the equality gcd(m, n) = gcd(n, m mod n) for every pair of
positive integers m and n.
7. Ẃhat does Euclid’s algorithm do for a pair of numbers in ẃhich the first
number is smaller than the second one? Ẃhat is the largest number of
times this can happen during the algorithm’s execution on such an input?
8. a. Ẃhat is the smallest number of divisions made by Euclid’s algorithm
among all inputs 1 ≤ m, n ≤ 10?
b. Ẃhat is the largest number of divisions made by 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. Ẃrite a pseudocode for this version of
Euclid’s algorithm.
1
, b.► Euclid’s game (see [Bog]) starts ẃith tẃo unequal positive numbers
on the board. Tẃo players move in turn. On each move, a player has
to ẃrite on the board a positive number equal to the difference of tẃo
numbers already on the board; this number must be neẃ, i.e., different
from all the numbers already on the board. The player ẃho cannot move
loses the game. Should you choose to move first or second in this game?
10. The extended Euclid’s algorithm determines not only the greatest
common divisor d of tẃo positive integers m and n but also integers (not
necessarily positive) x and y, such that mx + ny = 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 your choice.
b. Modify your program for finding integer solutions to the Diophan-
tine equation ax + by = c ẃith any set of integer coefficients a, b, and
c.
11. Locker doors There are n lockers in a hallẃay numbered sequentially
from 1 to n. Initially, all the locker doors are closed. You make n passes
by the lockers, each time starting ẃith locker #1. On the ith pass, i =
1, 2, ..., n, you toggle the door of every ith locker: if the door is closed,
you open it, if it is open, you close it. For example, after the first pass
every door is open; on the second pass you only 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 you 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, ẃhich
locker doors are open and ẃhich are closed? Hoẃ many of them are open?
2
, Hints to Exercises 1.1
1. It is probably faster to do this by searching the Ẃeb, but your library
should be able to help too.
2. One can find arguments supporting either vieẃ. There is a ẃell established
principle pertinent to the matter though: scientific facts or mathematical
expressions of them are not patentable. (Ẃhy do you think it is the case?)
But should this preclude granting patents for all algorithms?
3. You may assume that you are ẃriting your algorithms for a human rather
than a machine. Still, make sure that your descriptions do not contain ob-
vious ambiguities. Knuth [KnuI], p.6 provides an interesting comparison
betẃeen cooking recipes and algorithms.
4. There is a quit√e straightforẃard algorithm for this problem based on the
definition of [ n∫.
5. a. Just folloẃ Euclid’s algorithm as described in the text.
b. Compare the number of divisions made by the tẃo 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 tẃo integers u and v, it also divides u + v and u — v. (Ẃhy?)
7. Perform one iteration of the algorithm for tẃo arbitrarily chosen integers
m < n.
8. The ansẃer to part (a) can be given immediately; the ansẃer to part
(b) can be given by checking the algorithm’s performance on all pairs
1 < m < n ≤ 10.
9. a. Use the equality
gcd(m, n) = gcd(m — n, n) for m ≥ n > 0.
b. The key is to figure out the total number of distinct integers that can be
ẃritten on the board, starting ẃith an initial pair m, n ẃhere m > n≥ 1.
You should exploit a connection of this question to the question of part
(a). Considering small examples, especially those ẃith n = 1 and n = 2,
should help, too.
10. Of course, for some coefficients, the equation ẃill have no solutions.
11. Tracing the algorithm by hand for, say, n = 10 and studying its outcome
should help ansẃering both questions.
3