Most people with a degree in CS will certainly know what Big O stands for.
It helps us to measure how well an algorithm scales.
But I’m curious, how do you calculate or approximate the complexity of your algorithms?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I’ll do my best to explain it here on simple terms, but be warned that this topic takes my students a couple of months to finally grasp. You can find more information on the Chapter 2 of the Data Structures and Algorithms in Java book.
There is no mechanical procedure that can be used to get the BigOh.
As a “cookbook”, to obtain the BigOh from a piece of code you first need to realize that you are creating a math formula to count how many steps of computations get executed given an input of some size.
The purpose is simple: to compare algorithms from a theoretical point of view, without the need to execute the code. The lesser the number of steps, the faster the algorithm.
For example, let’s say you have this piece of code:
This function returns the sum of all the elements of the array, and we want to create a formula to count the computational complexity of that function:
So we have
f(N), a function to count the number of computational steps. The input of the function is the size of the structure to process. It means that this function is called such as:The parameter
Ntakes thedata.lengthvalue. Now we need the actual definition of the functionf(). This is done from the source code, in which each interesting line is numbered from 1 to 4.There are many ways to calculate the BigOh. From this point forward we are going to assume that every sentence that doesn’t depend on the size of the input data takes a constant
Cnumber computational steps.We are going to add the individual number of steps of the function, and neither the local variable declaration nor the return statement depends on the size of the
dataarray.That means that lines 1 and 4 takes C amount of steps each, and the function is somewhat like this:
The next part is to define the value of the
forstatement. Remember that we are counting the number of computational steps, meaning that the body of theforstatement gets executedNtimes. That’s the same as addingC,Ntimes:There is no mechanical rule to count how many times the body of the
forgets executed, you need to count it by looking at what does the code do. To simplify the calculations, we are ignoring the variable initialization, condition and increment parts of theforstatement.To get the actual BigOh we need the Asymptotic analysis of the function. This is roughly done like this:
C.f()get the polynomium in itsstandard form.Napproachesinfinity.Our
f()has two terms:Taking away all the
Cconstants and redundant parts:Since the last term is the one which grows bigger when
f()approaches infinity (think on limits) this is the BigOh argument, and thesum()function has a BigOh of:There are a few tricks to solve some tricky ones: use summations whenever you can.
As an example, this code can be easily solved using summations:
The first thing you needed to be asked is the order of execution of
foo(). While the usual is to beO(1), you need to ask your professors about it.O(1)means (almost, mostly) constantC, independent of the sizeN.The
forstatement on the sentence number one is tricky. While the index ends at2 * N, the increment is done by two. That means that the firstforgets executed onlyNsteps, and we need to divide the count by two.The sentence number two is even trickier since it depends on the value of
i. Take a look: the index i takes the values: 0, 2, 4, 6, 8, …, 2 * N, and the secondforget executed: N times the first one, N – 2 the second, N – 4 the third… up to the N / 2 stage, on which the secondfornever gets executed.On formula, that means:
Again, we are counting the number of steps. And by definition, every summation should always start at one, and end at a number bigger-or-equal than one.
(We are assuming that
foo()isO(1)and takesCsteps.)We have a problem here: when
itakes the valueN / 2 + 1upwards, the inner Summation ends at a negative number! That’s impossible and wrong. We need to split the summation in two, being the pivotal point the momentitakesN / 2 + 1.Since the pivotal moment
i > N / 2, the innerforwon’t get executed, and we are assuming a constant C execution complexity on its body.Now the summations can be simplified using some identity rules:
w)Applying some algebra:
And the BigOh is: