Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BIG O In absence of enough information

So, lets say you have a function, X(N) that is a total black box. You don't know the growth rate of the function, you can't look it up, and you can't view the source (at the moment).

Next lets examine it in the context of another function

for(int i = 0; i < N; ++i)
    X(N);

The code that you wrote is linear, yet clearly the function X has an impact of the growth rate of your function. If, for example, X(N) expands into for(int i = 0; i < N; ++i) your function is quadratic.

My question is this: If someone is asking what the Big O of your function is, what is the best way to describe the growth rate of your function?

I said I would call this linear, and the defense of my answer is the following.

If you knew the actual growth rate of X, you can give an accurate estimation of your code, yet while you can (in one way or another) get the code eventually, most functions don't come with performance statistics.

So if you did get access to the code of X, you could include it in your estimation, but where do you draw the line? X likely also calls other functions, which then call other functions. I feel that outside of manufactured scenarios where you are dealing with perfectly compartmentalized code, if you don't already know the growth rates of the black box functions being called you have to decide to estimate the code that you can.

like image 870
Taekahn Avatar asked May 02 '15 16:05

Taekahn


People also ask

Under what situation can we ignore Big O notation?

Big O notation ignores constants. For example, if you have a function that has a running time of 5n, we say that this function runs on the order of the big O of N. This is because as N gets large, the 5 no longer matters.

What does it mean for something to be in Big-O?

Big O is sometimes referred to as the algorithm's upper bound, meaning that it deals with the worst-case scenario. The best-case scenario doesn't really tell us anything — it will be finding our item in the first pass. We use worst-case to remove uncertainty — the algorithm will never perform worse than we expect.

Is Big-O for the worst time?

Worst case — represented as Big O Notation or O(n)Big-O, commonly written as O, is an Asymptotic Notation for the worst case, or ceiling of growth for a given function. It provides us with an asymptotic upper bound for the growth rate of the runtime of an algorithm.

What does Big-O refer to and what is it good for?

“Big O notation is a mathematical notation that describes the limiting behavior of a function when the argument tends towards a particular value or infinity.


2 Answers

If you would talk about drawing the line, I'd simply like to deliver like :-

The code's complexity :- O(N*o(X))

As soon as one get's to judge the complexity of the function X(N), one can simply substitute in the formula.

Till then, it will be a shorthand but useful notation all in all along with satisfying the loop's complexity.

like image 62
Am_I_Helpful Avatar answered Sep 20 '22 18:09

Am_I_Helpful


You simply can't tell. There just isn't enough information. Saying that your function is linear is wrong unless X(N) is constant time.

You could, however, measure the time that X(N) takes to complete for different input sizes. Often this will give you a rough estimate of how it behaves asymptotically.

like image 36
cfh Avatar answered Sep 19 '22 18:09

cfh