Download Design Algorithm and Analysis and more Exercises Algorithms and Programming in PDF only on Docsity!
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 B; those that might be difficult for a majority of students are marked by I
Exercises 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.
- 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?
- 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.
- 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.
- Design an algorithm to find 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?
- 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{ } down to gcd( )
- B Prove the equality gcd( ) = gcd( mod ) for every pair of positive integers and .
- What does Euclid’s algorithm do for a pair of integers in which the first is smaller than the second? What is the maximum number of times this can happen during the algorithm’s execution on such an input?
- a. What is the minimum number of divisions made by Euclid’s algorithm among all inputs 1 ≤ ≤ 10?
b. What is the maximum number of divisions made by Euclid’s algorithm among all inputs 1 ≤ ≤ 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 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?
- 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 find integer solutions to the Diophantine equation + = with any set of integer coefficients , , and .
- 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?
Solutions to Exercises 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]).
- 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]).
- n/a
- 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 first 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).
- Initialize the list of common elements to empty. Starting with the first 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 first positive odd numbers and the first positive even numbers, is equal to + − 1
- 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.
- Let us first prove that if divides two integers and it also divides both + and − . By definition 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 finite nonempty set of common divisors, including the largest element in the set, i.e., gcd( ) = gcd( )
- For any input pair such that 0 ≤ Euclid’s algorithm simply swaps the numbers on the first iteration:
gcd( ) = gcd( )
because mod = if Such a swap can happen only once since gcd( ) = gcd( mod ) implies that the first number of the new pair () will be greater than its second number ( mod ) after every iteration of the algorithm.
- 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 blog(3−))c where = (1 +
Exercises 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.)
- 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 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. (Note: According to a rumor on the Internet, interviewers at a well-known software company located near Seattle have given this problem to interviewees.)
- 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. = 12 sin where is the angle between sides and
c. = 12 where is the height to base
- Write pseudocode for an algorithm for finding real roots of equation ^2 + + = 0 for arbitrary real coefficients and (You may assume the availability of the square root function ())
- Describe the standard algorithm for finding the binary representation of a positive decimal integer
a. in English.
b. in pseudocode.
- Describe the algorithm used by your favorite ATM machine in dispensing cash. (You may give your description in either English or pseudocode, whichever you find more convenient.)
- 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.
- 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?
- Consider the following algorithm for finding 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.
- One of the most influential 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 different?
Solutions to Exercises 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).
- Let 1, 2, 5, 10 be labels representing the men of the problem, 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:
- 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 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 .
- Algorithm Quadratic( ) //The algorithm finds real roots of equation ^2 + + = 0 //Input: Real coefficients //Output: The real roots of the equation or a message about their absence if 6 = 0 ← ∗ − 4 ∗ ∗ if 0 ← 2 ∗ 1 ← (− + ()) 2 ← (− − ())
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.
- 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 finding //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
- n/a
- 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 infinitely many instances.
- n/a
- 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
Exercises 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?
- 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.)
- Design a simple algorithm for the string-matching problem.
- 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:
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.
- 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.
- 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 defining the “best” route?
b. How would you model this problem by a graph?
- a. Rephrase the traveling salesman problem in combinatorial object terms.
b. Rephrase the graph-coloring problem in combinatorial object terms.
Hints to Exercises 1.
- Trace the algorithm on the input given. Use the definitions of stability and being in-place that were introduced in the section.
- If you do not recall any searching algorithms, you should design a simple searching algorithm (without succumbing to the temptation to find one in the latter chapters of the book).
- This algorithm is introduced later in the book, but you should have no trouble to design it on your own.
- If you have not encountered this problem in your previous courses, you may look up the answers on the Web or in a discrete structures textbook. The answers are, in fact, surprisingly simple.
- No efficient algorithm for solving this problem for an arbitrary graph is known. This particular graph does have Hamiltonian circuits that are not difficult to find. (You need to find just one of them.)
- a. Put yourself (mentally) in a passenger’s place and ask yourself what criterion for the “best” route you would use. Then think of people that may have different needs.
b. The representation of the problem by a graph is straightforward. Give some thoughts, though, to stations where trains can be changed.
- a. What are tours in the traveling salesman problem?
b. It would be natural to consider vertices colored the same color as elements of the same subset.
- Create a graph whose vertices represent the map’s regions. You will have to decide on the edges on your own.
- Assume that the circumference in question exists and find its center first. Also, do not forget to give a special answer for ≤ 2
- Be careful not to miss some special cases of the problem.
Solutions to Exercises 1.
- a. Sorting 60, 35, 81, 98, 14, 47 by comparison counting will work as follows:
Array [05] 60 35 81 98 14 47
Initially [] 0 0 0 0 0 0 After pass = 0 [] 3 0 1 1 0 0 After pass = 1 [] 1 2 2 0 1 After pass = 2 [] 4 3 0 1 After pass = 3 [] 5 0 1 After pass = 4 [] 0 2 Final state [] 3 1 4 5 0 2
Array [05] 14 35 47 60 81 98
b. The algorithm is not stable. Consider, as a counterexample, the result of its application to 10 , 100
c. The algorithm is not in place because it uses two extra arrays of size : and
- Answers may vary but most students should be familiar with sequential search, binary search, binary tree search and, possibly, hashing from their introductory programming courses.
- Align the pattern with the beginning of the text. Compare the corre- sponding characters of the pattern and the text left-to right until either all the pattern characters are matched (then stop–the search is success- ful) or the algorithm runs out of the text’s characters (then stop–the search is unsuccessful) or a mismatching pair of characters is encountered. In the latter case, shift the pattern one position to the right and resume the comparisons.
- a. If we represent each of the river’s banks and each of the two islands by
the walk − − − − − − − −
b
a
c
d
b
a
c
d
If we want a walk that returns to its starting point, all the vertices in the corresponding multigraph must be even. Since a new bridge/edge changes the parity of two vertices, at least two new bridges/edges will be needed. For example, here is one such “enhancement”:
b
a
c
d
b
a
c
d
This would make possible − − − − − − − − − , among several other such walks.
- A Hamiltonian circuit is marked on the graph below:
- a. At least three “reasonable”criteria come to mind: the fastest trip, a trip with the smallest number of train stops, and a trip that requires the smallest number of train changes. Note that the first criterion requires the information about the expected traveling time between stations and the time needed for train changes whereas the other two criteria do not require such information.
b. A natural approach is to mimic subway plans by representing sta- tions by vertices of a graph, with two vertices connected by an edge if there is a train line between the corresponding stations. If the time spent on changing a train is to be taken into account (e.g., because the station in question is on more than one line), the station should be represented by more then one vertex.
- a. Find a permutation of given cities for which the sum of the distances between consecutive cities in the permutation plus the distance between its last and first city is as small as possible.
b. Partition all the graph’s vertices into the smallest number of disjoint subsets so that there is no edge connecting vertices from the same subset.
- a. Create a graph whose vertices represent the map’s regions and the edges connect two vertices if and only if the corresponding regions have a common border (and therefore cannot be colored the same color). Here