Recurrence relations

After thiswhat you will be able to doTurn T(n) = 2T(n/2) + n into a recursion tree and derive its n log n cost from n work on each of log n levels.

Questionwhat this lesson answersA recursive algorithm's cost is written in terms of its own cost on smaller inputs, so the cost equation refers to itself. How do you turn that self-reference into one plain cost, and why does splitting the work in half and combining the halves give n times log n?

Not coveredwhat this lesson leaves outWe solve one recurrence, the split-in-half-then-combine one, by drawing its tree and adding the levels. We do not give the general rule that reads the answer off any such recurrence.

Put the values in a list from smallest to largest. That job is called sorting. One useful way to do it begins by splitting the list into two equal groups. Put each group in order with the very same method. When both groups are ordered, look at their first remaining values, take the smaller one, and repeat until a new ordered list is full. That act of joining two ordered halves is called a merge.

The method has a strange feature. To sort a list of n{n} items, it first asks itself to sort two lists of n2{\dfrac{n}{2}} items. An algorithm that starts itself again on smaller inputs is doing recursion. Each start of the method, whether it is the original start or one made by an earlier start, is a call. A list of one item needs no split because it is already ordered. The smallest input at which this self-starting process stops is called a base case. Breaking a job into pieces, solving the pieces in the same manner, and joining their answers is also called divide and conquer.

Write down the self-reference

Let T(n){T(n)} mean the total amount of work needed to sort a list of n{n} items. The two smaller sorts cost T(n2){T\left(\dfrac{n}{2}\right)} each. The final merge has to visit the items across both halves once. The work of joining the solved halves back together is the combine step. A trip that looks at every item once is a pass, so the combine step costs about n{n} units of work. The cost therefore obeys

T(n)=2T(n2)+n.T(n) = 2T\left(\dfrac{n}{2}\right) + n.

The first piece says there are two smaller sorts. The second says that, after they return, one pass joins their results. An equation that defines a cost using that same cost on smaller inputs is called a recurrence relation. The word relation matters here: this line does not yet hand us a plain number. It passes the question down to smaller versions of itself. The base case is where that passing finally ends, because its cost is taken as given rather than split again.

There are many ways to push symbols through a recurrence. For this one, drawing says more. Put the original call at the top. Beneath it put the two calls it starts on half-sized lists. The calls directly started by one call are its children. Beneath those sit four calls on quarter-sized lists, then eight calls on eighth-sized lists, until the one-item base cases appear. A picture of the calls made by a recursion is called a recursion tree. One horizontal row of that picture is a level.

Now read the work row by row. The top level has one merge of size n{n}, so it has about n{n} units of combine work. The next level has two merges of size n2{\dfrac{n}{2}}. The level after that has four merges of size n4{\dfrac{n}{4}}. In each case, the number of calls and the size of each call trade against one another:

top level:1n=nnext level:2n2=nfollowing level:4n4=n.\begin{aligned} \text{top level:} &\quad 1 \cdot n = n \\ \text{next level:} &\quad 2 \cdot \dfrac{n}{2} = n \\ \text{following level:} &\quad 4 \cdot \dfrac{n}{4} = n. \end{aligned}

Every combine level does about n{n} work. That is the whole useful balance in this tree. More calls do not automatically mean more work, because each call has less input to combine. The bottom row has many one-item lists, but it is the stopping row, not another row of merges. If a one-item base case costs a fixed amount, all of them together add an amount that grows in step with n, which the earlier lessons called a linear term. The combine work is the part that explains the larger shape.

Step 1 of 4

One merge at the top

Start with the one call that sees the whole list. Once its two sorted halves return, joining them asks it to look across every item once.

Split, then join

The work repeats across the combine rows

Each box is one call and its input size. Lines point to the two smaller calls it starts. The note beside a row counts the joining work after that row's calls return.

Items in the list
88 merge work1 call of 8

Every row has the same calls-times-input-size total. The bottom row contains one-item lists, so it stops rather than merging. The rows above it turn that total into merge work.

Items
8
Merge work per row
8
Combine rows
3
Total merge work
24

8 items give 3 combine rows. Each has 8 units of merge work, for 24 units in total.

Count the equal rows

The only remaining question is how many combine levels there are. The input size goes from n{n} to n2{\dfrac{n}{2}}, then to n4{\dfrac{n}{4}}, and continues to halve until it reaches one. The logarithms lesson already counted that process: the number of halvings is log2n{\log_2 n}. This lesson uses that count rather than deriving it again.

Use j{j} only as a label for the combine rows, starting at one. Adding the same n{n} work over all of those rows gives

combine work=j=1log2nn=nlog2n.\begin{aligned} \text{combine work} &= \sum_{j = 1}^{\log_2 n} n \\ &= n \cdot \log_2 n. \end{aligned}

So the total sorting cost is described, up to the base cases’ linear total, by

T(n)nlog2n.T(n) \approx n \cdot \log_2 n.

This is where the shape comes from. Splitting in half gives the log2n{\log_2 n} levels. Combining everything on each level gives the n{n}. Neither factor was guessed from the code’s appearance: the tree separates them so they can be counted.

The written answer still says base two. That base is a real record of how the list was split. To write nlogn{n \log n} instead, the base is deliberately dropped. The logarithms lesson showed that changing bases only multiplies a logarithm by a fixed constant. The earlier lesson on throwing away fixed multipliers is what permits that shorter form. It is a decision about what the growth comparison will stop distinguishing, not a claim that base two and every other base produce the same number of operations.

This tree solved one particular self-referential cost: two equal pieces and one full pass to join them. Change the number of pieces, or change the cost of joining them, and the balance between the work per row and the number of rows changes. In some shapes, one row contributes most of the total. The Master theorem door is the general rule for reading that answer from a recurrence’s shape without drawing every tree.

Doorswhat to read next, and why

Symbolswhat each one means, and whether we defined it, measured it, or just started there

nStatus: defined
the number of items in the list being sorted
T(n)Status: defined
the total cost of the algorithm on an input of size n, the quantity the equation defines
jStatus: defined
a plain label for the combine rows of the tree, counted from one up to the number of rows
T(n/2)Status: defined
the cost of the same algorithm on half the input, which is why the equation refers to itself
the combine stepStatus: defined
the work done to join the solved halves back together, here one pass costing about n
a base caseStatus: bottoms out
the smallest input the recursion stops at, whose cost is taken as given rather than split further
What these classifications mean
defined
circular by construction, true because we chose it
empirical
a measured claim about the world that could have come out otherwise
bottoms out
a primitive of the model, with nothing under it here
door
used here, explained elsewhere