Professional Documents
Culture Documents
This file contains the exercises, hints, and solutions for Chapter 2 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 B; those that might be difficult for a majority of students are
marked by I
Exercises 2.1
1. For each of the following algorithms, indicate (i) a natural size metric for
its inputs, (ii) its basic operation, and (iii) whether the basic operation
count can be different for inputs of the same size:
b. computing !
d. Euclid’s algorithm
e. sieve of Eratosthenes
b. Missing socks Imagine that after washing 5 distinct pairs of socks, you
discover that two socks are missing. Of course, you would like to have the
largest number of complete pairs remaining. Thus, you are left with 4
complete pairs in the best-case scenario and with 3 complete pairs in the
worst case. Assuming that the probability of disappearance for each
1
of the 10 socks is the same, find the probability of the best-case
scenario; the probability of the worst-case scenario; the number of pairs
you should expect in the average case.
5. a.B Prove formula (2.1) for the number of bits in the binary
representation of a positive integer.
b.B Prove the alternative formula for the number of bits in the binary
representation of a positive integer :
= dlog2( + 1)e
b. You are considering buying a computer that is 1000 times faster than
the one you currently have. By what factor will the faster computer in-
crease the sizes of systems solvable in the same amount of time as on
the old computer?
8. For each of the following functions, indicate how much the function’s
value will change if its argument is increased fourfold.
√ 2 3
a. log2 b. c. d. e. f. 2
9. Indicate whether the first function of each of the following pairs has a
smaller, same, or larger order of growth (to within a constant multiple)
than the second function.
2
2 2 3
a. ( + 1) and 2000 b. 100 and 0 01
2 2
c. log2 and ln d. log2 and log2
−1
b. How long would it take if instead of doubling the number of grains for
each square of the chessboard, the inventor asked for adding two grains?
3
Hints to Exercises 2.1
1. The questions are indeed as straightforward as they appear, though
some of them may have alternative answers. Also, keep in mind the
caveat about measuring an integer’s size.
2. a. The sum of two matrices is defined as the matrix whose elements are
the sums of the corresponding elements of the matrices given.
c. The formula will be the same, with just one small adjustment to ac-
count for the different radix.
P +1
10. a. Use the formula =0 2 =2 −1.
b. Use the formula for the sum of the first odd numbers or the for-mula
for the sum of arithmetic progression.
4
Another document from Scribd.com that is
random and unrelated content:
Finn, 84
Flollo, 78
Geoffrey Gaimar, 85
Germany, 16
Glamorgan, 65, 79
Godfrey of Boulogne, 8
Goleuddydd, 17
Grafton, 85
Griffin of Carmarthen, 92
Guido de Colonna, 60
Guinnion, 15, 20
Gwledig, 17
Gwythur, 2, 53
Hengist, 15, 33
Henry II., 84
Historia Brittonum, 14
Holinshed, 85
Ida, 16
Igerne, 57, 74
Joyous Gard, 98
Juvenal, 12
Lanval, 58
Laon, 31
Leodogran, 134
Lincoln, 97
Llandaff, 64
London, 53, 96
Mabinogion, The, 2, 3, 12, 17, 18, 37, 39, 101, 110, 134
Marie of France, 58
Maxen Wledig, 18
Meleager, 48
Modred (Medrod, Medraut), 26, 27, 34, 52, 75, 82, 112, 122
Monmouth, 65
Mont St Michel, 66
Mynneu, Mount, 52
Natanleod, 30
Nennius, 17, 19, 20, 22, 23, 29, 41, 71, 134
Neustria, 78
Octha, 15, 74
Parzival (Wagner’s), 4
Pembrokeshire, 23
Pryderi, 54
Robert Fitz-Hamon, 65
Rome, 67
Sackville, 86
St Asaph, 64
St Michael’s Mount, 80
Sarras, 98
Severus, 16
Shaftesbury, 74
Skene, W. F., 39
Somerset, 89
Stubbs, 28
Swinburne, 105
Tacitus, 12
Tristram (Tristan, Drystan), 10, 52, 54, 100, 104, 114, 129, 139
Troy, 60
Virgil, 68
Vivien, 112
Vortimer, 32
Warinus, 33, 34
Warner, William, 86
Winchester, 6, 97
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the
terms of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation
makes no representations concerning the copyright status of any
work in any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™
work in a format other than “Plain Vanilla ASCII” or other format
used in the official version posted on the official Project
Gutenberg™ website (www.gutenberg.org), you must, at no
additional cost, fee or expense to the user, provide a copy, a means
of exporting a copy, or a means of obtaining a copy upon request, of
the work in its original “Plain Vanilla ASCII” or other form. Any
alternate format must include the full Project Gutenberg™ License
as specified in paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive from
the use of Project Gutenberg™ works calculated using the method
you already use to calculate your applicable taxes. The fee is owed
to the owner of the Project Gutenberg™ trademark, but he has
agreed to donate royalties under this paragraph to the Project
Gutenberg Literary Archive Foundation. Royalty payments must be
paid within 60 days following each date on which you prepare (or
are legally required to prepare) your periodic tax returns. Royalty
payments should be clearly marked as such and sent to the Project
Gutenberg Literary Archive Foundation at the address specified in
Section 4, “Information about donations to the Project Gutenberg
Literary Archive Foundation.”
• You provide a full refund of any money paid by a user who notifies
you in writing (or by e-mail) within 30 days of receipt that s/he does
not agree to the terms of the full Project Gutenberg™ License. You
must require such a user to return or destroy all copies of the works
possessed in a physical medium and discontinue all use of and all
access to other copies of Project Gutenberg™ works.
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
1.F.4. Except for the limited right of replacement or refund set forth
in paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES
OF MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.
Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.