At the simplest level, an algorithm is a set of specific instructions (or steps) that can be taken to successfully complete a task and can be repeated indefinitely. When talking about an algorithm in computational terms, we’re talking about the specific set of steps that are carried out to complete the computation.
Of course, the details of what make algorithms so important are a lot more complicated than this but this introductory definition is a great place to start when thinking about algos (that’s the abbreviation for algorithms fyi).
Algorithm Essentials: Complexity and Cocktail Parties
The simplest way to get any complex task done is to break it down into a series of smaller, manageable steps – which is essentially what an algorithm does.
An algorithm works by carrying out all of the smaller component steps that are needed to complete the final complex task.
Think about a repetitive, multi-step, and otherwise complex routine that you might need carry out in the real world – like spending the night making custom cocktails for guests at a party. Each cocktail made requires a very specific set of steps that have to be followed, in a specific order and using specific quantities of ingredients each time.
Not just this, but each type of cocktail requires a different set of variables to the others.
When someone orders a Margarita, the requirements are going to be very different to a Manhattan or a Moscow Mule. Each drink is different and has its own set of specific steps to follow which culminate in a complex (but delicious) end-product.
But what if you decide to only serve Mojitos? In this case, the same step by step preparation is required, but it needs to be repeated exactly the same each time the drink is made.
Enter the algorithm
Lets say we decide to build a robotic assistant that will flawlessly execute the production of our cocktails. In order to program our automated mixing maestro to create the perfect drink on command every time, we need to give it instructions on how to create each drink that a guest orders, repeating the same steps as often as necessary without fail.
To do this, we’ll need to produce a series of algorithms (one for each drink) that let the machine know the exact steps that need to be taken, in which order they need to be executed and the ingredients and quantities required at each step along the way.
Here’s what the (overly simplified) non-code version of our algorithm might look like for making a Strawberry Daiquiri:
- Guest orders Strawberry Daiquiri
- Take 1 medium glass
- Add 1 strawberry to the base of the glass and muddle
- Add 3/4 ounce of lime juice to the glass
- Add 2 ounces of white rum to the glass
- Fill remainder of the glass with ice to the rim
- Add cocktail shaker to top of glass and hold both together
- Continue to hold shaker and glass together
- Turn glass, emptying contents of the glass into shaker
- Continue to hold shaker and glass together
- Shake both vigorously for 5 seconds.
- Take a new, chilled glass from the refrigerator
- Place a strainer over chilled glass
- Empty the contents from the cocktail shaker into the strainer
- Serve drink to guest
With this set of instructions in place, our robo-mixologist should be ready to tackle this order and make the perfect drink whenever it’s requested.
Once we program all of the other drinks into the machine in the same way – with individual algorithms for each – not only will we not need to tell the machine every single time a drink is requested (or worse yet, make it ourselves 😱), but from that point on, the perfect replicable cocktail is just a button press or voice command away.
This is the power of algorithms.
Algorithms form the backbone of computer programs which have to carry out thousands of complex and varied tasks again and again and again… and again.
Sometimes algorithms are put to work helping with the mundane repetitive tasks that we simply wouldn’t want to carry out on repeat day in and day out, and sometimes they’re put to work on less common tasks where having a programmed system find a better way of doing them is more beneficial than having a human attempt the same task.