recurrences, unrolled
the maths you need
what it is
Binary search on 8 numbers, for a number bigger than all of them, costs 1 look plus binary search on 4. A cost that refers to itself at a smaller size is a recurrence. Unroll it level by level, and add the levels up.
Binary search looks for a wanted number in a row in order, smallest first. Take 8 numbers and look for 96, bigger than all of them, so the search never stops early. It looks at the middle number. With two middles it takes the first, here 41. That is too small, so 96 can only come after it. The search keeps those 4 numbers and does the same on them. So the search on 8 costs 1 look plus the search on 4.
The search on 4 is a smaller copy of the same problem, and it has a smaller copy of its own. Each size is a level: the row of 8 is level 0, the copy of 4 level 1, then 2 and 1. Each level does 1 look, at 41, 68, 75 and 90, so 4 looks in all. A cost given by the same cost at a smaller size, plus the work at this size, is a recurrence. To unroll it, write each level's work in a table, then add the levels up.
How a method makes its smaller copies is its shape. This page has three. Halving makes one copy, half the size, as binary search does. Splitting in two makes two copies, each half the size, as merge sort does. Taking one off makes one copy, one item shorter, as selection sort does.
Merge sort runs merge sort on each half of the row, then one merge joins the two. A row of 1 number is in order already. A merge joins two runs in order into one. While both have numbers left, it places the smaller front number next. Once one is used up, the rest of the other follows. Each number placed is a placing. So merge sort on 8 costs two merge sorts on 4 plus 8 placings. Selection sort takes the first number as the smallest found so far. It compares each other number with the smallest so far, keeping the smaller, then swaps the smallest to the front. So selection sort on 5 costs 4 comparisons plus selection sort on the 4 after the front.
Halving and splitting in two use rows of items, so every halving comes out exact. , read 2 to the power , is twos multiplied together: 8 is 2 to the power 3. Each halving takes one 2 away, so items halve times down to 1, and half of 2 is 1, so 2 to the power 0 is 1. Taking one off works on rows of any length, so there counts the items, not the halvings. On items it costs , the adding up that stops one short of , which is .
| shape | method | copies it passes down | work at a level | in all |
|---|---|---|---|---|
| halving | binary search, for a number bigger than all | one, half the size | 1 look | , on items |
| splitting in two | merge sort | two, each half the size | a placing for each item, at each level that merges | , on items |
| taking one off | selection sort | one, one item shorter | 1 comparison fewer than its items | , on items |
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: unroll it into levels
the line:Binary search on 8, unrolled. Levels 1 to 3 keep the half after the look above. Dashed, that look and the numbers before it. Each level does 1 look, 4 in all. level copies items in each looks at this level 0 1 8 1 1 1 4 1 2 1 2 1 3 1 1 1 in all 4 The levels for 8 items, with as 3. why it is allowed: Let be a level's number, 0 at the top. After halvings, twos are left, so level holds items. For 8, level 1 holds 4, which is 2 to the power 2. The levels stop at level , with no 2 left: 2 to the power 0 is 1 item. Each level's step is one look, and as in the first picture each look before the last keeps exactly the half after it.
the move: add up the levels
the line:why it is allowed: Let be the cost, the looks of every level added. , read the sum of, adds the term after it, here 1, once for each , from 0 under the sign to over it. For 8, is 3, so it is 1 + 1 + 1 + 1.
guess it, thenthe move: count the levels
the line:8 halves to 4, 2 and 1. That is 3 halvings, so 4 levels and 4 looks. why it is allowed: The levels run from 0 to , which is of them, each adding 1. For 8, 4 looks.
guess it, thenthe move: read it
the line:why it is allowed: By line 3 the cost is one more than the halvings. The page 'halving and logarithms' names that count the log to base 2 of the size. The number line at line 3 writes it under each number, 3 under the 8. Doubling to 16 items adds one halving, so one look: 5 against 4.
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
the move: unroll it into levels
the line:The site's merge sort on 8 numbers, runs of 1, then 2, 4 and 8. Rounds 1, 2 and 3 make the copies of levels 2, 1 and 0. Its last merge places all 8. level copies items in each placings at this level 0 1 8 8 1 2 4 8 2 4 2 8 3 8 1 0 in all 24 The levels for 8 items, with as 3. why it is allowed: Each copy splits in two, so the copies double at each level: copies at level . Each holds items, as in question 1. Cutting a row costs nothing here; only placings count. Each copy's merge places each of its items once. A copy of 1 item merges nothing, so the levels that merge run from 0 to .
the move: the work at a level
the line:Merge sort on 8, unrolled. Each copy of 2 or more splits into the two under it. Each level that merges places all 8, and the copies of 1 place none, 24 in all. why it is allowed: By line 1, level holds copies, each placing its items once. For 8, level 1 holds 2 copies of 4, so 2 times 4, 8 placings.
guess it, thenthe move: add up the levels that merge
the line:why it is allowed: Let be the cost, the placings of every level that merges added. When is 3, as 0, 1 and 2 gives 1 times 8, 2 times 4 and 4 times 2. That is a term for each row of the table that merges, 8 + 8 + 8.
guess it, thenthe move: each level places the whole input
the line:why it is allowed: twos times more twos make twos. So each level places , the whole input, whatever is.
guess it, thenthe move: count the levels that merge
the line:why it is allowed: The levels that merge run from 0 to , which is of them, each adding . For 8, 3 times 8 is 24.
guess it, thenthe move: read it
the line:why it is allowed: is the size and is its log to base 2. So by line 5 the cost is the size times its log, 24 placings for 8 items, as the site's merge sort counts them.
guess it, then
try it with a small : at , line 3 gives , which you can work out by hand
the move: unroll it into levels
the line:the row 15, 24, 38, 62, 7 after pass 1 7, 24, 38, 62, 15 after pass 2 7, 15, 38, 62, 24 after pass 3 7, 15, 24, 62, 38 after pass 4 7, 15, 24, 38, 62 The site's selection sort on 15, 24, 38, 62, 7. Each pass swaps the smallest of the rest to its front, leaving a row one shorter. Selection sort on 5, unrolled, each level named by its items. The level of 4 is the row after pass 1, less its settled front in blue, and so on. The levels compare 4, 3, 2, 1 and 0, 10 in all. why it is allowed: Here is the number of items, not a count of halvings. Name a level by its items, , from down to 1. At a level of items, each of the after the first is compared once with the smallest so far. The level of 1 item compares nothing. So levels work, 4 for 5 items.
the move: add up the levels from the smallest
the line:why it is allowed: Let be the cost, the comparisons of every level added, for at least 2. By line 1 the level of 2 makes 1, the level of 3 makes 2, and on, up to at the level of . For 5, 1 + 2 + 3 + 4.
guess it, thenthe move: use the page on adding up, reading its n as n - 1
the line:Two staircases of 1 to 4 squares, yellow hanging and blue standing, fill a rectangle 4 wide and 5 tall, 20 squares. So one staircase, 1 + 2 + 3 + 4, is 10. where does this come from?
question 1 of the page adding up Fit two staircases of squares together. What shape do they make? the move: draw the sum as a staircase
the line:why it is allowed: Let be the sum of the whole numbers from 1 to . Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to . For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.
the move: take two staircases
the line:why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.
the move: fit them into a rectangle
the line:Blue stands, yellow hangs. Two staircases of 1 to 5 squares fill a rectangle 5 wide and 6 tall, 30 squares. So 1 + 2 + 3 + 4 + 5 is 5 times 6, halved, which is 15. why it is allowed: Stand one staircase with its tallest column on the left. Hang the other upside down from the top, its tallest column on the right, as in the picture. The first column holds standing and 1 hanging. Each column to the right, the standing one loses a square and the hanging one gains one. So every column holds , and the columns fill a rectangle wide and tall.
the move: halve both sides
the line:why it is allowed: Halving both sides of line 3 keeps it true and leaves one . For 5, half of 30 is 15.
why it is allowed: The page on adding up finds that the whole numbers from 1 up to any last number add up to that number times one more than it, halved. Here the sum stops at . So read that page's as this page's . For 5 items the sum is 1 + 2 + 3 + 4, that page's sum with its as 4. And 4 times 5, halved, is 10.
guess it, thenthe move: tidy the bracket
the line:why it is allowed: is . For 5, 4 times 5, halved, is 10.
guess it, thenthe move: multiply out the bracket
the line:why it is allowed: lots of are lots of , less one . That is , where , read squared, is times . For 5, 4 times 5 is 20, and 25 less 5 is 20 too.
guess it, thenthe move: halve each part
the line:why it is allowed: Half of a difference is the difference of the halves. For 5, half of 20 is 10, and 12.5 less 2.5 is 10 too.
guess it, thenthe move: take out the biggest part
the line:- the comparisons,
- half of the items squared,
From 1 to 20 items, the comparisons sit just under half the items squared. The gap is half the items, so at 20 it is 190 against 200. items half of the items squared divided by it 5 10 12.5 0.8 10 45 50 0.9 100 4,950 5,000 0.99 1,000 499,500 500,000 0.999 Selection sort's comparisons beside half of the items times the items. The last column, 1 take away 1 over the items, climbs towards 1. why it is allowed: is times 1, and is times 1 over . Multiplying spreads over a difference, line 6 backwards, so take out of both parts. For 5, 12.5 less 2.5 is 12.5 times 0.8, which is 10. The bracket is divided by , the last column of the table.
guess it, thenthe move: read it
the line:why it is allowed: By line 7, as grows, 1 over shrinks towards nothing, 0.01 at 100. So for a big input the cost is about the size times itself, halved: 4,950 for 100 items, close to 5,000. For 5 items the part taken away still counts, 10 against 12.5.
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
a worked example
Unroll merge sort's cost for 16 items, level by level.
- 16 is with as 4, so the levels run from 0 to 4. Level 4 holds copies of 1 item, which merge nothing.
- Levels 0 to 3 hold 1 copy of 16, 2 of 8, 4 of 4 and 8 of 2. Each level places all 16.
- Four levels of 16 make 64. And with as 4 is 4 times 16, 64 too.
the trap
check yourself
Each level halves the input and does one step. How many steps for 64 items?
the answer
64 is with as 6. By question 1 that is , 7 steps.
How many placings does merge sort make on 1,024 items?
the answer
1,024 is with as 10. By question 2 that is , 10 times 1,024, which is 10,240.
How many comparisons does selection sort make on 30 numbers?
the answer
By question 3, 29 times 30, halved, which is 435.