3

I'm working on some lecture notes and wondered if there was a good example algorithm for the recurrence equation

$$T(n) = T(n/2) + n$$

I know that I can do selection in this running time, but that is a probabilistic algorithm, and I was hoping not to introduce this algorithm at this point in my lecture notes. I plan to introduce the selection problem in a later chapter after describing quick-sort. Is there a good example of an algorithm that has this recurrence equation as worst-case?

2 Answers2

2

Bottom-up heap construction. Choose one application of sift-down as your unit of measurement. Then the recurrence is $T(n) = T(n/2) + n$.

If you want to go beyond that, you can say that choosing sift-down as a unit for measurement isn't quite accurate, and its runtime depends on the height $h$ of heap being sifted down in. But you can show that bottom-up heap construction is still $O(n)$ due to

$$0\cdot n + 1\cdot \frac{n}{2} + 2\cdot \frac{n}{4} + \cdots + h\cdot 1 = \sum_{h=0}^{\lg n} h\frac{n}{2^h} < n\sum_{h=0}^\infty \frac{h}{2^h}$$

and showing (or simply stating without proof in case you feel the students are not ready yet) that $\sum_{h=0}^\infty \frac{h}{2^h}$ converges to a constant.

orlp
  • 13,988
  • 1
  • 26
  • 41
0

I ended up using binary sum, i.e., adding the numbers in a list pairwise:

def binary_sum(x):
    if len(x) == 0: return 0
    if len(x) == 1: return x[0]

    # O(n) work
    y = []
    for i in range(1, len(x), 2):
        y.append(x[i - 1] + x[i])
    if i + 1 < len(x): # handle odd lengths
        y[0] += x[i + 1]

    return binary_sum(y) # recurse on n//2 size

I justify this with a story about numerical issues when adding numbers of very different orders of magnitude.