About Gio
I am a torrent of ingenuity (or insanity) with a myriad of innovations (sometimes fallacies) and a wealth of inspiration (possibly naiveté). My name is Gio Carlo Cielo Borje and I like puffer fish because they're just cooltalkin', highwalkin' and fastlivin'.
I'm also nineteen and a current student at UC Irvine for Computer Science.
A Gentle Introduction to Algorithms for Web Developers
The common, self-taught web developer tinkering with their LAMP stack uses algorithms in their daily programming yet several web developers are unaware of it. When our developer realizes the importance of optimization on the web, algorithms become prevalent; however, several algorithm tutorials on the internet are meant for academics i.e. they have nasty math concepts. This tutorial is an introduction to the self-taught web developer.
Define: Algorithms
Those brilliant programmers, CLRS, that authored Introduction to Algorithms define algorithms in this simple manner:
In short, an algorithm could be a simple function that takes a set of parameters as input and returns an output. They are important to understand because algorithms often determine how quickly a system runs.
For example, a database may lookup a thousand elements within a few milliseconds because certain algorithms they use run in logarithmic time.
Simple Addition Algorithm
Consider this simple addition algorithm as an example to analyze:
Analyzing this algorithm is straightforward. The function takes two parameters
$xand$yas input and returns their sum as output. Frankly, this algorithm is simple; therefore, it’s uninteresting to us.What we care about in algorithms is their complexity.
Loops Add Complexity
Some of the most important control structures to web developers are loops. Loops alone add a significant amount of complexity to algorithm; specifically, they’re usually linear complexity.
This algorithm has become more complex but it's still manageable for analysis. It's easy to see now that the input is an array and the output is the sum of all elements in the specified array.
Aside from input and output, we analyze algorithms based on their complexity. Complexity considers how the algorithm scales with a specific input size. In the example above, the for-loop iterates from
$itosizeof($arr); consequently, the number of iterations is one-to-one with the number of elements. We call this type of complexity linear.Linear growth simply means that the algorithm is just a little slower than the addition algorithm because of the number of inputs considered.
Increasing Complexity with Nested Loops
Now you must be curious about how insanely complex algorithms are made. Here's a really easy method: nest the loops.
We call such nested growth polynomial instead of linear. Consider each foreach-loop as a linear algorithm that processes \(f\), \(t\) and \(p\) elements respectively. By nesting our linear loops, we process \(p\) elements several times per loop such that \(f t p\) elements are processed by the end of the algorithm. Consequently, we can also call this algorithm cubic for processing \(f t p\) elements.
The trick here to analyzing algorithm complexity is counting the number of nested for-loops. Most likely, the more nesting you have, the slower your algorithm becomes; conversely, the less nesting you have, the quicker your algorithm becomes.
Therefore, when confronted with a choice between three loops or one loop for a function, it's more likely that the single loop will be quicker.
Frankly, Analyzing Algorithms is Complex
So, I will not dive into deeper details beyond the scope of analyzing nested loops. We've discovered that nested loops are a sign of slow code; however, this may not always be the case.
Considering all the cases of algorithm analysis demands comprehensive learning; consider using CLRS as a guide book to algorithms. For others, this book may be too theoretical; instead, consider practicing challenging algorithms as they appear.
Further Readings: