curiouslychase

What Is An Algorithm?

Learn the basics of what an algorithm is with a simple, everyday example.

The term algorithm sounds like it's complicated, scary and unapproachable.

To some, "algorithm" sounds like a term for conjuring magic, much like alchemy.

The beauty of the term algorithm is that it's actually really simple: It's just a set of instructions.

An algorithm is a set of step-by-step instructions (called a procedure) to solve a particular problem.

I'll explain an algorithm by using one of my favorite examples that I use every day: Making my morning cup of coffee.

Algorithm for Making a Pour-Over Coffee

  1. Go to the kitchen.
  2. Get the kettle from the kettle base.
  3. Go to the sink.
  4. Hold the kettle under the faucet.
  5. Fill the kettle with water to the "Max" line.
  6. Go to the kettle base.
  7. Set the kettle on the kettle base.
  8. Turn on the kettle base to heat water.
  9. Get a bag of beans.
  10. Open the bag of beans.
  11. Pour beans into measuring cup.
  12. Get the grinder.
  13. Verify cup for grinds is under the grinding mechanism.
  14. Plug the grinder into the outlet.
  15. Pour beans into the grinder.
  16. Turn on grinder.
  17. Get the coffee carafe.
  18. Get the pour-over dripper.
  19. Get a pour-over filter.
  20. Put the pour-over filter in the dripper.
  21. Wait for kettle to reach 193 F.
  22. Take kettle off base.
  23. Take kettle and pour-over dripper to sink.
  24. Pour water for 2 seconds into dripper over sink in circular motion.
  25. Go back to carafe.
  26. Put dripper ontop of carafe.
  27. Pour grinds into dripper.
  28. Pour water into dripper for around 3 seconds until grinds bloom.
  29. Wait 30 seconds for bloom.
  30. Pour water into dripper for 45 seconds.
  31. Take dripper off of carafe.
  32. Grab a coffee mug.
  33. Pour coffee from carafe into coffee mug.

You may not think of this as an algorithm, but it is!

That's a LOT of instructions, but it's simple in that it is an algorithm — a set of step-by-step instructions to solve a problem.

Thanks to algorithms, I enjoy a perfect cup of coffee every morning.

Key Ingredients to An Algorithm

The key ingredients to an algorithm, what's required for an algorithm to exist are:

  1. Clearly Defined Inputs: An algorithm must have zero or more well-defined inputs. These are the data that your algorithm will process.
  2. Clearly Defined Outputs: An algorithm should have one or more well-defined outputs. This is the result of your algorithm after processing the input data.
  3. Definiteness: Each step in an algorithm must be clear and unambiguous. There should be no confusion about what the step does. It's the difference between "cook until done" and "bake at 350 degrees for 20 minutes".
  4. Finiteness: The algorithm must always terminate after a finite number of steps. An algorithm can't go on forever.
  5. Effectiveness: Every step of the algorithm must be sufficiently basic that it can in principle be done exactly and in a finite amount of time.
  6. Generality: The algorithm should be applicable to a set of inputs and not just a single, specific case.

Types of Algorithms

There are a growing number of types of algorithms.

This is a list of standard (or generally accepted/canonical) algorithms used in Computer Science and Problem Solving:

  1. Search Algorithms: These are algorithms designed to locate a specific item or set of items within a larger structure. Google's search algorithm is a famous example.
  2. Sort Algorithms: These algorithms are used to arrange data in a certain order. Examples include quicksort, mergesort, and bubblesort.
  3. Graph Algorithms: These are used for computing the shortest path in a graph, spanning trees, checking graph connectivity and so on. Dijkstra's and Prim's are notable ones here.
  4. Dynamic Programming Algorithms: These solve a complex problem by breaking it down into simpler subproblems in a recursive manner. An example is the Knapsack problem.
  5. Greedy Algorithms: These make the locally optimal choice at each stage with the hope of finding the global optimum. The activity selection problem is a classic case of a greedy algorithm.
  6. Divide and Conquer Algorithms: These break a problem into distinct subproblems of the same type. The merge sort and quicksort algorithms are examples of this approach.
  7. Brute Force Algorithms: These are the simplest algorithms, attempting to solve a problem through all possible solutions. Examples include simple linear search or the traveling salesman problem solved by checking all possible routes.
  8. Recursive Algorithms: These algorithms solve problems by dividing them into smaller, identical problems until they reach a simple case that can be solved directly. The Fibonacci series calculation is a classic example.
  9. Backtracking Algorithms: Backtracking is a type of algorithm for finding solutions to some computational problems, notably constraint satisfaction problems, by incrementally building candidates to the solutions, and abandoning a candidate as soon as it is determined that the candidate cannot possibly be extended to a valid solution. The classic example is the Eight Queens Puzzle.
  10. Randomized Algorithms: These use a random number at least once during the computation to make decisions. Quicksort using a random pivot is an example.
  11. Parallel Algorithms: These algorithms divide a problem into smaller subproblems and solve them concurrently (at the same time). An example would be matrix multiplication using parallel computing.
  12. Heuristic Algorithms: These are rule-of-thumb algorithms that help in making decisions when perfect logic or optimal solutions are not possible. They are commonly used in machine learning and artificial intelligence. Genetic algorithms and A* search are good examples.
  13. Online Algorithms: These algorithms process their inputs in a serial fashion, i.e., piece-by-piece in an online manner, without having the entire input available from the start. For example, internet packet routing.

Info

It's important that I point out that I don't know all of these types of algorithms. This post was a result of me thinking about algorithms and doing research on algorithms to learn. As I explore these types of algorithms, I'll write more about them, but I want to be clear that I couldn't enumerate them all from memory as of writing!

Conclusion

Algorithms are amazing. They're a simple idea that have wide reaching applications. From coffee, to driving, to getting ready for the day, to sorting and processing data, algorithms are deeply entrenched in our lives.

Stay curious!

Exercise

I think it's a great idea to take some time through your day to ask "what algorithms" do you notice?

The next time you sit down for work, the next time you go to a coffee shop and order a cup of coffee, the next time you decide to go for a bike ride. Notice the sequence of steps you take to perform a task.

Doing so gives you a better understanding of how to think about algorithms.

Doing an exercise like this is also a great way to experience wonder at how our brains create their own algorithms without us explicitly telling them to!

Sorting Algorithms

Share on Twitter