<<

A reprint from American Scientist the magazine of Sigma Xi, The Scientific Research Society

This reprint is provided for personal and noncommercial use. For any other use, please send a request Brian Hayes by electronic mail to [email protected]. Computing Science

Crinkly

Brian Hayes

n 1877 the german mathemati- If you were designing such a route, Ician made a shock- Some curves are you might start out with the kind of ing discovery. He found that a two- path that’s good for mowing a lawn: dimensional surface contains no more points than a one-dimensional line. so convoluted they Cantor compared the set of all points forming the area of a square with the wiggle free of the set of points along one of the line seg- But there’s a problem with these zig- ments on the perimeter of the square. one-dimensional world zags and spirals. A mathematical lawn He showed that the two sets are the mower cuts a vanishingly narrow same size. Intuition rebels against and fill up space swath, and so you have to keep re- this notion. Inside a square you could ducing the space between successive draw infinitely many parallel line seg- passes. Unfortunately, the limiting pat- ments side by side. Surely an area with invented, as well as curves that fill up tern when the spacing goes to zero is room for such an infinite array of lines a three-dimensional volume or even a not a filled area; it is a path that forever must include more points than a single region of some n-dimensional space. retraces the same line along one edge line—but it doesn’t. Cantor himself The very concept of dimension was of the square or around its perimeter, was incredulous: “I see it, but I don’t undermined. leaving the interior blank. believe it,” he wrote. Circa 1900, these space-filling curves The first successful recipe for a Yet the fact was inescapable. Cantor were viewed as mysterious aberra- space-filling was formulated defined a one-to-one correspondence tions, signaling how far mathematics in 1890 by Giuseppe­ Peano, an Ital- between the points of the square and had strayed from the world of every- ian mathematician also noted for his the points of the line segment. Every day experience. The mystery has nev- axioms of arithmetic. Peano did not point in the square was associated er entirely faded away, but the curves provide a diagram or even an explicit with a single point in the segment; ev- have grown more familiar. They are description of what his curve might ery point in the segment was matched playthings of programmers now, nice- look like; he merely defined a pair of with a unique point in the square. No ly adapted to illustrating certain algo- mathematical functions that give x and points were left over or used twice. rithmic techniques (especially recur- y coordinates inside a square for each It was like pairing up mittens: If you sion). More surprising, the curves have position t along a line segment. come out even at the end, you must turned out to have practical applica- Soon David Hilbert, a leading light have started with equal numbers of tions. They serve to encode geographic of German mathematics in that era, de- lefts and rights. information; they have a role in image vised a simplified version of Peano’s Geometrically, Cantor’s one-to-one processing; they help allocate resourc- curve and discussed its geometry. The mapping is a scrambled affair. Neigh- es in large computing tasks. And they illustration at the top of the opposite boring points on the line scatter to tickle the eye of those with a taste for page is a redrawing of a diagram widely separated destinations in the intricate geometric patterns. from Hilbert’s 1891 paper, showing square. The question soon arose: Is the first three stages in the construc- there a continuous mapping between How to Fill Up Space tion of the curve. a line and a surface? In other words, It’s easy to sketch a curve that com- can one trace a path through a square pletely fills the interior of a square. The Programming by Procrastination without ever lifting the pencil from the finished product looks like this: The lower illustration on the opposite paper and touch every point at least page shows a later stage in the evolu- once? It took a decade to find the first tion of the , when it has such curve. Then dozens more were become convoluted enough that one might begin to believe it will eventu- Brian Hayes is senior writer for American Scien- How uninformative! It’s not enough to ally reach all points in the square. The tist. Additional material related to the Comput- know that every point is covered by the curve was drawn by a computer pro- ing Science column appears at http://bit-player. passage of the curve; we want to see gram written in a recursive style that I org. Address: 11 Chandler St. #2, Somerville, MA how the curve is constructed and what call programming by procrastination. 02144. E-mail: [email protected] route it follows through the square. The philosophy behind the approach

178 American Scientist, Volume 101 © 2013 Brian Hayes. Reproduction with permission only. Contact [email protected]. is this: Plotting all those twisty turns 1 2 3 4 1 2 3 16 1 64 looks like a tedious job, so why not put it off as long as we can? Maybe we’ll 6 7 10 11 never have to face it. 2 3 Let us eavesdrop on a computer pro- gram named Hilbert as it mumbles to 5 8 9 12 itself while trying to solve this problem: 3 13 Hmm. I’m supposed to draw a 4 14 curve that fills a square. I don’t 1 4 2 3 1 2 15 16 know how to do that, but maybe 1 4 I can cut the problem down to 64 size. Suppose I had a subroutine A space-filling curve evolves through successive stages of refinement as it grows to cover the that would fill a smaller square, area of a square. This illustration is a redrawing of the first published diagram of such a curve; say one-fourth as large. I could the original appeared in an 1891 paper by David Hilbert. The idea behind the construction invoke that procedure on each is to divide a line segment into four intervals and divide a square into four quadrants, then quadrant of the main square, get- establish a correspondence between the points of corresponding intervals and quadrants. The ting back four separate pieces of process continues with further recursive subdivisions. the space-filling curve. Then, if I just draw three line segments to link the four pieces into one long curve, I’ll be finished! Of course I don’t actually have a subroutine for filling in a quad- rant. But a quadrant of a square is itself a square. There’s a program named Hilbert that’s supposed to be able to draw a space-filling curve in any square. I’ll just hand each of the quadrants off to Hilbert. The strategy described in this mono- logue may sound like a totally point- less exercise. The Hilbert program keeps subdividing the problem but has no plan for ever actually solving it. However, this is one of those rare and wonderful occasions when procrasti- nation pays off, and the homework as- signment you lazily set aside last night is miraculously finished when you get up in the morning. Consider the sizes of the successive subsquares in Hilbert’s divide-and- conquer process. At each stage, the side length of the square is halved, and the area is reduced to one-fourth. The limiting case, if the process goes on in- definitely, is a square of zero side length and zero area. So here’s the procrasti- nator’s miracle: Tracing a curve that After seven stages of elaboration the Hilbert curve meanders through 47 = 16,384 subdivisions of touches all the points inside a size-zero the square. The curve is an unbranched path with end points at the lower left and lower right. It square is easy, because such a square is is continuous in the sense that it has no gaps or jumps, but it is not smooth: All of the right angles in fact a single point. Just draw it! are points where the curve has no tangent (or, in terms of calculus, no derivative). Continuing the Practical-minded readers will object subdivision process leads to a limiting case where the curve fills the entire square, showing that a that a program running in a finite ma- two-dimensional square has no more points than a one-dimensional line segment. chine for a finite time will not actually reach the limiting case of squares that lustration above, I stopped the program curve, but a finite drawing like this one shrink away to zero size. I concede the after seven levels of , when the is at least a guide to the imagination. point. If the recursion is halted while squares were small but certainly larger There is one more important aspect the squares still contain multiple points, than a single point. The wiggly blue of this algorithm that I have glossed one of those points must be chosen as a line connects the centers of 47 = 16,384 over. If the curve is to be continuous— representative; the center of the square squares. Only in the mind’s eye will with no abrupt jumps—then all the is a likely candidate. In making the il- we ever see a true, infinite space-filling squares have to be arranged so that www.americanscientist.org © 2013 Brian Hayes. Reproduction with permission only. 2013 May–June 179 Contact [email protected]. one segment of the curve ends where A more recent invention is the “flow­ the next segment begins. Matching up snake” curve devised in the 1970s by ⇒ the end points in this way requires ro- . tating and reflecting some of the sub- Filling three-dimensional space squares. (For an animated illustration turns out to be even easier than filling of these transformations, see http:// the plane—or at least there are more bit-player.org/extras/hilbert.) ways to do it. Herman Haverkort of the Eindhoven Institute of Technol- Grammar and Arithmetic ogy in the Netherlands has counted ⇒ The procrastinator’s algorithm is the three-dimensional analogues of the certainly not the only way to draw a Hilbert curve; there are more than 10 space-filling curve. Another method million of them. exploits the self-similarity of the pat- tern—the presence of repeated motifs All Elbows that appear in each successive stage of In everyday speech the word curve ⇒ the construction. In the Hilbert curve suggests something smooth and fluid, the basic motif is a U-shaped path with without sharp corners, such as a pa- four possible orientations. In going rabola or a circle. The Hilbert curve from one stage of refinement to the is anything but smooth. All finite ver- next, each U orientation is replaced by sions of the curve consist of 90-degree a specific sequence of four smaller U bends connected by straight segments. ⇒ curves, along with line segments that In the infinite limit, the straight seg- link them together, as shown in the ments dwindle away to zero length, upper illustration at left. The substitu- leaving nothing but sharp corners. The tion rules form a grammar that gener- curve is all elbows. In 1900 the Ameri- grammar ates geometric figures in the same way can mathematician Eliakim Hastings ∩ ⇒ ⊃ ↑ ∩ → ∩ ↓ ⊂ that a linguistic grammar generates Moore came up with the term “crinkly phrases and sentences. curves” for such objects. ⊃ ⇒ ∩ → ⊃ ↑ ⊃ ← ∪ The output of the grammatical pro- In many respects these curves are ⊂ ⇒ ∪ ← ⊂ ↓ ⊂ → ∩ cess is a sequence of symbols. An easy reminiscent of , the objects of ∪ ⇒ ⊂ ↓ ∪ ← ∪ ↑ ⊃ way to turn it into a drawing is to in- fractional dimension that Benoit Man- terpret the symbols as commands in delbrot made famous. The curves’ self- Substitution rules generate the Hilbert curve the language of “turtle graphics.” The similarity is -like: Zooming in re- by replacing a U-shaped motif in any of four orientations with sequences of four rotated turtle is a conceptual drawing instru- veals ever more intricate detail. But the and reflected copies of the same motif. The ment, which crawls over the plane Hilbert curve is not a fractal, because rules constitute a grammar. in response to simple instructions to its dimension is not a fraction. Any move forward, turn left or turn right. finite approximation is simply a one- The turtle’s trail across the surface be- dimensional line. On passing to the 0.0 0.1 0.2 0.3 1. 0 comes the curve to be drawn. limit of infinite crinkli­ness, the curve When Peano and Hilbert were writ- suddenly becomes a two-dimensional 0.20 0.21 0.22 0.23 0.30 ing about the first space-filling curves, square. There is no intermediate state. they did not explain them in terms of Even though the complete path of 0.210 0.211 0.212 0.213 0.220 grammatical rules or turtle graphics. an infinite space-filling curve cannot Instead their approach was numeri- be drawn on paper, it is still a perfectly 0.213... cal, assigning a number in the interval well-defined object. You can calculate [0, 1] to every point on a line segment the location along the curve of any and also to every point in a square. For specific point you might care to know 0.21 0.22 the Hilbert curve, it’s convenient to do about. The result is exact if the input is this arithmetic in base 4, or quaternary, exact. A few landmark points for the working with the digits 0, 1, 2, 3. In a Hilbert curve are plotted in the lower 0.1 0.20 0.23 quaternary fraction such as 0.213, each illustration on the opposite page. successive digit specifies a quadrant or The algorithm for this calculation subquadrant of the square, as outlined implements the definition of the curve in the lower illustration at left. as a mapping from a one-dimensional What about other space-filling line segment to a two-dimensional curves? Peano’s curve is conceptual- square. The input to the function is a ly similar to Hilbert’s but divides the number in the interval [0, 1], and the 0.3 0.0 square into nine regions instead of four. output is a pair of x, y coordinates. Base-4 encoding of the Hilbert curve shows Another famous example was invented The inverse mapping—from x, y how fourfold divisions of the unit interval [0, 1] in 1912 by the Polish mathematician coordinates to the segment [0, 1]—is are mapped onto quadrants of the square. For Wacław Sierpin´ski; it partitions the more troublesome. The problem is that example, any base-4 number beginning 0.213 square along its diagonals, forming tri- a point in the square can be linked to must lie in the small square outlined in blue. angles that are then further subdivided. more than one point on the line.

180 American Scientist, Volume 101 © 2013 Brian Hayes. Reproduction with permission only. Contact [email protected]. The first space-filling curve (left) was described in 1890 by Italian mathematician Giuseppe Peano; the construction divides a square into nine smaller squares. A curve based on a triangular dissection (center) was introduced in 1912 by Polish mathematician Wacław Sierpin´ski. The “flowsnake” curve (right), invented by American mathematician Bill Gosper in the 1970s, fills a ragged-edged hexagonal area.

Cantor’s dimension-defying func- manager looked up the map coordinates card. Sorting the cards by index number tion was a one-to-one mapping: Each of each address, then looked up those yielded the delivery sequence. point on the line was associated with coordinates in a table, which supplied an Behind the scenes in this procedure exactly one point in the square, and index number to write on the Rolodex was a space-filling curve (specifically, vice versa. But Cantor’s mapping was not continuous: Adjacent points on the 1.000 1 2 5 7 3 2 line did not necessarily map to adja- 3 5 12 12 5 3 cent points in the square. In contrast, the space-filling curves are continu- ous but not one-to-one. Although each 0.875 point on the line is associated with a unique point in the square, a point in 3 5 the square can map back to multiple 0.750 8 8 points on the line. A conspicuous ex- ample is the center of the square, with the coordinates x = ½, y = ½. Three sep- 0.625 arate locations on the line segment (1∕6, ½ and 5∕6) all connect to this one point 1 2 in the square. 1 3 0.500 4 1 5 4 6 6 Math on Wheels 1 4 Space-filling curves have been called 5 5 monsters, but they are useful monsters. 0.375 One of their most remarkable applica- tions was reported 30 years ago by John 1 7 J. Bartholdi III and his colleagues at the 0.250 8 8 Georgia Institute of Technology. Their aim was to find efficient routes for driv- ers delivering Meals on Wheels to el- 1 31 derly clients scattered around the city 0.125 32 32 of Atlanta. Finding the best possible de- 1 1 11 15 livery sequence would be a challenging 0 16 12 12 16 1 task even with a powerful computer. 0.000 Meals on Wheels didn’t need the so- 0.000 0.125 0.250 0.375 0.500 0.625 0.750 0.875 1.000 lution to be strictly optimal, but they 1 1 1 1 1 1 1 1 3 2 5 1 7 3 5 2 3 4 5 7 1115 31 needed to plan and revise routes quick- 0 32 1612 8 6 5 4 3 8 512 2 12 5 8 3 4 5 6 8 1216 32 1 ly, and they had to do it with no com- Positions of points along the infinitely crinkled course of the Hilbert curve can be calculated puting hardware at all. Bartholdi and exactly, even though the curve itself cannot be drawn. Here 25 selected points in the interval his coworkers came up with a scheme [0,1] are mapped to coordinates in the unit square, [0,1]2. The points are color coded according that used a map, a few pages of printed to the largest prime factor of their denominator: red for 2, green for 3, blue for 5. Although a tables and two Rolodex files. finite approximation to the Hilbert curve is shown in the background, the positions within Planning a route started with Rolodex the square are those along the completed, infinite curve. The inverse mapping is not unique: cards listing the delivery addresses. The Points in the square map back to multiple points in the interval. www.americanscientist.org © 2013 Brian Hayes. Reproduction with permission only. 2013 May–June 181 Contact [email protected]. a finite approximation to a Sierpin´ski is usually good. For randomly distrib- ods rely on arrays of dots that vary in curve) that had been superimposed uted locations, the tours average about size to represent lighter and darker re- on the map. The index numbers in 25 percent longer than the optimum. gions. Both random and regular arrays the tables encoded position along this Other heuristic methods can beat this tend to blur fine features and sharp curve. The delivery route didn’t follow performance, but they are much more lines in an image. A halftone pattern the Sierpin´ski curve, with all its crinkly complicated. The Bartholdi method that groups the dots along the path of turns. The curve merely determined finds a route without even computing a Hilbert or can provide the sequence of addresses, and the the distances between sites. smooth tonal gradients while preserv- driver then chose the shortest point-to- Locality is a helpful property in ing crisp details. point route between them. other contexts as well. Sometimes Another application comes from a A space-filling curve works well in what’s needed is not a route from one quite different realm: the multiplica- this role because it preserves “local- site to the next but a grouping of sites tion of matrices (a critical step in large- ity.” If two points are nearby on the into clusters. In two or more dimen- scale computations). Accessing matrix plane, they are likely to be nearby on sions, identifying clusters can be dif- elements by rows and columns requires the curve as well. The route makes no ficult; threading a space-filling curve the same values to be read from memo- wasteful excursions across town and through the data set reduces it to a ry multiple times. In 2006 Michael Bad- back again. one-dimensional problem. er and Christoph Zenger of the Techni- The Meals on Wheels schedul- The graphic arts have enlisted the cal University of Munich showed that ing task is an instance of the travel- help of space-filling curves for a pro- clustering the data with a space-filling ing salesman problem, a notorious cess known as halftoning, which al- curve reduces memory traffic. stumper in computer science. The lows black-and-white devices (such as Bader is also the author of an excel- Bartholdi algorithm gives a solution laser printers) to reproduce shades of lent recent book that discusses space- that is not guaranteed to be best but gray. Conventional halftoning meth- filling curves from a computational point of view. An earlier volume by Hans Sagan is more mathematical. Given that people have found such a surprising variety of uses for these curious curves, I can’t help wondering whether nature has also put them to work. Other kinds of patterns are ev- erywhere in the natural world: stripes, spots, spirals and many kinds of branching structures. But I can’t recall seeing a Peano curve on the landscape. The closest I can come are certain trace fossils (preserved furrows and bur- rows of organisms on the sea floor) and perhaps the ridges and grooves on the surface of the human cerebrum.

Cantor’s Conundrums Applications of space-filling curves are necessarily built on finite examples— paths one can draw with a pencil or a computer. But in pure mathematics the focus is on the infinite case, where a line gets so incredibly crinkly that it suddenly becomes a plane. Cantor’s work on infinite sets was controversial and divisive in his own time. Leopold Kronecker, who had been one of Cantor’s professors in Berlin, later called him “a corrupter of youth” and tried to block publication of the paper on dimension. But Can- Approximate solutions to the traveling salesman problem emerge from a simple algorithm tor had ardent defenders, too. Hilbert based on space-filling curves. Here 25 cities (black dots) are randomly distributed within a wrote in 1926: “No one shall expel us square. The traveling salesman problem calls for the shortest tour that passes through all from the paradise that Cantor has cre- the cities and returns to the starting point. Listing the cities in the order they are visited by a space-filling curve yields a path of length 274 (green line); the optimal tour (red line) is about 13 ated.” Indeed, no one has been evicted. percent better, with a length of 239. The space-filling curve used in this example was invented (A few have left of their own volition.) by E. H. Moore in 1900; it is related to the Hilbert curve but forms a closed circuit. The unit of Cantor’s discoveries eventually led to distance for measuring tours is the step size of the . The optimal tour was com- clearer thinking about the nature of con- puted with the Concorde TSP Solver (http://www.tsp.gatech.edu/concorde.html). tinuity and smoothness, concepts at the

182 American Scientist, Volume 101 © 2013 Brian Hayes. Reproduction with permission only. Contact [email protected]. root of calculus and analysis. The relat- ed development of space-filling curves called for a deeper look at the idea of dimension. From the time of Descartes, it was assumed that in d-dimensional­ space it takes d coordinates to state the location of a point. The Peano and Hil- bert curves overturned this principle: A single number can define position on a line, on a plane, in a solid, or even in those 11-dimensional spaces so fashion- Summer Mountain Lightning & Some Music able in high-energy physics. At about the same time that Cantor, Tesla, maker of homemade lightning, put sparks Peano and Hilbert were creating their in a wall by mistake. He never crinkly curves, the English schoolmas- had much of a lover & now they’ve named ter Edwin Abbott was writing his fable a car for him; & now a tanager in the pine Flatland, about two-dimensional crea- has perched upright tures that dream of popping out of the to put itself in danger for a mate. plane to see the world in 3D. The Flat- landers might be encouraged to learn If, like a fire, that sound had three sides, that mere one-dimensional worms can if like a point, a flame, it would be break through to higher spaces just by pure geometry; such objects that strike you wiggling wildly enough. as beautiful, you cannot name. Tesla moved to the mountains, began Bibliography shooting rays, sexual Es the hawk gave back, Bader, M. 2013. Space-Filling Curves: An Intro- into the abstract—days adept duction with Applications in Scientific Comput- at non-nothingness—far past a life & its shape. ing. Berlin: Springer. Bartholdi, J. J. III, L. K. Platzman, R. L. Col- The great resister lins and W. H. Warden III. 1983. A minimal technology routing system for Meals on stays in you, plodding, then Wheels. Interfaces 13(3):1–8. the blind harpist plays. Between magnetic poles, Dauben, J. W. 1979. Georg Cantor: His Math- they place a motor made of money ematics and Philosophy of the Infinite. Cam- to drive the horror of an age—& daily, bridge: Harvard University Press. these unmanageable patterns, & weekly, Gardner, M. 1976. Mathematical games: In the magnificent ordinary. which “monster” curves force redefinition of the word “curve.” Scientific American 235:124–133. The next thing you make will be different. Hilbert, D. 1891. Über die stetige Abbildung You stand in the field not yet being einer Linie auf ein Flächenstück. Math- struck, talking to nothing, jagged ematische Annalen 38:459–460. & unsure. You knew this Moore, E. H. 1900. On certain crinkly curves. when you started the experiment; you wanted Transactions of the American Mathematical So- ciety 1(1):72–90. to be changed & you were— Null, A. 1971. Space-filling curves, or how to waste time with a plotter. Software: Practice and Experience 1:403–410. —Brenda Hillman Peano, G. 1890. Sur une courbe, qui remplit toute une aire plane. Mathematische Annalen 36:157–160. Platzman, L. K., and J. J. Bartholdi III. 1989. Spacefilling curves and the planar travel- ling salesman problem. Journal of the Asso- ciation for Computing Machinery 36:719–737. Sagan, H. 1991. Some reflections on the emer- gence of space-filling curves: The way it could have happened and should have happened, but did not happen. Journal of the Franklin Institute 328:419–430. Sagan, H. 1994. Space-Filling Curves. New York: Springer-Verlag. Sierpin´ski, W. 1912. Sur une nouvelle courbe continue qui remplit toute une aire plane. Bulletin de l’Académie des Sciences de Cracovie, Série A, 462–478. Velho, L., and J. de Miranda Gomes. 1991. Digital halftoning with space filling curves. Computer Graphics 25(4):81–90. www.americanscientist.org © 2013 Brian Hayes. Reproduction with permission only. 2013 May–June 183 Contact [email protected].