The.Algorithm.Design.Manual.Springer-Verlag.1998
The.Algorithm.Design.Manual.Springer-Verlag.1998 The.Algorithm.Design.Manual.Springer-Verlag.1998
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]
- Page 163 and 164: War Story: Stripping Triangulations
- Page 165 and 166: War Story: Mystery of the Pyramids
- Page 167 and 168: War Story: Mystery of the Pyramids
- Page 169 and 170: War Story: String 'em Up We were co
- Page 171 and 172: War Story: String 'em Up Figure: Su
- Page 173 and 174: Exercises Next: Implementation Chal
- Page 175 and 176: Exercises used to select the pivot.
- Page 177 and 178: Dynamic Programming Next: Fibonacci
- Page 179 and 180: Fibonacci numbers Next: The Partiti
- Page 181 and 182: Fibonacci numbers Next: The Partiti
- Page 183 and 184: The Partition Problem . What is the
- Page 185 and 186: The Partition Problem Figure: Dynam
- Page 187 and 188: Approximate String Matching Next: L
- Page 189 and 190: Approximate String Matching The val
- Page 191 and 192: Longest Increasing Sequence Will th
- Page 193 and 194: Minimum Weight Triangulation Next:
- Page 195 and 196: Limitations of Dynamic Programming
- Page 197 and 198: War Story: Evolution of the Lobster
- Page 199 and 200: War Story: Evolution of the Lobster
- Page 201 and 202: War Story: What's Past is Prolog Ne
- Page 203 and 204: War Story: What's Past is Prolog th
- Page 205 and 206: War Story: Text Compression for Bar
- Page 207 and 208: War Story: Text Compression for Bar
- Page 209 and 210: Divide and Conquer Next: Fast Expon
- Page 211 and 212: Fast Exponentiation Next: Binary Se
- Page 213: Binary Search Next: Square and Othe
- Page 217 and 218: Exercises whose denominations are ,
- Page 219 and 220: The Friendship Graph Next: Data Str
- Page 221 and 222: The Friendship Graph friendship gra
- Page 223 and 224: Data Structures for Graphs linked t
- Page 225 and 226: War Story: Getting the Graph ``Well
- Page 227 and 228: Traversing a Graph Next: Breadth-Fi
- Page 229 and 230: Breadth-First Search Next: Depth-Fi
- Page 231 and 232: Depth-First Search Next: Applicatio
- Page 233 and 234: Depth-First Search Next: Applicatio
- Page 235 and 236: Connected Components Next: Tree and
- Page 237 and 238: Two-Coloring Graphs Next: Topologic
- Page 239 and 240: Topological Sorting Next: Articulat
- Page 241 and 242: Modeling Graph Problems Next: Minim
- Page 243 and 244: Modeling Graph Problems good line s
- Page 245 and 246: Minimum Spanning Trees ● Prim's A
- Page 247 and 248: Prim's Algorithm inserted edge (x,y
- Page 249 and 250: Kruskal's Algorithm a tree of weigh
- Page 251 and 252: Dijkstra's Algorithm Next: All-Pair
- Page 253 and 254: All-Pairs Shortest Path Next: War S
- Page 255 and 256: War Story: Nothing but Nets Next: W
- Page 257 and 258: War Story: Nothing but Nets ``You a
- Page 259 and 260: War Story: Dialing for Documents Ne
- Page 261 and 262: War Story: Dialing for Documents If
- Page 263 and 264: War Story: Dialing for Documents CO
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]