What Are Computer Algorithms, and How Do They Work?

By YatriTrivedi on February 22nd, 2011

banner

Unless you’re into math or programming, the word “algorithm” might be Greek to you, but it’s one of the building blocks of everything you’re using to read this article. Here’s a quick explanation of what they are, and how they work.

Disclaimer: I’m not a math or computer science teacher, so not all of the terms I use are technical. That’s because I’m trying to explain everything in plain English for people aren’t quite comfortable with math. That being said, there is some math involved, and that’s unavoidable. Math geeks, feel free to correct or better explain in the comments, but please, keep it simple for the mathematically disinclined among us.

Image by Ian Ruotsala

What’s an Algorithm?

The word ‘algorithm’ has an etymology similar to ‘algebra,’ except that this refers to the Arabic mathematician himself, al-Khwarizmi (just an interesting tidbit). An algorithm, for the non-programmers among us, is a set of instructions that take an input, A, and provide an output, B, that changes the data involved in some way. Algorithms have a wide variety of applications. In math, they can help calculate functions from points in a data set, among much more advanced things. Aside from their use in programming itself, they play major roles in things like file compression and data encryption.

A Basic Set of Instructions

Let’s say your friend is meeting you in a grocery store and you’re guiding him towards you. You say things like “come in through the right-side doors,” “pass the fish section on the left,” and “if you see the dairy, you passed me.” Algorithms work like that. We can use a flowchart to illustrate instructions based on criteria we know of ahead of time or find out during the process.

icebreaking-routine

(image entitled “Icebreaking Routine” EDIT: courtesy of Trigger and Freewheel)

From START, you would head down the path, and depending on what happens you follow the “flow” to an end result. Flowcharts are visual tools which can more understandably represent a set of instructions used by computers. Similarly, algorithms help do the same with more math-based models.

Graphs

Let’s use a graph to illustrate the various ways we can give directions.

graph_drawn.gif

We can express this graph as a connection between all of its points. In order to reproduce this image, we can give a set of instructions to someone else.

Method 1

We can represent this as a series of points, and the information would follow the standard form of graph = {(x1, y1), (x2, y2), …, (xn, yn)}.

graph = {(0,0), (3,0), (3,3), (5,5), (7,10), (8,7), (9,4), (10,1)}

It’s pretty easy to plot each point, one after the other, and connect them to the previous point. However, imagine a graph with a thousand points or multiple segments all going every which way. That list would have a lot of data, right? And then having to connect each one, one at a time, can be a pain.

Method 2

Another thing we can do is give a starting point, the slope of the line between it and the next point, and indicate where to expect the next point using the standard form of graph={(starting point}, [m1, x1, h1], …, [mn, xn, hn]}. Here, the variable ‘m’ represents the slope of the line, ‘x’ represents the direction to count in (whether x or y), and ‘h’ tells you how many to count in said direction. You can also remember to plot a point after each movement.

graph = {(0,0), [0,x,3], [0,y,3], [1,x,2], [2.5,x,2], [-3,x,1], [-3,x,1], [-3,x,1]}

You’ll end up with the same graph. You can see that the last three terms in this expression are the same, so we may be able to trim that down by just saying “repeat that three times” in some way. Let’s say that anytime you see the variable ‘R’ appear, it means to repeat the last thing. We can do this:

graph = {(0,0), [0,x,3], [0,y,3], [1,x,2], [2.5,x,2], [-3,x,1], [R=2]}

What if the individual points don’t really matter, and only the graph itself does? We can consolidate those last three sections like so:

graph = {(0,0), [0,x,3], [0,y,3], [1,x,2], [2.5,x,2], [-3,x,3]}

It shortens things up a bit from where they were before.

Method 3

Let’s try doing this another way.

y=0, 0≤x≤3
x=0, 0≤y≤3
y=x, 3≤x≤5
y=2.5x-7.5, 5≤x≤7
y=-3x+29, 7≤x≤8
y=-3x+29, 8≤x≤9
y=-3x+29, 9≤x≤10

Here we have it in pure algebraic terms. Once again, if the points themselves don’t matter and only the graph does, we can consolidate the last three items.

y=0, 0≤x≤3
x=0, 0≤y≤3
y=x, 3≤x≤5
y=2.5x-7.5, 5≤x≤7
y=-3x+29, 7≤x≤10

Now, which method you pick depends on your abilities. Maybe you’re great with math and graphing, so you choose the last option. Maybe you’re good at navigating, so you choose the second option. In the realm of computers, however, you’re doing many different kinds of tasks and the computer’s ability doesn’t really change. Therefore, algorithms are optimized for the tasks they complete.

Another important point to note is that each method relies on a key. Each set of instructions is useless unless you know what to do with them. If you don’t know that you’re supposed to plot each point and connect the dots, the first set of points means nothing. Unless you know what each variable means in the second method, you won’t know how to apply them, much like the key to a cipher. That key is also an integral part of using algorithms, and often, that key is found in the community or via a “standard.”

File Compression

When you download a .zip file, you extract the contents so that you can use whatever is inside of it. Nowadays, most operating systems can dive into .zip files like they were normal folders, doing everything in the background. On my Windows 95 machine over a decade ago, I had to extract everything manually before I could see anything more than the filenames inside. That’s because what was stored on the disk as a .zip file was not in a usable form. Think of a pull-out couch. When you want to use it as a bed, you have to remove the cushions and unfold it, which takes up more space. When you don’t need it, or you want to transport it, you can fold it back up.

Compression algorithms are adjusted and optimized specifically for the types of files they are targeted to. Audio formats, for example, each use a different way to store data that, when decoded by the audio codec, will give a sound file similar to the original waveform. For more information on those difference, check out our previous article, What Are the Differences Between All Those Audio Formats? Lossless audio formats and .zip files have one thing in common: they both yield the original data in its exact form after the process of decompression. Lossy audio codecs use other means to save disk space, such as trimming frequencies that aren’t able to be heard by human ears and smoothing out the waveform in sections to get rid of some detail. In the end, while we may not be able to really hear the difference between an MP3 and a CD track, there’s definitely a deficit of information in the former.

Data Encryption

enc-algorithms-(truecrypt)

Algorithms are also used when securing data or communication lines. Instead of storing data so that it uses less disk space, it’s stored in a manner that is undetectable by other programs. If someone steals your hard drive and starts to scan it, they can pick up data even when you delete files because the data itself is still there, even though the forwarding location to it is gone. When data is encrypted, whatever is stored doesn’t look like what it is. It usually looks random, as if fragmentation had built up over time. You can also store data and make it appear as another type of file. Image files and music files are good for this, as they can be quite large without drawing suspicion, for example. All of this is done by using mathematical algorithms, which take some kind of input and convert it into another, very specific type of output.  For more information on how encryption works, check out HTG Explains: What is Encryption and How Does It Work?


Algorithms are mathematical tools which provide a variety of uses in computer science. They work to provide a path between a start point and an end point in a consistent way, and provide the instructions to follow it. Know more than what we highlighted? Share your explanations in the comments!

Yatri Trivedi is a monk-like geek. When he's not overdosing on meditation and geek news of all kinds, he's hacking and tweaking something, often while mumbling in 4 or 5 other languages.

  • Published 02/22/11
More Articles You Might Like

Enter Your Email Here to Get Access for Free:

Go check your email!