# How-To Geek

## HTG Explains: What Are Computer Algorithms and How Do They Work?

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.

(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.

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

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!

- Published 02/22/11

Don’t try to read this first thing in the morning. Found a few things missing in my math. TaTa

Should have started with boolean algebra

umad?

Yup, it’s Greek.

Sounds very cool but…. woosh… straight over my head I’m afraid. :)

Try this maybe. Let’s figure out your gas mileage. First how may miles did you drive? (100). Second how many gallons of gas did you use?(10) Now the math Miles divided by gallons(100/10). You have gotten 10 miles to the gallon of gas. This routine is a very basic Algorithm.

Whenever you talk about algorithms, you should probably mention heuristics. Algorithms are provably correct. Heuristics are “best practices,” or should hopefully get you a nearly correct result. For example, A* search is often called an algorithm when it is actually a heuristic.

I tend to think of programming algorithms as recipies. You have your list of ingredients (variables, database connections, constants, etc). And then you have step-by-step instructions on how to use those ingredients to get a desired end result (a delicious dish, cake maybe?).

It also has the bonus that in both cases if you miss a step, or do a step wrong, the results may very well be disaster!

Then you take individual recipies and combine them all together to make a multiple course meal. And just like with food, you have to be aware of what you combine together to make sure the end result is pleasing to your customer.

Hey – Maybe those Arabs were on to something or perhaps they were just bored and looking for something to do. Methinks an old-fashioned digital calulator is good enpugh!

Excellent article, even without really reading the math! And I love Joe M’s analogy. It added even more clarification for those of us who may know a bit about messing with computers, bit are by no means geeks.

Thanks.

Thanks, I got the general idea…

Wow. This is a great way to make my head hurt. I prefer boolean myself, but I think that is because of my electronics background. It’s a very good article. 1 Internets to Joe M btw. Great addition to the article (please excuse the poor math pun).

Thanks. It’s a nice read. Now I know a little better what my Geek-Coder friends are talking about.

Having never been to New Jersey, I got lost. Exit from what? the grocery store? or- maybe New Jersey has doors, and we’re going to New York…

thanks for taking the time to explain it’s not your fault i don’t get it yet hahaha!!!

Excellent article. Joe M’s analogy was also well done and takes the mystery out of all these subjects.

Nice over simplified article. Thanks Yatri.

the more i read you stuff the more i believe

your i.q. is the top, none higher

i dont understand a lot of what you are saying, but

after reading info by readers on point i get a fair enough view

many thanks

An algorithm is simply a bridge between what we know and what we want to know.

A requirement for an algorithm is that it must terminate. This subtle point is used to help prove if something is computable. An operating system is an example of something that’s not an algorithm, because it’s designed to endlessly manage computer processes and resources. Now that’s an extra tidbit to chat about at the next happy hour mixer. :-)

Excellent post in my opinion.

Or put simply:

An Algorithm is a precise rule (or set of rules) specifying how to solve some problem.

Reccomended reading:

http://en.wikipedia.org/wiki/Algorithm

(In mathematics and computer science, an algorithm is an effective method expressed as a finite list of well-defined instructions for calculating a function. Algorithms are used for calculation, data processing, and automated reasoning.)

Good article, it is a very good introduction for those who want to know.

though it was from Al Gore rhythm since he said he helped invent Internet hahahahhahah

Arabs were the most advanced mathematicians of their time and invented all sorts of things in Algebra, left behind great architecture and literature.

Menyoj, writer

I enjoyed your article and learned a few things.

good stuff…………

I was trying to explain this concept to a processing engineer and a mid level manager just this morning at my place of employment. I think they are stuck in an anolog world of tubes and sparks. Digital control concepts that could solve my co-worker’s wild swings of processing difficulty are in the realm of fantasy to this management. I wish I could have presented your report on algorithms in support of my arguement that his problem had a workable solution. I’ll print it out an present this information as a follow up tomorrow when I get on the job. Thanks for your input. Just a liberal arts widgetmaker…

Reminds me of a SatNav, only difference being that it is a kind of verbal algorithm that takes you from point A to point B!

Very informative article it helps you to understand What actually Algorithms are and how they work.It helps you to understand the concept well.

WOW1 Where have I been all my life. :)

Method 1 is easy to understand. In method 2, I’m still trying to figure out how you get from [0,y,3] to [1,x,2] on the given chart. The last 3 [-3,x,1] I follow. What am I missing?

Ahh – wait till we start talking about Stochastic models and Heuristics . :-P

I never paid attention to my teachers at school when it came to Maths, but nowadays I try to teach myself as much as i possibly can (taught myself Algebra from scratch a year ago for TAFE entry exam). So thank you for this article, very helpful and I’ll endeavour to wade through it over time and hopefully glean some knowledge out of it. :)