reading , and
counting the cost
what it is
is a ceiling on how a count grows, is a floor, and is both at once. Each holds from some size of input on.
Say a method takes exactly steps for items. The table sets that count beside and . At 34 items the count is still above .
| items, | the count | ||
|---|---|---|---|
| 1 | 523 | 4 | 10 |
| 10 | 1,000 | 400 | 1,000 |
| 20 | 2,100 | 1,600 | 4,000 |
| 34 | 4,648 | 4,624 | 11,560 |
| 35 | 4,875 | 4,900 | 12,250 |
| 100 | 32,500 | 40,000 | 100,000 |
So from 35 items on, the count never climbs above 4 times . That is a ceiling, and , read big O, names it: the count is in . The simpler formula in the brackets, , 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. writes neither number.
, read big omega, names a floor. The count never drops below 3 times : at 10 items, 300 against 1,000. So it is in . A floor needs the same two numbers, any pair that works, and writes neither. The multiple must be above zero: 0 times sits under every count and says nothing. A ceiling or a floor is called a bound.
, 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 : at 100 items, 30,000, 32,500 and 40,000. So it is in : it grows like .
A ceiling can be true but loose. The count is in too, yet no fixed multiple of 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 , 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.
the move: name the gap
the line:why it is allowed: is how far stands above the count. The count is at most where is zero or more.
the move: take the count away part by part
the line:why it is allowed: Taking away the count takes away each of its three parts.
guess it, thenthe move: take 3n squared from 4n squared
the line:why it is allowed: less is .
guess it, thenthe move: a fact about a square
the line:times minus 10 minus 10 times minus 10 minus 10 times 100 The four parts add to . At 13 items, 3 times 3 is 9, and 169 less 260 plus 100 is 9 too. why it is allowed: The plan: write as a square less a number, then ask when the square passes that number. Half of 20 is 10, so the square of gives back .
guess it, thenthe move: take 100 from both sides
the line:why it is allowed: Line 4, with 100 taken from both sides.
guess it, thenthe move: use line 5
the line:why it is allowed: Line 5 put in place of the of line 3.
guess it, thenthe move: add the two numbers
the line:why it is allowed: Taking away 100 and then 500 takes away 600.
guess it, thenthe move: the square up to 34 items
the line:why it is allowed: Below 10 items, 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.
guess it, thenthe move: the square from 35 items on
the line:why it is allowed: From 35 items on, is 25 or more, so its square is at least 25 times 25, which is 625.
guess it, thenthe move: read the sign of
the line:items, its square 1 minus 9 81 minus 519 10 0 0 minus 600 34 24 576 minus 24 35 25 625 25 40 30 900 300 is the square less 600. why it is allowed: By lines 7 and 8, up to 34 items 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.
guess it, thenthe move: read it for the count
the line:why it is allowed: By line 1, zero or more means the count is at most , and below zero, above it. Line 10 says where.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
the move: name the count
the line:why it is allowed: is the count. It has a squared part, , a part that grows like , and a fixed part, 500.
the move: a fact about n
the line:why it is allowed: For at least 1, times 1 is at most times .
guess it, thenthe move: a fact about 1
the line:why it is allowed: For at least 1, 1 times 1 is at most times .
guess it, thenthe move: a ceiling
the line:why it is allowed: By line 2, swap the of in line 1 for . The right side gets no smaller.
guess it, thenthe move: a higher ceiling
the line:why it is allowed: By line 3, swap the 500, which is 500 times 1, for 500 times . No smaller again.
guess it, thenthe move: add like with like
the line:why it is allowed: 3, 20 and 500 lots of make 523 lots. At 1 item the count is 523 too, so this ceiling touches it there.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 6, from 1 item on, never climbs above 523 times , and 523 is above zero. Read is in big O of squared.
guess it, thenthe move: a floor
the line:why it is allowed: Line 1 without , which is above zero. What is left is smaller.
guess it, thenthe move: read it as a bound
the line:- the count,
- the ceiling of line 6,
- the ceiling of question 1,
- the floor of line 8,
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 , crosses under it just before 35 items and stays under. runs far above: true, but generous. The count soon runs along its floor, : 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, never drops below 3 times , and 3 is above zero.
guess it, thenthe move: a ceiling and a floor together
the line:why it is allowed: Lines 7 and 9 give a ceiling and a floor with the same term, . From 1 item on, stays between 3 and 523 times .
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
the move: name the count
the line:why it is allowed: is the count of question 2, now only for 10 items or more.
the move: a fact for the middle part
the line:why it is allowed: 20 times is 2 times 10 times . With at least 10, swap that 10 for ; it gives no less.
guess it, thenthe move: a fact for the fixed part
the line:items, 500 5 100 50 500 125 10 200 200 500 500 20 400 800 500 2,000 100 2,000 20,000 500 50,000 At 5 items, and 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 at least 10, swap each 10 for ; it gives no less.
guess it, thenthe move: a ceiling
the line:why it is allowed: By line 2, swap the in line 1 for . The right side gets no smaller.
guess it, thenthe move: a higher ceiling
the line:why it is allowed: By line 3, swap the 500 for . No smaller again.
guess it, thenthe move: add like with like
the line:why it is allowed: 3, 2 and 5 lots of make 10 lots. At 10 items both sides are 1,000.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 6, from 10 items on, never climbs above 10 times . The claim of question 2 again, with another pair: a smaller multiple for a later start.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
the move: name the count
the line:why it is allowed: is the count of question 2.
the move: bring in the ceiling of question 2
the line:why it is allowed: Line 6 of question 2, which holds from 1 item on.
guess it, thenthe move: a fact about n
the line:why it is allowed: For at least 1, times 1 is at most times .
guess it, thenthe move: a higher ceiling
the line:why it is allowed: By line 3, swap the in line 2 for . The right side gets no smaller.
guess it, thenthe move: read it as a bound
the line:- the count,
- the ceiling of line 2,
- the ceiling of line 4,
- a small multiple, 0.01 times
A log-log plot from 10 to ten million items. soon keeps an almost even gap above the count, about 174 times it, since the count runs along and 523 over 3 is about 174. pulls further away with every power of ten. Even 0.01 times 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, never climbs above 523 times . So is in as well as in .
guess it, thenthe move: multiply line 2 by n
the line:why it is allowed: The plan: show that even 0.01 times ends up above , then any smaller multiple too. First, line 2 times , which is above zero, so 'at most' still holds: is at least times , 1,000 times at 1,000 items.
guess it, thenthe move: multiply by 0.01 over 523
the line:why it is allowed: Line 6 times 0.01 over 523, chosen to turn into . It is above zero, so 'at most' still holds.
guess it, thenthe move: cancel the 523
the line:why it is allowed: On the right, times 523 and over 523 cancel.
guess it, thenthe move: write the left side as a number times
the line:why it is allowed: Line 8's left side, with its set apart.
guess it, thenthe move: where that number is 1
the line: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.
guess it, thenthe move: read the number past 52,300 items
the line:items, 523 0.01 5,230 0.1 52,300 1 523,000 10 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 in line 9 grows with . By line 10 it is 1 at 52,300 items, so it is above 1 past them.
guess it, thenthe move: read it past 52,300 items
the line:why it is allowed: By line 11 the number is above 1 there, and is above zero, so the number times is more than . By line 9, is at least that, so it is above . The plot shows it much sooner, at 308 items: any size that works will do.
guess it, thenthe move: the same for any multiple
the line: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 is above past 523 divided by it. For 0.001, past 523,000 items.
guess it, thenthe move: so no floor of n cubed
the line:why it is allowed: A floor stays at or under from some size on. By line 13, past that size and the size line 13 gives, the multiple of is above . So it is no floor.
guess it, thenthe move: read the two ceilings together
the line:why it is allowed: Line 5 is true, but by line 14 no floor of sits under , so does not grow like : the ceiling overshoots. The ceiling of question 2 shares its term with a floor, , so it is tight.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
a worked example
Check the three pairs at 40 items, for the count .
- At 40 items, is 1,600. 3 times 1,600 is 4,800, 20 times 40 is 800, and 500 more make 6,100.
- 4 times 1,600 is 6,400, above 6,100, as the pair 4 from 35 items says.
- 10 times 1,600 is 16,000, and 523 times 1,600 is 836,800. Both are above too.
- 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
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. is under the count up to 34 items, so the size says where the claim starts.
The count is in and in . Which says more, and why?
the answer
. It shares its term with the floor , so the count is in . By question 4, no fixed multiple of is a floor under it, so is loose.
Someone says a method whose worst case is in takes about steps on its worst inputs. What is wrong?
the answer
is only a ceiling. The worst case could cost , which is in by question 4. At 1,000 items that is 3,020,500 steps, while is a billion, about 330 times more.