RSS

How-To Geek

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.

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.

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.

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:

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!

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.

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.

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

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. :-)

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

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…

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. :)

DID YOU KNOW?

Wolves howl out of sync with one another, with each wolf joining in the howling a few moments behind the last wolf, in order to create the illusion that there are more wolves in the pack.