Solution Manual For Introduction To The Design And Analysis Of Algorithms, 3rd Edition
- Information Technology
- 499 pages
- Shared May 2025
Prepare better with Solution Manual For Introduction To The Design And Analysis Of Algorithms, 3rd Edition, a solutions manual that breaks down complex textbook problems.
Preview 16 of 499 pages
See the first 16 pages exactly as they appear in the document.
Page 1 Read page 1 as text
This fi le 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 B ; those that might be di ffi cult for a majority of students are marked by I 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 o ffi cial 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 from an algorithm’s description. b. Write down a recipe for cooking your favorite dish with the precision required by an algorithm. 4. Design an algorithm for computing b √ c for any positive integer . Be- sides assignment and comparison, your algorithm may only use the four basic arithmetical operations. 5. Design an algorithm to fi nd all the common elements in two sorted lists of numbers. For example, for the lists 2, 5, 5, 5 and 2, 2, 3, 5, 5, 7, the output should be 2, 5, 5. What is the maximum number of comparisons your algorithm makes if the lengths of the two given lists are and respectively? 6. a. Find gcd (31415, 14142) by applying Euclid’s algorithm. b. Estimate how many times faster it will be to fi nd gcd (31415, 14142) by Euclid’s algorithm compared with the algorithm based on checking consecutive integers from min { } down to gcd( ) 7. B Prove the equality gcd( ) = gcd( mod ) for every pair of positive integers and . 8. What does Euclid’s algorithm do for a pair of integers in which the fi rst is smaller than the second? What is the maximum number of times this can happen during the algorithm’s execution on such an input? 9. a. What is the minimum number of divisions made by Euclid’s algorithm among all inputs 1 ≤ ≤ 10? 1
Page 2 Page 3 Read page 3 as text
b. What is the maximum number of divisions made by Euclid’s algorithm among all inputs 1 ≤ ≤ 10? 10. a. Euclid’s algorithm, as presented in Euclid’s treatise, uses subtractions rather than integer divisions. Write pseudocode for this version of Euclid’s algorithm. b. I Euclid’s game (see [Bog]) starts with two unequal positive in- tegers 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 di ff erence of two numbers already on the board; this number must be new, i.e., di ff erent from all the numbers already on the board. The player who cannot move loses the game. Should you choose to move fi rst or second in this game? 11. The extended Euclid’s algorithm determines not only the greatest common divisor of two positive integers and but also integers (not necessarily positive) and , such that + = 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 to fi nd integer solutions to the Diophantine equation + = with any set of integer coe ffi cients , , and . 12. B Locker doors There are lockers in a hallway, numbered sequentially from 1 to . Initially, all the locker doors are closed. You make passes by the lockers, each time starting with locker #1. On the th pass, = 1 2 , you toggle the door of every th locker: if the door is closed, you open it; if it is open, you close it. After the last pass, which locker doors are open and which are closed? How many of them are open? 2
Page 4 Read page 4 as text
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 fi nd arguments supporting either view. There is a well established principle pertinent to the matter, though: scienti fi c 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 provides an interesting comparison between cooking recipes and algorithms [KnuI, p.6]. 4. There is a quite straightforward algorithm for this problem based on the de fi nition of b √ c . 5. Try to design an algorithm that always makes less than comparisons. 6. a. Just follow Euclid’s algorithm as described in the text. b. Compare the number of divisions made by the two algorithms. 7. Prove that if divides both and (i.e., = and = for some positive integers and ) , then it also divides both and = mod and vice versa Use the formula = + (0 ≤ ) and the fact that if divides two integers and it also divides + and − (Why?) 8. Perform one iteration of the algorithm for two arbitrarily chosen integers 9. 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 ≤ 10 10. a. Use the equality gcd( ) = gcd( − ) for ≥ 0 b. The key is to fi gure out the total number of distinct integers that can be written on the board, starting with an initial pair where ≥ 1 You should exploit a connection of this question to the question of part (a). Considering small examples, especially those with = 1 and = 2 should help, too. 11. Of course, for some coe ffi cients, the equation will have no solutions. 12. Tracing the algorithm by hand for, say, = 10 and studying its outcome should help answering both questions. 3
Page 5 Read page 5 as text
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 a ff airs 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 √ can check the squares of consecutive positive integers until the fi rst square exceeding 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. Initialize the list of common elements to empty. Starting with the fi rst ele- ments of the lists given, repeat the following until one of the lists becomes empty. Compare the current elements of the two lists: if they are equal, add this element to the list of common elements and move to the next elements of both lists (if any); otherwise, move to the element following the smaller of the two involved in the comparison. The maximum number of comparisons, which is made by this algorithm on some lists with no common elements such as the fi rst positive odd numbers and the fi rst positive even numbers, is equal to + − 1 6. 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 itera- tions is either 1 and 2; hence the total number of multiplications is be- tween 1 · 14142 and 2 · 14142. Therefore, Euclid’s algorithm will be between 1 · 14142 11 ≈ 1300 and 2 · 14142 11 ≈ 2600 times faster. 4
Page 6 Read page 6 as text
7. Let us fi rst prove that if divides two integers and it also divides both + and − . By de fi nition of division, there exist integers and such that = and = Therefore ± = ± = ( ± ) i.e., divides both + and − Also note that if divides it also divides any integer multiple of Indeed, since divides = Hence = ( ) = ( ) i.e., divides Now we can prove the assertion in question. For any pair of positive integers and if divides both and , it also divides both and = mod = − Similarly, if divides both and = mod = − it also divides both = + and Thus, the two pairs ( ) and ( ) have the same fi nite nonempty set of common divisors, including the largest element in the set, i.e., gcd( ) = gcd( ) 8. For any input pair such that 0 ≤ Euclid’s algorithm simply swaps the numbers on the fi rst iteration: gcd( ) = gcd( ) because mod = if Such a swap can happen only once since gcd( ) = gcd( mod ) implies that the fi rst number of the new pair ( ) will be greater than its second number ( mod ) after every iteration of the algorithm. 9. a. For any input pair ≥ ≥ 1 in which is a multiple of 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 ≤ 10 Note: A pertinent general result (see [KnuII, p. 360]) is that for any input pair where 0 ≤ the number of divisions required by Euclid’s algorithm to compute gcd( ) is at most b log (3 − ) ) c where = (1 + √ 5) 2 . 5
Page 7 Read page 7 as text
10. a. Here is a nonrecursive version: Algorithm Euclid2 ( ) //Computes gcd( ) by Euclid’s algorithm based on subtractions //Input: Two nonnegative integers and not both equal to 0 //Output: The greatest common divisor of and while 6 = 0 do if swap ( ) ← − return b. It is not too di ffi cult 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 gcd( ) where is the maximum of the initial numbers which includes two integers of the initial pair. Hence, the total number of possible moves is gcd( ) − 2 Consequently, if gcd( ) is odd, one should choose to go fi rst; if it is even, one should choose to go second. 11. n/a 12. 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 (1 ≤ ≤ ) is toggled on pass (1 ≤ ≤ ) if and only if divides Hence, the total number of times door is toggled is equal to the number of its divisors. Note that if divides i.e. = then divides too. Hence all the divisors of can be paired (e.g., for = 12 such pairs are 1 and 12, 2 and 6, 3 and 4) unless is a perfect square (e.g., for = 16 4 does not have another divisor to be matched with). This implies that has an odd number of divisors if and only if it is a perfect square, i.e., = 2 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 is equal to b √ c : these numbers are the squares of the positive integers between 1 and b √ c inclusively. 6
Page 8 Read page 8 as text
Exercises 1.2 1. Old World puzzle A peasant fi nds 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 rickety 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 fl ashlight. A maximum of two people can cross the bridge at one time. Any party that crosses, either one or two people, must have the fl ashlight with them. The fl ashlight 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. (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 com- puting the area of a triangle whose side lengths are given positive numbers , , and ? a. = p ( − )( − )( − ) where = ( + + ) 2 b. = 1 2 sin where is the angle between sides and c. = 1 2 where is the height to base 4. Write pseudocode for an algorithm for fi nding real roots of equation 2 + + = 0 for arbitrary real coe ffi cients and (You may assume the availability of the square root function ( ) ) 5. Describe the standard algorithm for fi nding the binary representation of a positive decimal integer a. in English. b. in pseudocode. 6. Describe the algorithm used by your favorite ATM machine in dispensing cash. (You may give your description in either English or pseudocode, whichever you fi nd more convenient.) 7
Page 9 Read page 9 as text
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 Internet. 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 e ffi cient? 9. Consider the following algorithm for fi nding the distance between the two closest elements in an array of numbers. Algorithm MinDistance ( [0 − 1]) //Input: Array [0 .. − 1] of numbers //Output: Minimum distance between two of its elements ← ∞ for ← 0 to − 1 do for ← 0 to − 1 do if 6 = and | [ ] − [ ] | ← | [ ] − [ ] | return 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 in fl uential books on problem solving, titled How To Solve It [Pol57], was written by the Hungarian-American mathematician George Pólya (1887—1985). Pólya summarized his ideas in a four-point summary. Find this summary on the Internet 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 di ff erent? 8
Page 10 Read page 10 as text
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 fi rst 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 coe ffi - cients, 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 fi eld trip to refresh your memory. 7. Question (a) is di ffi cult, 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 di ff erent algorithms for sorting an array of numbers. 9. You can: decrease the number of times the inner loop is executed, make that loop run faster (at least for some inputs), or, more signi fi cantly, design a faster algorithm from scratch. 10. n/a 9
Page 11 Read page 11 as text
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, represent the fl ashlight’s location, and the number in the parenthesis be the total amount of time elapsed. The following sequence of moves solves the problem: (0) 1,2,5,10 1,2 (2) 5,10 2 (3) 1,5,10 2,5,10 (13) 1 5,10 (15) 1,2 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 di ffi culty here lies in computing sin 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 either. c. The formula says nothing about how to compute . 4. Algorithm Quadratic ( ) //The algorithm fi nds real roots of equation 2 + + = 0 //Input: Real coe ffi cients //Output: The real roots of the equation or a message about their absence if 6 = 0 ← ∗ − 4 ∗ ∗ if 0 ← 2 ∗ 1 ← ( − + ( )) 2 ← ( − − ( )) 10
Page 12 Read page 12 as text
return 1 2 else if = 0 return − (2 ∗ ) else return ‘ no real roots’ else // = 0 if 6 = 0 return − else // = = 0 if = 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 by 2: the remainder (0 or 1) will be the next (from right to left) digit of the binary representation in question. Replace by the quotient of the last division and repeat this operation until becomes 0. b. Algorithm Binary ( ) //The algorithm implements the standard method for fi nding //the binary expansion of a positive decimal integer //Input: A positive decimal integer //Output: The list − 1 1 0 of ’s binary digits ← 0 while 6 = 0 ← mod 2 ← b 2 c ← + 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 correct decimal digits. With this interpretation, the problem has in fi nitely 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 ( [0 − 1]) //Input: An array [ 0 .. − 1 ] of numbers //Output: The minimum distance between two of its elements 11
Page 13 Read page 13 as text
← ∞ for ← 0 to − 2 do for ← + 1 to − 1 do ← | [ ] − [ ] | if ← return A faster algorithm is based on the idea of presorting (see Section 6.1). 10. Pólya’s general four-point approach is: 1. Understand the problem 2. Devise a plan 3. Implement the plan 4. Look back/check 12
Page 14 Read page 14 as text
Exercises 1.3 1. Consider the algorithm for the sorting problem that sorts an array by counting, for each of its elements, the number of smaller elements and then uses this information to put the element in its appropriate position in the sorted array: Algorithm ComparisonCountingSort ( [0 − 1] , [0 − 1]) //Sorts an array by comparison counting //Input: Array [0 − 1] of orderable values //Output: Array [0 − 1] of ’s elements sorted in nondecreasing order for ← 0 to − 1 do [ ] ← 0 for ← 0 to − 2 do for ← + 1 to − 1 do if [ ] [ ] [ ] ← [ ] + 1 else [ ] ← [ ] + 1 for ← 0 to − 1 do [ [ ]] ← [ ] a. Apply this algorithm to sorting the list 60, 35, 81, 98, 14, 47. b. Is this algorithm stable? c. Is it in place? 2. Name the algorithms for the searching problem that you already know. Give a good succinct description of each algorithm in English. (If you know no such algorithms, use this opportunity to design one.) 3. Design a simple algorithm for the string-matching problem. 4. Königsberg bridges The Königsberg bridge puzzle is universally accepted as the problem that gave birth to graph theory. It was solved by the great Swiss-born mathematician Leonhard Euler (1707—1783). The problem asked whether one could, in a single stroll, cross all seven bridges of the city of Königsberg exactly once and return to a starting point. Following is a sketch of the river with its two islands and seven bridges: 13
Page 15 Read page 15 as text
a. State the problem as a graph problem. b. Does this problem have a solution? If you believe it does, draw such a stroll; if you believe it does not, explain why and indicate the small- est number of new bridges that would be required to make such a stroll possible. 5. Icosian Game A century after Euler’s discovery (see Problem 4), an- other famous puzzle–this one invented by the renown Irish mathemati- cian Sir William Hamilton (1805-1865)–was presented to the world under the name of the Icosian Game. The game was played on a circular wooden board on which the following graph was carved: Find a Hamiltonian circuit –a path that visits all the graph’s vertices exactly once before returning to the starting vertex–for this graph. 6. Consider the following problem: Design an algorithm to determine the best route for a subway passenger to take from one designated station to another in a well-developed subway system similar to those in such cities as Washington, D.C., and London, UK. a. The problem’s statement is somewhat vague, which is typical of real- life problems. In particular, what reasonable criterion can be used for de fi ning the “best” route? b. How would you model this problem by a graph? 7. a. Rephrase the traveling salesman problem in combinatorial object terms. b. Rephrase the graph-coloring problem in combinatorial object terms. 14
Page 16 Read page 16 as text
8. Consider the following map: a b c d e f a. Explain how we can use the graph-coloring problem to color the map so that no two neighboring regions are colored the same. b. Use your answer to part (a) to color the map with the smallest number of colors. 9. Design an algorithm for the following problem: Given a set of points in the Cartesian plane, determine whether all of them lie on the same circumference. 10. Write a program that reads as its inputs the ( ) coordinates of the endpoints of two line segments 1 1 and 2 2 and determines whether the segments have a common point. 15
483 more pages in the full document
Unlock it once and it stays in your library, ready to chat with or turn into flashcards.
Unlock the full documentMore Information Technology study materials
See all
Exam GuidesTest Bank for Managing and Using Information Systems: A Strategic Approach, 7th Edition (Chapters 1-13)
Information Technology
452 pages149downloadsFeb 2025
Exam GuidesTest Bank for Essentials of MIS, 12th Edition
Information Technology
342 pages146downloadsFeb 2025
Exam GuidesA+ Guide to Hardware, 9th Edition Test Bank
Information Technology
116 pages142downloadsFeb 2025
Textbook GuidesSolution Manual for Essentials of System Analysis and Design, 4th Edition
Information Technology
282 pages140downloadsFeb 2025
Textbook GuidesGuide To Networking Essentials, 7th Edition Solution Manual
Information Technology
48 pages139downloadsFeb 2025
Past Exams2023-2024 SOPHOS FIREWALL V19.5 Practice Exam with Answers (75 Solved Questions)
Information Technology
7 pages134downloadsFeb 2025
Class NotesLecture Notes for CCNP Enterprise: Core Networking (ENCOR) v8 Lab Manual, 2nd Edition
Information Technology
1089 pages123downloadsFeb 2025
Class NotesLecture Notes for Adobe XD Classroom in a Book (2020 release)
Information Technology
66 pages118downloadsFeb 2025
Study this document with CramX
Once it is in your library, every tool can work from it.

