Search code examples
algorithmcomplexity-theoryclrs

any linear function an + b is O(n^2) CLRS


In 3rd edition of CLRS specifically section 3.1 (page 47 in my book) they say

when a > 0, any linear function an + b is in O(n^2), which is easily verified by taking c = a + |b| and n0 = max(1,-b/a).

where n0 is the value such that when n >= n0 we could show that an + b <= cn^2 in a proof of the above.

  1. I tried to verify this but I couldn't get very far :(
  2. How did they choose these values of c and n0? I know that the only thing that matters is that there exists such a c and n0 such that the above is true to prove that an + b is O(n^2) but I wonder how did they choose specifically those values of c and n0? They don't seem arbitrary, its as if they applied some technique I have never seen before to obtain them.

Thanks.


Solution

  • Let's take the simple case where a and b are both positive. What the authors are trying to do is to create a value where the quadratic function dominates the linear function for n >= 1. That's it. They're just trying to create a general solution to show where the right parabola dominates any line.

    So for n=1, the value of the linear function (i.e. l(n) = an + b) will be a+b when n=1. A dominating quadratic without any linear sub-functions (i.e. q(n) = c * n^2) would dominate the linear function, l(n) at n=1 if we choose c = a + b. So, q(n) = (a+b)n^2 dominates l(n) = an + b when n>=1, assuming a and b are both positive. You can check out examples for yourself for plotting 30x^2 and 10x + 20 on Densmos.

    It's a bit trickier when b is negative, but the positive case is basically the point.