YOU CAN SWIM ALL DAY IN THE SEA OF KNOWLEDGE AND NOT GET WET.
PREFACE
D id you know that Richard Feynman started developing the equations that won him the Nobel Prize after seeing someone throw a plate in the air? Or that John von Neumann modeled parts of his electronic computer on a friends idea about how memories are stored in the human brain? Or that the sight of a kicking and screaming orangutan at the zoo led Charles Darwin to his big idea? What Feynman, von Neumann, Darwin, and others have in common is that they see physics and mathematics and science everywhere, way beyond the confines of their laboratories.
Even if youre not gunning for a Nobel Prize, you probably do things in your everyday life that can be modeled as algorithms. In fact, you apply them on a daily basis to solve various problems: finding pairs of socks in a pile of clothes, deciding when to go to the grocery store, determining how to prioritize your tasks for the day, and so on. An algorithm is a series of unambiguous steps that achieves some meaningful objective in finite time. The series might begin with some input and is expected to produce an output. Those are an algorithms characteristics. Whats fascinating is that Babylonian tablets from around 1800 to 1600 BCE reveal that ancient Babylonians wrote down their procedures for determining things like, say, compound interest or the width and length of a cistern given its height and volume using algorithms. That is to say, their procedures were made up of an unambiguous series of steps; they had some input, some output, they eventually terminated and they were useful. Algorithms can thus be found in the works of various contributors to mathematics over the centuries. After the advent of computers, these characteristics have proved crucial because they allow computers to carry out tasks in a way that is predictable.
Despite the importance of algorithms in our lives, texts on the subject tend to focus largely on intricate detailsthe howwhile perhaps ignoring the more practical lessons of those algorithms that make them appealing. The seemingly simple everyday tasks we just mentioned can be undertaken in a number of different ways. The more aware we are of those ways, the better we can hone our ability to achieve a task in the most efficient way. Think of it like enhancing a general-purpose intuition that we all possess. Thats where Bad Choices comes in. This book aims to acquaint you with algorithmic thinking by highlighting different ways of approaching everyday tasks and pointing out how these approaches fare relative to each other. For instance, two methods of looking for a shirt in your size on a rack of shirts might be described like the graph below.
Those shapes of lines have names like linear and logarithmic, which we will flesh out and discuss throughout the book. And while both approaches are comparable in terms of performance when we have a few things, notice how that changes as the number of things increases. This book includes twelve familiar scenes, such as a living room, a tailor shop, and a department store. In each scene, there are a number of potential tasks to be done. After each illustration, a paragraph describes the scene, and a few pages of commentary and discussion relate the scene to concrete concepts from computer science and highlight at least two possible ways of undertaking the fundamental task at hand. One thats slower and one thats faster. That difference is what the books title aims to emphasize, albeit somewhat provocatively. The title is partly inspired by computer scientist Donald Knuths talk of good algorithms, which is to say fast or effective ones.
INTRODUCTION
WHY FOCUS ON RELATIVE MAGNITUDES?
C omparisons are amazingly powerful. One of the first things children learn are abstractions like big and small, which is why when a child asks, How tall is that titanosaur that they now have at the Natural History Museum? one finds that it is less meaningful for the child to hear the response, Seventeen feet tall, little one, and more meaningful to hear, If Ms. Susan, Ms. Margaret, and Mr. Jascha were to stand on each others shoulders, Mr. Jascha would probably be able to tickle the titanosaurs chin.
Thinking in terms of relative magnitudes may in fact be an ability that we are all born with. Recent experiments seem to suggest that babies show as much brain activity in response to a change in an image they are seeing as they do in response to noticeable changes in the number of images they are seeing. Other experiments in remoter parts of the world suggest that people who have not been subjected to what we might call formal education reason about numbers in terms of orders of magnitude. Its an intuition that we appear to have innately.
One subspecies of humans whose appreciation for this intuition is manifest is computer scientists. Its what gives them the ability to recognize, fairly quickly, which of several competing approaches to solving a problem might be better. That fact is a reminder that seeing things in terms of relative magnitudes is an ability that remains useful even after you develop mastery of a field. Think of it like the mathematical notation you learned in primary school, which you continued to use throughout school and college and beyond.
This idea is a large part of the motivation behind writing this book. I had long used comparisons, estimates, and approximations to understand various concepts during my school and college years, but I dared not admit that to anyone because it felt like a less sophisticated way of learning. It wasnt until I read books like