How to Understand Algorithms

By John Lister

An algorithm is simply a set of rules to follow in a specific situation. Humans use algorithms all the time: for example, when leaving the house you may follow an algorithm that could be expressed as "Check if the sky is gray; If Yes, take umbrella. Check if the sun shining; if Yes, take sunglasses. Leave house." All computer applications boil down to one of more algorithms, each of which carries out a particular task.

Characteristics of Algorithms

The defining point of an algorithm is that it leaves no room for subjectivity or judgment. An algorithm handles data and takes resulting action in a fixed manner according to its set of rules. In its purest form, whenever you put the same inputs into an algorithm you'll get the same outcome. A computer using an algorithm has the advantage over humans in that it will always carry out the relevant actions correctly and quickly; humans have the advantage in that they can alter and even abandon algorithms if doing so is advantageous in a particular situation -- a skill you could describe as "common sense."

Comparing Algorithms

Assuming an algorithm is correctly designed to give the desired outcome in any particular situation, the main point of comparison between two outcomes is speed. The Topcoder site notes that algorithms are normally rated based on the time it would take to carry out a particular task with the set of inputs that makes the process the most complicated and time-consuming. For example, when trying to match a piece of data with something in a lengthy list, the algorithm could just check the list one item at a time. However, a better solution is to put the list in order, such as alphabetical or numerical, then compare the data with the middle entry and see if it is "higher or lower." Using this result, the algorithm can eliminate half the entries as non-matches immediately, then repeat the process until it finds a match, likely in far fewer steps than checking the list in full.

Approximations & Heuristics

Some problems are particularly difficult for an algorithm to solve. A classic example is the scenario of a travelling salesman who needs to visit several cities and find the route with the shortest total traveling time. The sheer number of possible options makes this such a complicated task that it would take an algorithm too long to explore every possibility and come up with the perfect answer. Instead the answer is to mirror human behavior by taking the first possible solution that meets an acceptable level of variation from the optimal route -- in other words, the first answer that is good enough, rather than perfect. This concept of using algorithms to get a result that favour practicalities over perfection is sometimes known as heuristic analysis.

Key To Understanding

Jonathan Cutrell, technology director at Whiteboard, argues that simply learning the code needed to write an algorithm in a particular programming language isn't enough to get the best results. Instead programmers need to take the time to understand the purpose of every component of an algorithm's code, concentrating on the How and Why rather than just the What. This approach will help adapt and improve algorithms for particular situations. Cutrell also notes the importance of understanding how a particular algorithm interacts with other software elements such as applications and even an operating system. These interactions might change the most efficient approach to an algorithm compared with the approach you'd take when designing it in isolation.