r/learnprogramming • • Apr 05 '22

Name one programming/comp sci concept you never understood and if you understand it, try to explain it to them

Name a programming concept such as virtual functions, pointers, certain algorithms, etc that you always had a hard time understanding and why.

Someone else who reads yours and does understand it, try to explain it to them to help them learn

1.6k Upvotes

738 comments sorted by

View all comments

Show parent comments

166

u/tzaeru Apr 05 '22 edited Apr 05 '22

Big O basically tells how much time or space a function needs when its input grows in size.

For example, consider this algorithm:

function(int n):
    counter = 0
    for (i = 0; i < n; i++)
         for (j = 0; j < n; j++)
             counter++
    return counter

If n is 1, what's the number returned? It's 1*1 = 1. There was 1 increment operation done.

If n is 4, what's the number returned? It's 4*4 = 16. There were 16 increment operations done.

So you see the algorithm's time complexity grows by n^ 2. Big O notation is a way of expressing this; we can say that the time complexity is O(n2 ).

You can simply get the time complexity by counting the number of operations in the function and comparing how that number changes depending on the size of the input.

O(log n) is one very common time complexity and one example of an algorithm with O(log n) time complexity is searching a sorted array with binary search. The reason it's log n is that in each step, binary search splits the search space in two; so with input with size of 8 there are at most 4->2->1 or 3 steps, while with input size of 16 there's at most 8->4->2->1 or 4 steps.

4/3 = 1.33.. and log(16)/log(8) = 1.33..

94

u/Rote515 Apr 05 '22

Although this comment is true, i don’t think it gets to the heart of BigO, the most important part of bigO is that it’s a time complexity estimation more than an exact time complexity which your examples do not show. For example a 3N + 2N + 2N2 algorithm has a bigO of N2 as we drop the constants and only use the fastest growing term, because as N gets large it ends up being the only term we care about from a complexity point of view.

24

u/XilamBalam Apr 05 '22

Yes, it's looking how the function "behaves at infinity" (asymptotic behaviour).

1

u/ExtraFig6 Apr 07 '22

this part gets missed a lot imo

the behavior at infinity is irrelevant if you know you only ever need n<=10.

1

u/RadioactiveHappiness Apr 24 '22

This reminds me a lot of looking at L'Hopital's Rule for evaluating limits of indeterminate functions! Excuse me, just a lowly Calc 3 student working towards taking Comp Sci this fall :)

5

u/MaxwellCE Apr 06 '22

What if the algorithm is N² + N³? Could we say that has a big O of N³, or is the N² component non-negligible?

10

u/Rote515 Apr 06 '22

BigO only ever cares about the fastest growing term, so n2 + n3 would be N3 significance doesn’t matter, consider n = 20 in the above algorithm, n2 = 400 n3 = 8000 at that point n is negligible already, now imagine n=100 or n=1000 a larger power always makes the smaller power insignificant at any non trivial n

2

u/[deleted] Apr 06 '22

Infinite limits have entered the chat

1

u/tzaeru Apr 06 '22

That's a good point yes. Honestly it's been so long I last thought about the Big O notation that I had completely forgotten about removing the constants and finding the highest term.

2

u/__Fred Apr 06 '22 edited Apr 06 '22

/u/famrob

To add to that: Technically O(n²) is a set. It's the set of all functions for which this is true:

There is exists a factor C and a threshold for which all x thereafter: f(x) ≤ C * x²


For example, if you have this Python function:

def combinations(list1, list2):
    result = []
    for e1 in list1:
        for e2 in list2:
            result.append(e1 + e2)
    return result

How long does it take to run? Well, we can't know for sure. If each of those operations always took the same time (which isn't true), it would take the running time of

  • result = []

    • for e1 in list1 * the length of list1

    • for e2 in list2 * len(list1) * len(list2)

    • result.append(e1 + e2) * len(list1) * len(list2)

    • the running time of return result

The the function "t" that gives the running time of combinations has to have two parameters "n" - len(list1) and "m" - len(list2).

t(n,m) = a + b * n + c * n * m

Everything clear so far? "c" would be the time for for e2 in list2: result.append(e1 + e2), for example.

For simplicity sake, we can just assume that both lists have the same length. Then the formula becomes this:

t(n) = a + b * n + c * n²

Is this function in the set of O(n²)? Can you choose a factor C and a threshold n'?

For all n > n': t(n) ≤ C * n²
For all n > a: a + b * n + c * n² ≤ (a+b+c) * n²

(Seems correct. I'm not sure.)


Anyway... Basically, what you normally do if you have a sum of multiple factors, is look at the factor that would grow the fastest. "c * n²" grows faster than "b * n". No matter how large b is, if you took a big enough n, "c * n²" would be larger than "b * n". "dn" would grow even faster than n².

So, if you know that "c * n²" is the dominant factor in your time function, than you can already say that the function is in (or colloquially is) O(n²).

If you have one loop that is dependent on the input, you typically have O(n) and if you have a nested loop, it's typically O(n²).

If we want to know whether an algorithm is good, we don't need the exact execution time function, we just need the complexity class.

If I'm not mistaken technically O(n) is a subset of O(n²), but don't try to write that the time complexity of insertion-sort is O(n³) in an exam. That's not what they mean.

Wikipedia for further details

1

u/corner_guy0 Apr 06 '22

Great explanation brother and also Happy Cake Day!