Skip to the content
Kets & Gates homeOpen the app

Math toolkit

Big-O and repeated trials

How log n, √N, n, n² and 2ⁿ grow, and how repeating a test drives its failure chance down

about 15 min

Practise this in the app

In the app every question checks your answer and explains a wrong one.

Big-O: growth without the constants

When you compare algorithms, what matters is how the cost grows as the input grows, not the exact count. Big-O notation says this briefly: f(n)=O(g(n))f(n) = O(g(n)) means that ff grows no faster than gg, ignoring constant factors. Precisely, there is a constant CC with f(n)≤C g(n)f(n) \le C\,g(n) for every large enough nn.

For example, 3n2+5n=O(n2)3n^2 + 5n = O(n^2): once n≥5n \ge 5, 5n≤n25n \le n^2, so 3n2+5n≤4n23n^2 + 5n \le 4n^2. Constant factors and smaller terms drop out. Big-O is only an upper bound, so n=O(n2)n = O(n^2) is true as well; it just says less than n=O(n)n = O(n). When people quote a cost such as O(n2)O(n^2), they mean the smallest simple class that fits.

Check yourself

What is the smallest big-O class that contains f(n)=7n3+100n2+4f(n) = 7n^3 + 100n^2 + 4, written in its simplest form?

  1. AO(n3+n2)O(n^3 + n^2)
  2. BO(n2)O(n^2), since 100n2100n^2 is the biggest term for small nn
  3. CO(n3)O(n^3)
  4. DO(2n)O(2^n)
Show the answer

Answer: C. O(n3)O(n^3)

For n≥1n \ge 1, 7n3+100n2+4≤7n3+100n3+4n3=111n37n^3 + 100n^2 + 4 \le 7n^3 + 100n^3 + 4n^3 = 111n^3, so f=O(n3)f = O(n^3). No smaller power works, because the 7n37n^3 term eventually beats any constant times n2n^2.

Why not the others

  • A. That is the same class as O(n3)O(n^3), since n3+n2≤2n3n^3 + n^2 \le 2n^3. The simplest form drops the smaller term.
  • B. Big-O is about large nn. Once n>100/7n > 100/7, the term 7n37n^3 is larger than 100n2100n^2, and no constant times n2n^2 keeps up with n3n^3.
  • D. True, since ff grows no faster than 2n2^n, but far from the smallest class. 7n3+100n2+4≤111n37n^3 + 100n^2 + 4 \le 111n^3 for n≥1n \ge 1, so O(n3)O(n^3) already holds.

Five growth rates

Five functions come up again and again in this course: log⁡2n\log_2 n, n\sqrt n, nn, n2n^2 and 2n2^n. Each one eventually leaves the one before it far behind. Here they are at a few sizes, rounded:

n102050100log⁡2n3.34.35.66.6n3.24.57.110n102050100n210040025001042n1024≈106≈1015≈1030\begin{array}{r|rrrr} n & 10 & 20 & 50 & 100 \\ \hline \log_2 n & 3.3 & 4.3 & 5.6 & 6.6 \\ \sqrt n & 3.2 & 4.5 & 7.1 & 10 \\ n & 10 & 20 & 50 & 100 \\ n^2 & 100 & 400 & 2500 & 10^4 \\ 2^n & 1024 & \approx 10^6 & \approx 10^{15} & \approx 10^{30} \end{array}

Slow growers: on the scale of nn up to 100, log⁡2n\log_2 n hardly moves and n\sqrt n only reaches 10. Fast growers: 2n2^n and n2n^2 stay close up to n=4n = 4, where both are 16; after that 2n2^n leaves n2n^2 behind, and by n=9n = 9 it is off the chart.

Check yourself

From n=2n = 2 on, n3n^3 is larger than 2n2^n for a while. From which whole number nn on is 2n2^n larger than n3n^3 for good?

Hint
  1. Compare 2n2^n and n3n^3 at n=8,9,10n = 8, 9, 10.
Show the answer

Answer: 1010

29=512<729=932^9 = 512 < 729 = 9^3, but 210=1024>1000=1032^{10} = 1024 > 1000 = 10^3. From there 2n2^n doubles at each step while n3n^3 grows by a factor (1+1/n)3<2(1 + 1/n)^3 < 2, so 2n2^n stays ahead.

Common slips

  • 99: Not yet: 29=5122^9 = 512 is still below 93=7299^3 = 729.
  • 44: At n=4n = 4, 2n2^n catches n2n^2, not n3n^3: 24=162^4 = 16 while 43=644^3 = 64.

Watch which variable a cost is measured in. Grover search, in Part 2, takes about N\sqrt N steps to search NN items. If the items are labelled by nn⁠-bit strings, then N=2nN = 2^n and

N=2n/2.\sqrt N = 2^{n/2}.

That is still exponential in the number of bits nn, only with half the exponent. A square-root speedup of an exponential cost leaves an exponential cost. It is a real gain, since 2n/22^{n/2} is far smaller than 2n2^n, but it does not turn an exponential problem into a polynomial one.

Check yourself

A search space has N=240N = 2^{40} items, and N=2k\sqrt N = 2^k. What is kk?

Show the answer

Answer: 2020

240=240/2=220\sqrt{2^{40}} = 2^{40/2} = 2^{20}, about a million, against 2402^{40}, about a trillion. Still exponential in the number of bits.

Common slips

  • 3838: Taking a square root halves the exponent; it doesn’t subtract 2. 240=240/2\sqrt{2^{40}} = 2^{40/2}.
  • 6.326.32: That is 40\sqrt{40}. The square root applies to 2402^{40}, which halves its exponent: 2202^{20}.

Polynomial against exponential

A cost that grows like a power of the input size, such as nn, n2n^2 or n10n^{10}, is called polynomial. A cost that grows like 2n2^n or 2n/22^{n/2} is exponential. Polynomial-time algorithms count as efficient.

The difference shows when hardware improves. Suppose a computer becomes 1000 times faster. For an algorithm whose time grows like n2n^2, the largest input you can handle in the same time grows by a factor 1000≈31.6\sqrt{1000} \approx 31.6. For one whose time grows like 2n2^n, it grows by log⁡21000≈10\log_2 1000 \approx 10: ten more bits, however large nn already was. No faster machine fixes an exponential cost, which is why turning an exponential cost into a polynomial one is the prize.

Check yourself

An algorithm takes time proportional to n3n^3. A computer 1000 times faster arrives. By what factor does the largest input you can handle in the same time grow?

Hint
  1. If nn grows by a factor ff, by what factor does n3n^3 grow?
Show the answer

Answer: 1010

Multiplying nn by ff multiplies n3n^3 by f3f^3. The new machine does 1000 times the work in the same time, so f3=1000f^3 = 1000 and f=10f = 10.

Common slips

  • 10001000: The time per step falls by 1000, but the work grows like n3n^3: multiplying nn by ff multiplies the work by f3f^3. So f3=1000f^3 = 1000.
  • 31.631.6: That is 1000\sqrt{1000}, the factor for an n2n^2 algorithm. For n3n^3 take the cube root.

Check yourself

An algorithm takes time proportional to 2n2^n and handles n=50n = 50 in a day. On a computer 1000 times faster, about what nn can it handle in a day?

Hint
  1. Each extra bit doubles the running time. How many doublings make 1000?
Show the answer

Answer: 6060

The new machine does 1000 times the work in a day, so the new nn satisfies 2n=1000×2502^{n} = 1000\times2^{50}, which gives n=50+log⁡21000≈50+10=60n = 50 + \log_2 1000 \approx 50 + 10 = 60.

Common slips

  • 5000050000: That would hold for an algorithm whose time grows like nn. With 2n2^n, each extra bit doubles the time, so 1000 times the speed buys only log⁡21000≈10\log_2 1000 \approx 10 more bits.
  • 15811581: That multiplies nn by 1000\sqrt{1000}, which is right for an n2n^2 algorithm. With 2n2^n, 1000 times the speed adds about 10 to nn.

Repeating a test

Many algorithms succeed only with some probability pp per run, but you can check whether a run succeeded, for example by testing the answer it gives. Then you repeat until a run succeeds. All of kk independent runs fail with probability (1−p)k(1-p)^k, and since 1−p≤e−p1 - p \le e^{-p},

(1−p)k≤e−pk.(1-p)^k \le e^{-pk}.

To push the chance of failure below δ\delta, it is enough that e−pk≤δe^{-pk} \le \delta, that is k≥ln⁡(1/δ)/pk \ge \ln(1/\delta)/p. So about ln⁡(1/δ)/p\ln(1/\delta)/p runs suffice. The dependence on δ\delta is only logarithmic: going from a failure chance of one in a thousand to one in a million just doubles the number of runs, since ln⁡106=2ln⁡103\ln 10^6 = 2\ln 10^3.

Stop at a success: a test that succeeds with probability 14\tfrac14 per run. The chance that every run fails (bars) shrinks by a factor 34\tfrac34 per run and stays under e−k/4e^{-k/4} (dots). Majority vote: a vote over kk runs of an algorithm that is right 23\tfrac23 of the time; the next part explains it.

Check yourself

Each run succeeds with probability p=0.01p = 0.01, and you can tell when it has. Using k≥ln⁡(1/δ)/pk \ge \ln(1/\delta)/p, about how many runs make the chance that all of them fail at most δ=0.001\delta = 0.001?

Hint
  1. ln⁡1000≈6.91\ln 1000 \approx 6.91.
Show the answer

Answer: 691691

ln⁡(1/δ)/p=ln⁡(1000)/0.01≈6.91×100≈691\ln(1/\delta)/p = \ln(1000)/0.01 \approx 6.91\times100 \approx 691. The exact requirement (0.99)k≤0.001(0.99)^k \le 0.001 gives k≥688k \ge 688, so the bound costs almost nothing.

Common slips

  • 100100: That is 1/p1/p. After 100 runs all have failed with probability 0.99100≈0.370.99^{100} \approx 0.37, far above 0.0010.001. You need ln⁡(1000)≈6.9\ln(1000) \approx 6.9 times as many.
  • 10001000: That is 1/δ1/\delta. The number of runs depends on δ\delta only through ln⁡(1/δ)≈6.9\ln(1/\delta) \approx 6.9, and on pp through 1/p=1001/p = 100.
  • 100000100000: That multiplies 1/p1/p by 1/δ1/\delta. The dependence on δ\delta is logarithmic: multiply 1/p1/p by ln⁡(1/δ)≈6.9\ln(1/\delta) \approx 6.9 instead.

Majority vote

Some algorithms give an answer that is right with probability at least 23\tfrac23, and you can’t check the answer directly. Run such an algorithm kk times, with kk odd, and take the majority answer. The majority is wrong only if more than half the runs are wrong, and that becomes unlikely fast. A result called the Chernoff bound shows that the chance falls off exponentially in kk, roughly like e−cke^{-ck} for a constant cc that depends on how far the success chance is above 12\tfrac12.

In the majority-vote chart above, three runs give a wrong majority with probability 727≈0.26\tfrac{7}{27} \approx 0.26, and 21 runs with probability about 0.060.06. Any fixed success chance above 12\tfrac12 can be pushed close to certainty with a modest number of runs. That is why “right with probability 23\tfrac23” is the usual standard for a randomized algorithm: the 23\tfrac23 itself is arbitrary.

Check yourself

An algorithm answers correctly with probability 23\tfrac23. You run it three times and take the majority answer. What is the probability that the majority is wrong?

P(wrong)=  ?P(\text{wrong}) = \;?

Hints
  1. The majority is wrong when two or three of the runs are wrong.
  2. Exactly two wrong can happen in 3 ways, each with probability (13)2⋅23(\tfrac13)^2\cdot\tfrac23.
Show the answer

Answer: P(wrong)=727P(\text{wrong}) = \frac{7}{27}

Exactly two wrong: 3⋅(13)2⋅23=6273\cdot(\tfrac13)^2\cdot\tfrac23 = \tfrac{6}{27}. All three wrong: 127\tfrac{1}{27}. Together 727≈0.26\tfrac{7}{27} \approx 0.26, already better than 13\tfrac13 for one run.

Common slips

  • 127\frac{1}{27}: That is the chance that all three runs are wrong. The majority is also wrong when exactly two of the three are wrong: 3⋅(13)2⋅23=6273\cdot(\tfrac13)^2\cdot\tfrac23 = \tfrac{6}{27} more.
  • 29\frac{2}{9}: That is the chance that exactly two runs are wrong. Add the case where all three are wrong, (13)3=127(\tfrac13)^3 = \tfrac{1}{27}.
  • 13\frac{1}{3}: That is the chance for a single run. The majority needs at least two of the three runs to be wrong, which is less likely.

Go further

Free resources for when you want more than this refresher covers.

Lessons that use it

Practise this in the appAll refreshers