Tuesday, February 18, 2014

Why Designing an Algorithm is like Designing a Chocolate Chip Cookie Recipe

What is an algorithm? In a world where understanding code and technology is becoming essential, I'd like to offer this –perhaps obvious–analogy.

Algorithms are like Chocolate Chip Cookie Recipes.

An algorithm is a set of steps to solve a problem, while trying to minimize or maximize something. A recipe is a set of steps to solve the problem of "I'm hungry!" while fitting your constraints.

An algorithm has input, procedures, and output. A recipe has ingredients, recipe steps, and a finished dish.

There can be many different algorithms to do the same thing. There can be tons of sorting algorithms, just like there can be a billion Chocolate Chip Cookie recipes on AllRecipes.com.

Algorithms try to optimize for different scenarios. They try to minimize space and cost, or maximize speed, or elegance.

Recipes also try to optimize for different things. You can maximize speed, because you want to eat cookies NOW. You can try minimize space, and make it all in one bowl. You can try and make it resource-non-intensive, because you're lazy. You can try to make it really beautiful, because you enjoy the look as much as the taste. Or you can maximize deliciousness while minimizing calories. A multiparameter optimization!

You can optimize all these things, but it’s up to you, the algorithm designer–the cook–to decide which metric is important to YOU.

Designing an algorithm also requires iterative testing. You can try combining different methods, and must check the result and tweak parameters until you think it works well. Sometimes you just throw it all out if it's a mess and start over. When making a recipe, you can combine steps from different recipes, lick the spoon, and tweak ingredient amounts and until you deem it worthy. You, the cook, decide when it's good and ready to serve up to others.

Deciding which problem to solve is also pretty important. You're a chef, you have all these skills–do you use them to cater wedding parties? To open a restaurant? To make designer cupcakes? Which problem in the world do you solve with your skills?

Algorithms are like recipes. We even have cookbooks :) Have I missed anything? 

Whelp, I'm off. Let me know if this has been a totally obvious post. All I know is, these pictures are making me hungry...

Edit: Some more contributions from a friends:

Experience in breaking things can help guide you in your design. Copying an algorithm line-by-line can sometimes get the job done, just like following a recipe word-for-word can give you cookies with no effort. But you don't necessarily understand what you did. What really teaches you, though, is messing up. What? If I replace margarine with butter, then my cookie edges burn? What? My algorithm doesn't work when I use X instead of Y? So experience and trial and error matter. I'd like to suggest that this is how many coders that grow up learning to code, before taking any computer science classes.

However, a theoretical background matters a lot, too. How do you become a master chef? You learn the theory. You know all of your ingredients, their properties, and methods. You know that egg whites contain proteins that break down when salt is added. You know that the point of whipping is not to mix vigorously, but to add air into the mixture, so a flick-lift method is better than a spinning whip. With a theoretical background, you can guess what the outcome will be even before you do it. A good background in computer science and algorithms will give you this.

1 comment:

PhilB said...

Along the same lines, you may enjoy this article: