The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998 The.Algorithm.Design.Manual.Springer-Verlag.1998

18.04.2013 Views

Square and Other Roots Next: Exercises Up: Divide and Conquer Previous: Binary Search Square and Other Roots The square root of n is the number r such that . Square root computations are performed inside every pocket calculator, but it is instructive to develop an efficient algorithm to compute square roots. First, observe that the square root of must be at least 1 and at most n. Let l=1 and r=n. Consider the midpoint of this interval, m=(l+r)/2. How does compare to n? If , then the square root must be greater than m, so the algorithm repeats with l=m. If , then the square root must be less than m, so the algorithm repeats with r=m. Either way, we have halved the interval with only one comparison. Therefore, after only rounds we will have identified the square root to within . This bisection method, as it is called in numerical analysis, can also be applied to the more general problem of finding the roots of an equation. We say that x is a root of the function f if f(x)=0. Suppose that we start with values l and r such that f(l) > 0 and f(r) < 0. If f is a continuous function, there must be a root between l and r. Depending upon the sign of f(m), where m=(l+r)/2, we can cut this window containing the root in half with each test and stop when our estimate becomes sufficiently accurate. Root-finding algorithms that converge faster than binary search are known for both of these problems. Instead of always testing the midpoint of the interval, these algorithms interpolate to find a test point closer to the actual root. Still, binary search is simple, robust, and works as well as possible without additional information on the nature of the function to be computed. Algorithms Mon Jun 2 23:33:50 EDT 1997 file:///E|/BOOK/BOOK2/NODE56.HTM [19/1/2003 1:28:59]

Exercises Next: Implementation Challenges Up: Breaking Problems Down Previous: Square and Other Roots Exercises 1. Consider the problem of storing n books on shelves in a library. The order of the books is fixed by the cataloging system and so cannot be rearranged. Therefore, we can speak of a book , where , that has a thickness and height . The length of each bookshelf at this library is L. Suppose all the books have the same height h (i.e. for all i, j) and the shelves are all separated by a distance of greater than h, so any book fits on any shelf. The greedy algorithm would fill the first shelf with as many books as we can until we get the smallest i such that does not fit, and then repeat with subsequent shelves. Show that the greedy algorithm always finds the optimal shelf placement, and analyze its time complexity. 2. (*) This is a generalization of the previous problem. Now consider the case where the height of the books is not constant, but we have the freedom to adjust the height of each shelf to that of the tallest book on the shelf. Thus the cost of a particular layout is the sum of the heights of the largest book on each shelf. ❍ Give an example to show that the greedy algorithm of stuffing each shelf as full as possible does not always give the minimum overall height. ❍ Give an algorithm for this problem, and analyze its time complexity. Hint: use dynamic programming. 3. (*) Consider a city whose streets are defined by an grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. Unfortunately, the city has bad neighborhoods, which are defined as intersections we do not want to walk in. We are given an matrix BAD, where BAD[i,j] = ``yes'' if and only if the intersection between streets i and j is somewhere we want to avoid. (a) Give an example of the contents of BAD such that there is no path across the grid avoiding bad neighborhoods. (b) Give an O( X Y ) algorithm to find a path across the grid that avoids bad neighborhoods. file:///E|/BOOK/BOOK2/NODE57.HTM (1 of 4) [19/1/2003 1:29:01]

Square and Other Roots<br />

Next: Exercises Up: Divide and Conquer Previous: Binary Search<br />

Square and Other Roots<br />

<strong>The</strong> square root of n is the number r such that . Square root computations are performed inside<br />

every pocket calculator, but it is instructive to develop an efficient algorithm to compute square roots.<br />

First, observe that the square root of must be at least 1 and at most n. Let l=1 and r=n. Consider the<br />

midpoint of this interval, m=(l+r)/2. How does compare to n? If , then the square root must be<br />

greater than m, so the algorithm repeats with l=m. If , then the square root must be less than m, so<br />

the algorithm repeats with r=m. Either way, we have halved the interval with only one comparison.<br />

<strong>The</strong>refore, after only rounds we will have identified the square root to within .<br />

This bisection method, as it is called in numerical analysis, can also be applied to the more general<br />

problem of finding the roots of an equation. We say that x is a root of the function f if f(x)=0. Suppose<br />

that we start with values l and r such that f(l) > 0 and f(r) < 0. If f is a continuous function, there must be<br />

a root between l and r. Depending upon the sign of f(m), where m=(l+r)/2, we can cut this window<br />

containing the root in half with each test and stop when our estimate becomes sufficiently accurate.<br />

Root-finding algorithms that converge faster than binary search are known for both of these problems.<br />

Instead of always testing the midpoint of the interval, these algorithms interpolate to find a test point<br />

closer to the actual root. Still, binary search is simple, robust, and works as well as possible without<br />

additional information on the nature of the function to be computed.<br />

<strong>Algorithm</strong>s<br />

Mon Jun 2 23:33:50 EDT 1997<br />

file:///E|/BOOK/BOOK2/NODE56.HTM [19/1/2003 1:28:59]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!