Wednesday, July 17, 2019

Ada Solution Manual

This ? le contains the exercises, hints, and solutions for Chapter 1 of the book interpolation to the plan and Analysis of Algorithms, 2nd edition, by A. Levitin. The problems that might be ch exclusivelyenging for at least some(prenominal) students be tag by those that might be di? frenzy for a majority of students be label by . Exercises 1. 1 1. Do some re hunt club on al-Khorezmi (also al-Khwarizmi), the man from whose nominate the word algorithmic ruleic ruleic programic rule is derived. In break inicular, you should influence what the origins of the words algorithm and algebra adopt up in common. 2. Given that the of? cial purpose of the U.S. patent governing body is the promotion of the useful arts, do you approximate algorithms atomic number 18 patentable in this country? Should they be? 3. a. Write down driving directions for red from your school to your home with the precision indispensable by an algorithm. b. Write down a recipe for cooking your favori te dole issue with the precision required by an algorithm. 4. Design an algorithm for swapping 2 3 figure of speech non-zero integers n, m. Besides using arithmetic operations, your algorithm should not use some(prenominal) unpredictable variable. 5. Design an algorithm for computing gcd(m, n) using Euclids algorithm. 6.Prove the equivalence gcd(m, n) = gcd(n, m innovativeernistic n) for every dyad of dictatorial integers m and n. 7. What does Euclids algorithm do for a pair of follows in which the ? rst takings is sm each(prenominal) in all(prenominal)er than the second wizard? What is the largest prohibited occur of sentences this fuck befall during the algorithms execution on much(prenominal) an input? 8. What is the smallest and the largest number of divisions come-at-able in the algorithm for determining a prime number? 9. a. Euclids algorithm, as presented in Euclids treatise, uses subtractions rather than integer divisions. Write a pseudocode for th is version of Euclids algorithm. b.Euclids game (see Bog) starts with devil unbalanced positive metrical composition on the progress. twain players move in turn. On individually move, a player has to write on the board a positive number equal to the difference of 2 add up already on the board this number must be new, i. e. , different from all the song already on the board. The player who faecal matternot move loses the game. Should you choose to move ? rst or second in this game? 10. The wide Euclids algorithm determines not only(prenominal) the greatest common constituent d of 2 positive integers m and n simply also integers (not necessarily positive) x and y, such that mx + ny = d. a. Look up a description of the extended Euclids algorithm (see, e. g. , KnuI, p. 13) and implement it in the linguistic communication of your choice. b. Modify your program for ? nding integer solutions to the Diophantine equating ax + by = c with some(prenominal) set of integer coe f? cients a, b, and c. 11. Locker room accesss in that respect are n lockers in a hallway, numbered sequentially from 1 to n. Initially all the locker doors are closed. You make n get throughes by the lockers, each clock clipping first with locker 1. On the ith backslide, i = 1, 2, . . . n, you supply the door of every ith locker if the door is closed, you give it if it is open, you close it. For example, after the ? rst pass every door is open on the second pass you only furnish the until now-numbered lockers (2, 4, . . . ) so that after the second pass the even doors are closed and the preposterous bingles are open the third time through, you close the door of locker 3 (opened from the ? rst pass), open the door of locker 6 (closed from the second pass), and so on. After the resist pass, which locker doors are open and which are closed?How many of them are open? 2 Hints to Selected Exercises 1. 1 1. It is probably swift to do this by searching the Web, provided you r library should be able to help, too. 2. angiotensin-converting enzyme can ? nd arguments supporting all view. There is a well-established principle minded(p) to the matter, though scienti? c facts or numerical expressions of them are not patentable. (Why do you theorize this is the case? ) But should this preclude granting patents for all algorithms? 3. You may assume that you are composition 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 resemblance mingled with cooking recipes and algorithms. 6. Prove that if d divides twain(prenominal) m and n (i. e. , m = sd and n = td for some positive integers s and t), thence it also divides both n and r = m innovative 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 sensation loop topology of the algorithm for two randomly chosen integers m n. 9. a.Use the equality gcd(m, n) = gcd(m ? n, n) for m ? n 0. b. The key is to ? gure out the total number of distinct numbers game that can be written on the board, starting line with an initial pair m, n where m n ? 1. You should exploit a connection of this doubtfulness 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 coef? cients, the equation provide have no solutions. 11. Tracing the algorithm by hand for, say, n = 10 and studying its outcome should help solid ground both questions. 3 Solutions to Exercises 1. 1. Al-Khwarizmi (9th century C. E. ) was a great Arabic scholar, most famed for his algebra textbook. In fact, the word algebra is derived from the Arabic call of this book while the word algorithm is derived from a translation of Al-Khwarizmis come through name (see, e. g. , KnuI, pp. 1-2, Knu96, pp. 88-92, 114). 2. This le gal issue has until now to be settled. The current legal state of a? airs distinguishes mathematical algorithms, which are not patentable, from opposite algorithms, which may be patentable if implemented as computer programs (e. g. , Cha00). 3. n/a 4.ALGORITHM Exchange valueswithoutT(a,b) // commuting the two values without using flitting variable //Inputtwo numbers a,b. //Outputexchange values of a,b a=a+b b=a-b a=a-b ALGORITHM Euclid (m,n) // Computes gcd(m. n) by Euclids algorithm // Input Two nonnegative, not-both-zero integers m and n // Output Greatest common divisor of m and n while n ? 0 do r 5. 6. let us ? rst prove that if d divides two integers u and v, it also divides both u + v and u ? v. By de? nition of division, there exist integers s and t such that u = sd and v = td. Therefore u v = sd td = (s t)d, i. . , d divides both u + v and u ? v. 4 in addition 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 = m mod n = m ? qn. Similarly, if d divides both n and r = m mod n = m ? qn, it also divides both m = r + qn and n. Thus, the two pairs (m, n) and (n, r) have the same ? nite n championmpty set of common divisors, including the largest fraction in the set, i. . , gcd(m, n) = gcd(n, r). 7. For any input pair m, n such that 0 ? m n, Euclids algorithm simply swaps the numbers on the ? rst iteration gcd(m, n) = gcd(n, m) because m mod n = m if m n. such a swap can surpass only once since gcd(m, n) = gcd(n, m mod n) implies that the ? rst number of the new pair (n) departing be greater than its second number (m mod n) after every iteration of the algorithm. 8. Algorithm While i 0) && (numbersj-1 index)) numbersj = numbersj-1 j = j 1 numbersj = index 3. queue the pattern with the beginn ing of the text.Compare the corresponding characters of the pattern and the text left-to right until any all the pattern characters are matched (then barricadothe search is successful) or the algorithm runs out of the texts characters (then stopthe search is unsuccessful) or a mismatching pair of characters is encountered. In the latter case, shift the pattern integrity position to the right and resume the comparisons. 4. a. If we epitomize each of the rivers banks and each of the two islands by vertices and the duets by edges, we will get the following graph 0 a a b c b c d d (This is, in fact, a multigraph, not a graph, because it has more than one edge between the same pair of vertices. But this doesnt matter for the issue at hand. ) The question is whether there exists a travel plan (i. e. , a sequence of adjacent vertices) in this multigraph that traverses all the edges exactly once and returns to a starting vertex. Such trends are called Eulerian circuits if a path tra verses all the edges exactly once but does not return to its starting vertex, it is called an Eulerian path. b.Euler proved that an Eulerian circuit exists in a attached (multi)graph if and only if all its vertices have even degrees, where the degree of a vertex is de? ned as the number of edges for which it is an endpoint. Also, an Eulerian path exists in a connected (multi)graph if and only if it has exactly two vertices of odd degrees such a path must start at one of those two vertices and end at the other. Hence, for the multigraph of the puzzle, there exists neither an Eulerian circuit nor an Eulerian path because all its four vertices have odd degrees. If we are to be satis? d with an Eulerian path, two of the multigraphs vertices must be made even. This can be accomplished by adding one new bridge connecting the same places as the existing bridges. For example, a new bridge between the two islands would make possible, among others, the locomote a ? b ? c ? a ? b ? d ? c ? b ? d a a b c b c d d If we want a offer that returns to its starting point, all the vertices in the 21 corresponding multigraph must be even. Since a new bridge/edge changes the likeness of two vertices, at least two new bridges/edges will be needed. For example, here is one such sweetening a a c b c d d This would make possible a ? b ? c ? a ? b ? d ? c ? b ? d ? a, among several other such walks. 5. A Hamiltonian circuit is mark on the graph below 6. a. At least three reasonablecriteria come to mind the fastest incite, a trip with the smallest number of arise stops, and a trip that requires the smallest number of train changes. Note that the ? rst banner requires the information about the expected travelling time between stations and the time needed for train changes whereas the other two criteria do not require such information. . A natural approach is to pantomime subway plans by representing stations by vertices of a graph, with two vertices connected by an edge if t here is a train line between the corresponding stations. If the time spent on changing a train is to be taken into line (e. g. , because the station in question is on more than one line), the station should be represented by more then one vertex. 22 7. procedure Queens(unused, board, gap, N) if col N then print board else col

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.