Skip to content

reading OO, Ω\Omega and Θ\Theta

counting the cost

what it is

OO is a ceiling on how a count grows, Ω\Omega is a floor, and Θ\Theta is both at once. Each holds from some size of input on.

Say a method takes exactly 3n2+20n+5003n^2 + 20n + 500 steps for nn items. The table sets that count beside 4n24n^2 and 10n210n^2. At 34 items the count is still above 4n24n^2.

items, nnthe count4n24n^210n210n^2
1523410
101,0004001,000
202,1001,6004,000
344,6484,62411,560
354,8754,90012,250
10032,50040,000100,000
The count, 3n2+20n+5003n^2 + 20n + 500, beside 4n24n^2 and 10n210n^2. 10n210n^2 catches the count at 10 items and stays at or over it.

So from 35 items on, the count never climbs above 4 times n2n^2. That is a ceiling, and OO, read big O, names it: the count is in O(n2)O(n^2). The simpler formula in the brackets, n2n^2, is called the term.

A ceiling needs two numbers: a fixed multiple, here 4, and a size it holds from, here 35. Fixed means the same at every size. Any pair that works will do: 10 from 10 items, or 523 from 1. O(n2)O(n^2) writes neither number.

Ω\Omega, read big omega, names a floor. The count never drops below 3 times n2n^2: at 10 items, 300 against 1,000. So it is in Ω(n2)\Omega(n^2). A floor needs the same two numbers, any pair that works, and Ω(n2)\Omega(n^2) writes neither. The multiple must be above zero: 0 times n2n^2 sits under every count and says nothing. A ceiling or a floor is called a bound.

Θ\Theta, read theta, is a ceiling and a floor with the same term: the tight bound. From 35 items on the count stays between 3 and 4 times n2n^2: at 100 items, 30,000, 32,500 and 40,000. So it is in Θ(n2)\Theta(n^2): it grows like n2n^2.

A ceiling can be true but loose. The count is in O(n3)O(n^3) too, yet no fixed multiple of n3n^3 is a floor under it. Questions 1 to 4 prove each claim.

where the formulas come from

Each question is worked out one numbered line at a time. A line shows the move it makes, the line itself, and why the move is allowed. A line either follows from the lines above it, or brings in something the question needs, such as how the method works, a small fact about nn, or a result from another page, with its own reason. Press 'hide the lines, then guess each one' to cover the lines and uncover them one by one, guessing each before you see it.

question 1With the multiple 4, from what size is 3n2+20n+5003n^2 + 20n + 500 at most 4n24n^2?
  1. the move: name the gap

    the line:
    D=4n2−(3n2+20n+500)D = 4n^2 - (3n^2 + 20n + 500)

    why it is allowed: DD is how far 4n24n^2 stands above the count. The count is at most 4n24n^2 where DD is zero or more.

  2. the move: take the count away part by part

    the line:
    D=4n2−3n2−20n−500D = 4n^2 - 3n^2 - 20n - 500

    why it is allowed: Taking away the count takes away each of its three parts.

  3. the move: take 3n squared from 4n squared

    the line:
    D=n2−20n−500D = n^2 - 20n - 500

    why it is allowed: 4n24n^2 less 3n23n^2 is n2n^2.

  4. the move: a fact about a square

    the line:
    (n−10)2=n2−20n+100(n - 10)^2 = n^2 - 20n + 100
    timesnnminus 10
    nnn2n^2minus 10 times nn
    minus 10minus 10 times nn100
    The four parts add to n2−20n+100n^2 - 20n + 100. At 13 items, 3 times 3 is 9, and 169 less 260 plus 100 is 9 too.

    why it is allowed: The plan: write DD as a square less a number, then ask when the square passes that number. Half of 20 is 10, so the square of n−10n - 10 gives back n2−20nn^2 - 20n.

  5. the move: take 100 from both sides

    the line:
    (n−10)2−100=n2−20n(n - 10)^2 - 100 = n^2 - 20n

    why it is allowed: Line 4, with 100 taken from both sides.

  6. the move: use line 5

    the line:
    D=(n−10)2−100−500D = (n - 10)^2 - 100 - 500

    why it is allowed: Line 5 put in place of the n2−20nn^2 - 20n of line 3.

  7. the move: add the two numbers

    the line:
    D=(n−10)2−600D = (n - 10)^2 - 600

    why it is allowed: Taking away 100 and then 500 takes away 600.

  8. the move: the square up to 34 items

    the line:
    up to 34 items, (n−10)2is at most 576\begin{gathered} \text{up to 34 items, } (n - 10)^2 \\ \text{is at most 576} \end{gathered}

    why it is allowed: Below 10 items, n−10n - 10 is minus 9 to minus 1, so its square is at most 81, under 576. From 10 to 34 items it is 0 to 24, so its square is at most 24 times 24, which is 576.

  9. the move: the square from 35 items on

    the line:
    from 35 items on, (n−10)2is at least 625\begin{gathered} \text{from 35 items on, } (n - 10)^2 \\ \text{is at least 625} \end{gathered}

    why it is allowed: From 35 items on, n−10n - 10 is 25 or more, so its square is at least 25 times 25, which is 625.

  10. the move: read the sign of DD

    the line:
    D is below zeroup to 34 items, abovezero from 35 on\begin{gathered} D \text{ is below zero} \\ \text{up to 34 items, above} \\ \text{zero from 35 on} \end{gathered}
    items, nnn−10n - 10its squareDD
    1minus 981minus 519
    1000minus 600
    3424576minus 24
    352562525
    4030900300
    DD is the square less 600.

    why it is allowed: By lines 7 and 8, up to 34 items DD is at most 576 less 600, below zero. By lines 7 and 9, from 35 items on it is at least 625 less 600, which is 25.

  11. the move: read it for the count

    the line:
    the count is at most 4n2from 35 items on\begin{gathered} \text{the count is at most } 4n^2 \\ \text{from 35 items on} \end{gathered}

    why it is allowed: By line 1, DD zero or more means the count is at most 4n24n^2, and below zero, above it. Line 10 says where.

try it with a small nn: at n=4n = 4, line 1 gives D=−564D = -564, which you can work out by hand

question 2Is 3n2+20n+5003n^2 + 20n + 500 in O(n2)O(n^2), in Ω(n2)\Omega(n^2), and so in Θ(n2)\Theta(n^2)?
  1. the move: name the count

    the line:
    C=3n2+20n+500C = 3n^2 + 20n + 500

    why it is allowed: CC is the count. It has a squared part, 3n23n^2, a part that grows like nn, and a fixed part, 500.

  2. the move: a fact about n

    the line:
    n≤n2n \le n^2

    why it is allowed: For nn at least 1, nn times 1 is at most nn times nn.

  3. the move: a fact about 1

    the line:
    1≤n21 \le n^2

    why it is allowed: For nn at least 1, 1 times 1 is at most nn times nn.

  4. the move: a ceiling

    the line:
    C≤3n2+20n2+500C \le 3n^2 + 20n^2 + 500

    why it is allowed: By line 2, swap the nn of 20n20n in line 1 for n2n^2. The right side gets no smaller.

  5. the move: a higher ceiling

    the line:
    C≤3n2+20n2+500n2C \le 3n^2 + 20n^2 + 500n^2

    why it is allowed: By line 3, swap the 500, which is 500 times 1, for 500 times n2n^2. No smaller again.

  6. the move: add like with like

    the line:
    C≤523n2C \le 523n^2

    why it is allowed: 3, 20 and 500 lots of n2n^2 make 523 lots. At 1 item the count is 523 too, so this ceiling touches it there.

  7. the move: read it as a bound

    the line:
    C∈O(n2)C \in O(n^2)

    why it is allowed: By line 6, from 1 item on, CC never climbs above 523 times n2n^2, and 523 is above zero. Read CC is in big O of nn squared.

  8. the move: a floor

    the line:
    C≥3n2C \ge 3n^2

    why it is allowed: Line 1 without 20n+50020n + 500, which is above zero. What is left is smaller.

  9. the move: read it as a bound

    the line:
    C∈Ω(n2)C \in \Omega(n^2)
    102104106108101010121014101610110210310410510610710110210310410510610710810910101011101210131014101510161017101102103104105106107
    • the count, 3n2+20n+5003n^2 + 20n + 500
    • the ceiling of line 6, 523n2523n^2
    • the ceiling of question 1, 4n24n^2
    • the floor of line 8, 3n23n^2
    A log-log plot from 10 to ten million items: each step across or up is ten times the one before. The count, in heavy ink, starts above 4n24n^2, crosses under it just before 35 items and stays under. 523n2523n^2 runs far above: true, but generous. The count soon runs along its floor, 3n23n^2: at 1,000 items the floor is 3,000,000 and the count 3,020,500.

    why it is allowed: By line 8, from 1 item on, CC never drops below 3 times n2n^2, and 3 is above zero.

  10. the move: a ceiling and a floor together

    the line:
    C∈Θ(n2)C \in \Theta(n^2)

    why it is allowed: Lines 7 and 9 give a ceiling and a floor with the same term, n2n^2. From 1 item on, CC stays between 3 and 523 times n2n^2.

try it with a small nn: at n=4n = 4, line 1 gives C=628C = 628, which you can work out by hand

question 3From 10 items on, does a smaller multiple than 523 work?
  1. the move: name the count

    the line:
    C=3n2+20n+500C = 3n^2 + 20n + 500

    why it is allowed: CC is the count of question 2, now only for 10 items or more.

  2. the move: a fact for the middle part

    the line:
    20n≤2n220n \le 2n^2

    why it is allowed: 20 times nn is 2 times 10 times nn. With nn at least 10, swap that 10 for nn; it gives no less.

  3. the move: a fact for the fixed part

    the line:
    500≤5n2500 \le 5n^2
    items, nn20n20n2n22n^25005n25n^2
    510050500125
    10200200500500
    204008005002,000
    1002,00020,00050050,000
    At 5 items, 2n22n^2 and 5n25n^2 fall short of the parts they stand for. At 10 items each matches its part. After that they pull ahead.

    why it is allowed: 500 is 5 times 10 times 10. With nn at least 10, swap each 10 for nn; it gives no less.

  4. the move: a ceiling

    the line:
    C≤3n2+2n2+500C \le 3n^2 + 2n^2 + 500

    why it is allowed: By line 2, swap the 20n20n in line 1 for 2n22n^2. The right side gets no smaller.

  5. the move: a higher ceiling

    the line:
    C≤3n2+2n2+5n2C \le 3n^2 + 2n^2 + 5n^2

    why it is allowed: By line 3, swap the 500 for 5n25n^2. No smaller again.

  6. the move: add like with like

    the line:
    C≤10n2C \le 10n^2

    why it is allowed: 3, 2 and 5 lots of n2n^2 make 10 lots. At 10 items both sides are 1,000.

  7. the move: read it as a bound

    the line:
    C∈O(n2)C \in O(n^2)

    why it is allowed: By line 6, from 10 items on, CC never climbs above 10 times n2n^2. The claim of question 2 again, with another pair: a smaller multiple for a later start.

try it with a small nn: at n=10n = 10, line 1 gives C=1,000C = 1,000, which you can work out by hand

question 4Is 3n2+20n+5003n^2 + 20n + 500 in O(n3)O(n^3) too, and does that say much?
  1. the move: name the count

    the line:
    C=3n2+20n+500C = 3n^2 + 20n + 500

    why it is allowed: CC is the count of question 2.

  2. the move: bring in the ceiling of question 2

    the line:
    C≤523n2C \le 523n^2

    why it is allowed: Line 6 of question 2, which holds from 1 item on.

  3. the move: a fact about n

    the line:
    n2≤n3n^2 \le n^3

    why it is allowed: For nn at least 1, n2n^2 times 1 is at most n2n^2 times nn.

  4. the move: a higher ceiling

    the line:
    C≤523n3C \le 523n^3

    why it is allowed: By line 3, swap the n2n^2 in line 2 for n3n^3. The right side gets no smaller.

  5. the move: read it as a bound

    the line:
    C∈O(n3)C \in O(n^3)
    10210410610810101012101410161018102010221024101102103104105106107101102103104105106107108109101010111012101310141015101610171018101910201021102210231024101102103104105106107
    • the count, 3n2+20n+5003n^2 + 20n + 500
    • the ceiling of line 2, 523n2523n^2
    • the ceiling of line 4, 523n3523n^3
    • a small multiple, 0.01 times n3n^3
    A log-log plot from 10 to ten million items. 523n2523n^2 soon keeps an almost even gap above the count, about 174 times it, since the count runs along 3n23n^2 and 523 over 3 is about 174. 523n3523n^3 pulls further away with every power of ten. Even 0.01 times n3n^3 passes the count, at 308 items: about 292,181 against 291,252, where 307 items gave about 289,344 against 289,387.

    why it is allowed: By line 4, from 1 item on, CC never climbs above 523 times n3n^3. So CC is in O(n3)O(n^3) as well as in O(n2)O(n^2).

  6. the move: multiply line 2 by n

    the line:
    nC≤523n3nC \le 523n^3

    why it is allowed: The plan: show that even 0.01 times n3n^3 ends up above CC, then any smaller multiple too. First, line 2 times nn, which is above zero, so 'at most' still holds: 523n3523n^3 is at least nn times CC, 1,000 times CC at 1,000 items.

  7. the move: multiply by 0.01 over 523

    the line:
    0.01 nC523≤0.01×523n3523\frac{0.01 \, nC}{523} \le \frac{0.01 \times 523n^3}{523}

    why it is allowed: Line 6 times 0.01 over 523, chosen to turn 523n3523n^3 into 0.01n30.01n^3. It is above zero, so 'at most' still holds.

  8. the move: cancel the 523

    the line:
    0.01 nC523≤0.01n3\frac{0.01 \, nC}{523} \le 0.01n^3

    why it is allowed: On the right, times 523 and over 523 cancel.

  9. the move: write the left side as a number times CC

    the line:
    0.01n523×C≤0.01n3\frac{0.01n}{523} \times C \le 0.01n^3

    why it is allowed: Line 8's left side, with its CC set apart.

  10. the move: where that number is 1

    the line:
    0.01×52,300523=1\frac{0.01 \times 52{,}300}{523} = 1

    why it is allowed: 52,300 is 523 divided by 0.01. So 0.01 times 52,300 is 523, and 523 over 523 is 1.

  11. the move: read the number past 52,300 items

    the line:
    past 52,300 items, the numberin front of C is above 1\begin{gathered} \text{past 52,300 items, the number} \\ \text{in front of } C \text{ is above 1} \end{gathered}
    items, nn0.01n523\frac{0.01n}{523}
    5230.01
    5,2300.1
    52,3001
    523,00010
    Ten times the items, ten times the number. It is 1 at 52,300 items and above 1 after.

    why it is allowed: The number in front of CC in line 9 grows with nn. By line 10 it is 1 at 52,300 items, so it is above 1 past them.

  12. the move: read it past 52,300 items

    the line:
    past 52,300 items, 0.01n3is above C\begin{gathered} \text{past 52,300 items, } 0.01n^3 \\ \text{is above } C \end{gathered}

    why it is allowed: By line 11 the number is above 1 there, and CC is above zero, so the number times CC is more than CC. By line 9, 0.01n30.01n^3 is at least that, so it is above CC. The plot shows it much sooner, at 308 items: any size that works will do.

  13. the move: the same for any multiple

    the line:
    any fixed multiple of n3passes C in the end\begin{gathered} \text{any fixed multiple of } n^3 \\ \text{passes } C \text{ in the end} \end{gathered}

    why it is allowed: Lines 7 to 12 used only that 0.01 is above zero. Any fixed number above zero works the same: that many times n3n^3 is above CC past 523 divided by it. For 0.001, past 523,000 items.

  14. the move: so no floor of n cubed

    the line:
    no fixed multiple of n3is a floor under C\begin{gathered} \text{no fixed multiple of } n^3 \\ \text{is a floor under } C \end{gathered}

    why it is allowed: A floor stays at or under CC from some size on. By line 13, past that size and the size line 13 gives, the multiple of n3n^3 is above CC. So it is no floor.

  15. the move: read the two ceilings together

    the line:
    true, but aloose ceiling\begin{gathered} \text{true, but a} \\ \text{loose ceiling} \end{gathered}

    why it is allowed: Line 5 is true, but by line 14 no floor of n3n^3 sits under CC, so CC does not grow like n3n^3: the ceiling overshoots. The ceiling 523n2523n^2 of question 2 shares its term with a floor, 3n23n^2, so it is tight.

try it with a small nn: at n=4n = 4, line 1 gives C=628C = 628, which you can work out by hand

a worked example

Check the three pairs at 40 items, for the count 3n2+20n+5003n^2 + 20n + 500.

  1. At 40 items, n2n^2 is 1,600. 3 times 1,600 is 4,800, 20 times 40 is 800, and 500 more make 6,100.
  2. 4 times 1,600 is 6,400, above 6,100, as the pair 4 from 35 items says.
  3. 10 times 1,600 is 16,000, and 523 times 1,600 is 836,800. Both are above too.
  4. A check at one size cannot prove a pair. Questions 1 to 3 prove each pair at every size from its start.

the trap

check yourself

  1. Why does each claim on this page give a size as well as a multiple?

    the answer

    A multiple that suits big inputs can fail on small ones. 4n24n^2 is under the count up to 34 items, so the size says where the claim starts.

  2. The count 3n2+20n+5003n^2 + 20n + 500 is in O(n2)O(n^2) and in O(n3)O(n^3). Which says more, and why?

    the answer

    O(n2)O(n^2). It shares its term with the floor 3n23n^2, so the count is in Θ(n2)\Theta(n^2). By question 4, no fixed multiple of n3n^3 is a floor under it, so O(n3)O(n^3) is loose.

  3. Someone says a method whose worst case is in O(n3)O(n^3) takes about n3n^3 steps on its worst inputs. What is wrong?

    the answer

    O(n3)O(n^3) is only a ceiling. The worst case could cost 3n2+20n+5003n^2 + 20n + 500, which is in O(n3)O(n^3) by question 4. At 1,000 items that is 3,020,500 steps, while n3n^3 is a billion, about 330 times more.