100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Exam (elaborations)

Exam (elaborations) TEST BANK FOR Introduction to The Design and Analysis of Algorithms By Anany Levitin (Solution Manual)

Rating
-
Sold
-
Pages
390
Grade
A+
Uploaded on
15-11-2021
Written in
2021/2022

Exam (elaborations) TEST BANK FOR Introduction to The Design and Analysis of Algorithms By Anany Levitin (Solution Manual) book ”Introduction to the Design and Analysis of Algorithms,” 2nd edition, by A. Levitin. The problems that might be challenging for at least some students are marked by ; 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-Khwarizmi), the man from whose name the word “algorithm” is derived. In particular, you 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 system is the promotion of the “useful arts,” do you think algorithms are patentable in this country? Should they be? 3. a. Write down driving directions for going from your school to your home with the precision required by an algorithm. b. Write down a recipe for cooking your favorite dish with the precision required by an algorithm. 4. Design an algorithm for computing  √ n for any positive integer n. Besides 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 how many times faster it will be to find gcd(31415, 14142) by Euclid’s algorithm compared with the algorithm based on checking consecutive integers from min{m, n} down to gcd(m, n). 6.  Prove the equality gcd(m, n) = gcd(n,mmod n) for every 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 by Euclid’s algorithm among all inputs 1 ≤ m, n ≤ 10? b. What 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. Write a pseudocode for this version of Euclid’s algorithm. 1 b. Euclid’s game (see [Bog]) starts with two unequal positive numbers on the board. Two players move in turn. On each move, a player has to write on the board a positive number equal to the difference of two numbers already on the board; this number must be new, i.e., different from all the numbers already on the board. The player who 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 two 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 Diophantine equation ax + by = c with any set of integer coefficients a, b, and c. 11. Locker doors There are n lockers in a hallway numbered sequentially from 1 to n. Initially, all the locker doors are closed. You make n passes by the lockers, each time starting with 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, which locker doors are open and which are closed? How many of them are open? 2 Hints to Exercises 1.1 1. It is probably faster to do this by searching the Web, but your library 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. (Why do you think it is the case?) But should this preclude granting patents for all algorithms? 3. You may assume that you are writing your algorithms for a human rather than a machine. Still, make sure that your descriptions do not contain obvious ambiguities. Knuth [KnuI], p.6 provides an interesting comparison between cooking recipes and algorithms. 4. There is a quite 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 by 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 = mmod 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. (Why?) 7. Perform one iteration of the algorithm for two arbitrarily chosen integers m<n. 8. The answer to part (a) can be given immediately; the answer 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 written on the board, starting with an initial pair m, n where m > n ≥ 1. You should exploit a connection of this question to the question of part (a). Considering small examples, especially those with n = 1 and n = 2, should help, too. 10. Of course, for some coefficients, the equation will have no solutions. 11. Tracing the algorithm by hand for, say, n = 10 and studying its outcome should help answering both questions. 3 Solutions to Exercises 1.1 1. Al-Khwarizmi (9th century C.E.) was a great Arabic scholar, most famous for his algebra textbook. In fact, the word “algebra” is derived from the Arabic title of this book while the word “algorithm” is derived from a translation of Al-Khwarizmi’s last name (see, e.g., [KnuI], pp. 1-2, [Knu96], pp. 88-92, 114). 2. This legal issue has yet to be settled. The current legal state of affairs distinguishes mathematical algorithms, which are not patentable, from other algorithms, which may be patentable if implemented as computer programs (e.g., [Cha00]). 3. n/a 4. A straightforward algorithm that does not rely on the availability of an approximate value of √ n can check the squares of consecutive positive integers until the first square exceeding n is encountered. The answer will be the number’s immediate predecessor. Note: A much faster algorithm for solving this problem can be obtained by using Newton’s method (see Sections 11.4 and 12.4). 5. a. gcd(31415, 14142) = gcd(14142, 3131) = gcd(3131, 1618) = gcd(1618, 1513) = gcd(1513, 105) = gcd(1513, 105) = gcd(105, 43) = gcd(43, 19) = gcd(19, 5) = gcd(5, 4) = gcd(4, 1) = gcd(1, 0) = 1. b. To answer the question, we need to compare the number of divisions the algorithms make on the input given. The number of divisions made by Euclid’s algorithm is 11 (see part a). The number of divisions made by the consecutive integer checking algorithm on each of its 14142 iterations is either 1 and 2; hence the total number of multiplications is between 1·14142 and 2·14142. Therefore, Euclid’s algorithm will be between 1·14142/11 ≈ 1300 and 2·14142/11 ≈ 2600 times faster. 6. Let us first prove that if d divides two integers u and v, it also divides both u+v and u−v. By definition of division, there exist integers s and t such that u = sd and v = td. Therefore u ± v = sd ± td = (s ± t)d, i.e., d divides both u + v and u − v. 4 Also note that if d divides u, it also divides any integer multiple ku of u. Indeed, since d divides u, u = sd. Hence ku = k(sd) = (ks)d, i.e., d divides ku. Now we can prove the assertion in question. For any pair of positive integers m and n, if d divides both m and n, it also divides both n and r = mmod n = m−qn. Similarly, if d divides both n and r = mmod n = m − qn, it also divides both m = r + qn and n. Thus, the two pairs (m, n) and (n, r) have the same finite nonempty set of common divisors, including the largest element in the set, i.e., gcd(m, n) = gcd(n, r). 7. For any input pair m, n such that 0 ≤ m < n, Euclid’s algorithm simply swaps the numbers on the first iteration: gcd(m, n) = gcd(n,m) because mmod n = m if m < n. Such a swap can happen only once since gcd(m, n) = gcd(n,mmod n) implies that the first number of the new pair (n) will be greater than its second number (mmod n) after every iteration of the algorithm. 8. a. For any input pair m ≥ n ≥ 1, in which m is a multiple of n, Euclid’s algorithm makes exactly one division; it is the smallest number possible for two positive numbers. b. The answer is 5 divisions, which is made by Euclid’s algorithm in computing gcd(5, 8). It is not too time consuming to get this answer by examining the number of divisions made by the algorithm on all input pairs 1 < m < n ≤ 10. Note: A pertinent general result (see [KnuII], p. 360) is that for any input pair m, n where 0 ≤ n < N, the number of divisions required by Euclid’s algorithm to compute gcd(m, n) is at most logφ(3−φ)N) where φ = (1+ √ 5)/2. 9. a. Here is a nonrecursive version: Algorithm Euclid2 (m, n) //Computes gcd(m, n) by Euclid’s algorithm based on subtractions //Input: Two nonnegative integers m and n not both equal to 0 //Output: The greatest common divisor of m and n while n = 0 do if m < n swap(m, n) m ← m− n return m 5 b. It is not too difficult to prove that the integers that can be written on the board are the integers generated by the subtraction version of Euclid’s algorithm and only them. Although the order in which they appear on the board may vary, their total number always stays the same: It is equal to m/gcd(m, n), where m is the maximum of the initial numbers, which includes two integers of the initial pair. Hence, the total number of possible moves is m/gcd(m, n)−2. Consequently, if m/gcd(m, n) is odd, one should choose to go first; if it is even, one should choose to go second. 10. n/a 11. Since all the doors are initially closed, a door will be open after the last pass if and only if it is toggled an odd number of times. Door i (1 ≤ i ≤ n) is toggled on pass j (1 ≤ j ≤ n) if and only if j divides i. Hence, the total number of times door i is toggled is equal to the number of its divisors. Note that if j divides i, i.e. i = jk, then k divides i too. Hence all the divisors of i can be paired (e.g., for i = 12, such pairs are 1 and 12, 2 and 6, 3 and 4) unless i is a perfect square (e.g., for i = 16, 4 does not have another divisor to be matched with). This implies that i has an odd number of divisors if and only if it is a perfect square, i.e., i = j2. Hence doors that are in the positions that are perfect squares and only such doors will be open after the last pass. The total number of such positions not exceeding n is equal to  √ n: these numbers are the squares of the positive integers between 1 and  √ n inclusively. 6 Exercises 1.2 1. Old World puzzle A peasant finds himself on a riverbank with a wolf, a goat, and a head of cabbage. He needs to transport all three to the other side of the river in his boat. However, the boat has room for only the peasant himself and one other item (either the wolf, the goat, or the cabbage). In his absence, the wolf would eat the goat, and the goat would eat the cabbage. Solve this problem for the peasant or prove it has no solution. (Note: The peasant is a vegetarian but does not like cabbage and hence can eat neither the goat nor the cabbage to help him solve the problem. And it goes without saying that the wolf is a protected species.) 2. New World puzzle There are four people who want to cross a bridge; they all begin on the same side. You have 17 minutes to get them all across to the other side. It is night, and they have one flashlight. A maximum of two people can cross the bridge at one time. Any party that crosses, either one or two people, must have the flashlight with them. The flashlight must be walked back and forth; it cannot be thrown, for example. Person 1 takes 1 minute to cross the bridge, person 2 takes 2 minutes, person 3 takes 5 minutes, and person 4 takes 10 minutes. A pair must walk together at the rate of the slower person’s pace. For example, if person 1 and person 4 walk across first, 10 minutes have elapsed when they get to the other side of the bridge. If person 4 returns the flashlight, a total of 20 minutes have passed and you have failed the mission. (Note: According to a rumor on the Internet, interviewers at a well-known software company located near Seattle have given this problem to interviewees.) 3. Which of the following formulas can be considered an algorithm for computing the area of a triangle whose side lengths are given positive numbers a, b, and c? a. S = p(p − a)(p − b)(p − c), where p = (a + b + c)/2 b. S = 1 2 bc sin A, where A is the angle between sides b and c c. S = 1 2aha, where ha is the height to base a 4. Write a pseudocode for an algorithm for finding real roots of equation ax2 + bx + c = 0 for arbitrary real coefficients a, b, and c. (You may assume the availability of the square root function sqrt(x).) 5. Describe the standard algorithm for finding the binary representation of a positive decimal integer a. in English. b. in a pseudocode. 7 6. Describe the algorithm used by your favorite ATM machine in dispensing cash. (You may give your description in either English or a pseudocode, whichever you find more convenient.) 7. a. Can the problem of computing the number π be solved exactly? b. How many instances does this problem have? c. Look up an algorithm for this problem on the World Wide Web. 8. Give an example of a problem other than computing the greatest common divisor for which you know more than one algorithm. Which of them is simpler? Which is more efficient? 9. Consider the following algorithm for finding the distance between the two closest elements in an array of numbers. Algorithm MinDistance(A[0..n − 1]) //Input: Array A[0..n − 1] of numbers //Output: Minimum distance between two of its elements dmin ←∞ for i ← 0 to n − 1 do for j ← 0 to n − 1 do if i = j and |A[i] − A[j]| < dmin dmin ← |A[i] − A[j]| return dmin Make as many improvements as you can in this algorithmic solution to the problem. (If you need to, you may change the algorithm altogether; if not, improve the implementation given.) 10. One of the most influential books on problem solving, titled How To Solve It [Pol57], was written by the Hungarian-American mathematician George Polya (1887—1985). Polya summarized his ideas in a four-point summary. Find this summary on the Web or, better yet, in his book, and compare it with the plan outlined in Section 1.2. What do they have in common? How are they different? 8 Hints to Exercises 1.2 1. The peasant would have to make several trips across the river, starting with the only one possible. 2. Unlike the Old World puzzle of Problem 1, the first move solving this puzzle is not obvious. 3. The principal issue here is a possible ambiguity. 4. Your algorithm should work correctly for all possible values of the coefficients, including zeros. 5. You almost certainly learned this algorithm in one of your introductory programming courses. If this assumption is not true, you have a choice between designing such an algorithm on your own or looking it up. 6. You may need to make a field trip to refresh your memory. 7. Question (a) is difficult though the answer to it–discovered in 1760s by the German mathematician Johann Lambert –is well-known. By comparison, question (b) is incomparably simpler. 8. You probably know two or more different algorithms for sorting an array of numbers. 9. You can: decrease the number of times the innermost loop is executed, make that loop run faster (at least for some inputs), or, more significantly, design a faster algorithm from scratch. 10. n/a 9 Solutions to Exercises 1.2 1. Let P, w, g, and c stand for the peasant, wolf, goat, and cabbage head, respectively. The following is one of the two principal sequences that solve the problem: Pwgc P g w c g Pw c Pwg c w P gc Pw c g w c P g Pwgc Note: This problem is revisited later in the book (see Section 6.6). 2. Let 1, 2, 5, 10 be labels representing the men of the problem, f represent the flashlight’s location, and the number in the parenthesis be the total amount of time elapsed. The following sequence of moves solves the problem: (0) f,1,2,5,10 f,1,2 (2) 5,10 2 (3) f,1,5,10 f,2,5,10 (13) 1 5,10 (15) f,1,2 f,1,2,5,10 (17) 3. a. The formula can be considered an algorithm if we assume that we know how to compute the square root of an arbitrary positive number. b. The difficulty here lies in computing sin A. Since the formula says nothing about how it has to be computed, it should not be considered an algorithm. This is true even if we assume, as we did for the square root function, that we know how to compute the sine of a given angle. (There are several algorithms for doing this but only approximately, of course.) The problem is that the formula says nothing about how to compute angle A either. c. The formula says nothing about how to compute ha. 4. Algorithm Quadratic(a, b, c) //The algorithm finds real roots of equation ax2 + bx + c = 0 //Input: Real coefficients a, b, c //Output: The real roots of the equation or a message about their absence if a = 0 D ← b ∗ b − 4 ∗ a ∗ c if D >0 temp ← 2 ∗ a x1 ← (−b + sqrt(D))/temp x2 ← (−b − sqrt(D))/temp 10 return x1, x2 else if D = 0 return −b/(2 ∗ a) else return ‘no real roots’ else //a = 0 if b = 0 return −c/b else //a = b = 0 if c = 0 return ‘all real numbers’ else return ‘no real roots’ Note: See a more realistic algorithm for this problem in Section 11.4. 5. a. Divide the given number n by 2: the remainder rn (0 or 1) will be the next (from right to left) digit of the binary representation in question. Replace n by the quotient of the last division and repeat this operation until n becomes 0. b. Algorithm Binary(n) //The algorithm implements the standard method for finding //the binary expansion of a positive decimal integer //Input: A positive decimal integer n //Output: The list bk bk−1...b1 b0 of n’s binary digits k ← 0 while n = 0 bk ← nmod 2 n ← n/2 k ← k +1 6. n/a 7. a. π, as an irrational number, can be computed only approximately. b. It is natural to consider, as an instance of this problem, computing π’s value with a given level of accuracy, say, with n correct decimal digits. With this interpretation, the problem has infinitely many instances. 8. n/a 9. The following improved version considers the same pair of elements only once and avoids recomputing the same expression in the innermost loop: Algorithm MinDistance2 (A[0..n − 1]) //Input: An array A[0..n − 1] of numbers //Output: The minimum distance d between two of its elements 11 dmin ←∞ for i ← 0 to n − 2 do for j ← i +1 to n − 1 do temp ← |A[i] − A[j]| if temp < dmin dmin ← temp return dmin A faster algorithm is based on the idea of presorting (see Section 6.1). 10. Polya’s general four-point approach is: 1. Understand the problem 2. Devise a plan 3. Implement the plan 4. Look back/check 12

Show more Read less











Whoops! We can’t load your doc right now. Try again or contact support.

Document information

Uploaded on
November 15, 2021
Number of pages
390
Written in
2021/2022
Type
Exam (elaborations)
Contains
Questions & answers

Subjects

  • solution manual

Content preview

, This file contains the exercises, hints, and solutions for Chapter 1 of the
book ”Introduction to the Design and Analysis of Algorithms,” 2nd edition, by
A. Levitin. The problems that might be challenging for at least some students
are marked by ; 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-Khwarizmi), the man from
whose name the word “algorithm” is derived. In particular, you 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 system is the promo-
tion of the “useful arts,” do you think algorithms are patentable in this
country? Should they be?
3. a. Write down driving directions for going from your school to your home
with the precision required by an algorithm.

b. Write down a recipe for cooking your favorite dish with 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 how many times faster it will be to find gcd(31415, 14142)
by Euclid’s algorithm compared with the algorithm based on checking
consecutive integers from min{m, n} down to gcd(m, n).
6.  Prove the equality gcd(m, n) = gcd(n, m mod n) for every 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 by Euclid’s algorithm
among all inputs 1 ≤ m, n ≤ 10?

b. What 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. Write a pseudocode for this version of
Euclid’s algorithm.

1

, b. Euclid’s game (see [Bog]) starts with two unequal positive numbers
on the board. Two players move in turn. On each move, a player has
to write on the board a positive number equal to the difference of two
numbers already on the board; this number must be new, i.e., different
from all the numbers already on the board. The player who 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 two 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 with any set of integer coefficients a, b, and
c.
11. Locker doors There are n lockers in a hallway numbered sequentially
from 1 to n. Initially, all the locker doors are closed. You make n passes
by the lockers, each time starting with 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, which
locker doors are open and which are closed? How many of them are open?




2

, Hints to Exercises 1.1
1. It is probably faster to do this by searching the Web, but your library
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. (Why do you think it is the case?)
But should this preclude granting patents for all algorithms?
3. You may assume that you are writing 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
between cooking recipes and algorithms.
4. There is a quite
√ 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 by 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. (Why?)
7. Perform one iteration of the algorithm for two arbitrarily chosen integers
m < n.
8. The answer to part (a) can be given immediately; the answer 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
written on the board, starting with an initial pair m, n where m > n ≥ 1.
You should exploit a connection of this question to the question of part
(a). Considering small examples, especially those with n = 1 and n = 2,
should help, too.
10. Of course, for some coefficients, the equation will have no solutions.
11. Tracing the algorithm by hand for, say, n = 10 and studying its outcome
should help answering both questions.


3

Get to know the seller

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
Expert001 Chamberlain School Of Nursing
View profile
Follow You need to be logged in order to follow users or courses
Sold
797
Member since
4 year
Number of followers
566
Documents
1190
Last sold
6 days ago
Expert001

High quality, well written Test Banks, Guides, Solution Manuals and Exams to enhance your learning potential and take your grades to new heights. Kindly leave a review and suggestions. We do take pride in our high-quality services and we are always ready to support all clients.

4.2

159 reviews

5
104
4
18
3
14
2
7
1
16

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions