Articles

N-ary Tree Data Structure API in Java

Image
TreeDS4j is a Java (6+) API that allows handling n-ary tree data structure in such a flexible way. Indeed, the user is subject to enter structured and coherent data, and the latter would be stored in an hierarchy, accordingly. The link below contains two samples that show how to take advantage of the API. https://sourceforge.net/projects/treeds4j/

Decreasing Complexity Means Increasing Performance

What is wrong the piece of C Language code below? for ( i = 1 ; i <= length(str) ; i ++ ) { // Do some stuff } There is a recalculation of the length of the string at every iteration, which is a major waste of time. Indeed, at the first iteration, we already know how many times we need to iterate; when it's time for the second iteration, a recalculation of the already known value is done once again ... we keep doing this n times. Hence, complexity here is unnecessarily high. Detailing the code above in a more explicit manner, it may be translated as follows: int n = 0; while (str[i] != '\0') { n ++; } for ( i = 1 ; i <= n ; i ++ ) { // Do some stuff n = 0; while (str[i] != '\0') { n ++; } } At a glance, not only the code becomes redundant and longer, this fragment's time complexity is obviously O(n²) . The easy optimisation is to add a variable n , which will hold the length of the string, and, consequently...

Indirect Recursion - A Natural Phenomenon

Image
Indirect recursion could be noticed in seeds and trees: a seed ends up to be a tree, and the latter produces many seeds, which themselves become trees and so forth. An interesting case was beautifully described in the Qur'an, chapter 2, verse 261, a plain description that is easy  to represent: " The example of those who spend their wealth in the way of Allah is like a seed [of grain] which grows seven spikes ; in each spike is a hundred grains. And Allah multiplies [His reward] for whom He wills. And Allah is all-Encompassing and Knowing ". ( Source ). Regardless of the religious aspects, one can notice the exponential growth of seeds, which is appreciated in agriculture, but seriously deprecated in Computer Science. If we represent this phenomenon (seeds and spikes) as a Java algorithm: void seed(x) { System.out.println("Seed " + x); for (i = 0; i < 7; i ++) { spike(i); } } void spike(y) { System.out.println("Sp...

Triple Nested Loops - Two Linear Loops and One Logarithmic with a non-Constant Base - The Monster

Image
To obtain the function that tells the exact number of iterations a set of nested loops would perform didn't seem to be a tedious exercise. However, I came accross this interesting case: - Linear outer loop - Dependent first level linear inner loop - Dependent second level logarithmic inner loop with a general base. for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1; j <= i ; j ++ ) { for ( k = 1 ; k <= j ; k *= a ) { // c } } } Ceilinged/Floored logarithmic functions within summations turned out to have very lengthy closed-forms (See my previous post). We will dissect the formulas above (for any a > 1 and n >= 1): Therefore, This link  points at a  plain   Java program that represents the above functions (f(), g () , h () , u () , v () , w () , respectively), and give the exact number of iterations performed by the covered algorithm mentioned earlier (receives two a...

Logarithmic Summations and Discrete Loops - Ceiling and Floor Functions

Image
Reading the so called paper " Discrete Loops and Worst Case Performance " ( by Dr. Johann Blieberger ), I came across some interesting summations: Useful here: for ( i = 1; i <= n ; i ++ ) { for (j = 1; j <= i ; j *= 2) { // Some constant time C instructions. } } Because: Likewise: Applicable here: for ( i = 1; i <= n ; i ++ ) { for (j = 1; j < i ; j *= 2) { // Some constant time C instructions. } } Because: And more generally: for ( i = 1; i <= n ; i ++ ) { for (j = 1; j <= i ; j *= a) { // Some constant time C instructions. } } We can proceed like the following: Further: for ( i = 1; i <= n ; i ++ ) { for (j = 1; j < i ; j *= a) { // Some constant time C instructions. } } With: Thus, while I tried to refine the results of some nested loops running time analysis, I had to face a particul...

Asymptotic Analysis - Vehicles Race and Algorithm Running Time

Image
First, watch this video: Now, let's assume the vehicles, namely the Jet, the Car, and the Motorcycle are your algorithms. Also, suppose the length of the racecourse is the size of the data n . Consequently, the speed of the vehicles can be analogized to the running time of the algorithms. Eventually, if we had to embody the circumstances of this interesting race, we will obtain this (approximately): Supposedly, the respective vehicles speed closed-form: Which means: Formal Analysis: Let the instant between 0:24 and 0:30 be represented by  n 0,  instant 0:33 represented by  n' 0 , and instant 0:36 by  n'' 0 . The most interesting case to study, is T car (n), as it grows faster than T motorC (n) but slower than T jet (n). Critique: We notice that, if the length of the courserace was small, the motorcycle would have won this contest. The speed of the jet is exponentially larger than both the car and the mo...

Asymptotic Analysis - Approaching Algorithms to Mathematics (I)

Image
Complexity theory is the field of mathematics that allows to determine the asymptotic order of growth of a given mathematical function. Let's take a look first at different possible orders of growth: We notice that n! is the most expensive order of growth in this plot; indeed, it takes a very large number of seconds, or even minutes, for instance, to execute an input of size n = 10. whilst 10n  (the cheapest order of growth) would take less than a hundred seconds to execute an input of the same size (n = 10). Moreover, we notice, for example, that 2n² is slower than 20n within the interval [0 .. 10[, but eventually do intersect when n = 10. In other words, an algorithm can be faster than an other one, when values are small, which might be misleading and deceiving. Thanks to complexity theory, determining the order of growth of any mathematical function ( remember: any algorithm can be represented as such ), helps to formally decide whether the algorithm we are goin...