Recursive sum: recurrence T(n) = T(n-1) + 2 solved step by step

Build the recurrence from the code (direct solution, divide, conquer, combine) and solve T(n) = T(n-1) + 2 by backward substitution, forward chaining and the characteristic polynomial.

The question

Consider the recursive algorithm below. Algorithm RecSum (A, n) IF n == 0 RETURN 0 ELSE: RETURN A[n] + RecSum(A, n-1) END Count the arithmetic operations. A holds n numbers, A[1] … A[n]. Part 1: Write the recurrence for the worst case and solve it by backward substitution, forward chaining and the characteristic polynomial (Big O). Part 2: Find the best case (Big Ω). Part 3: Find Big Θ.

The recurrence and backward substitution

For a recursive algorithm we write a recurrence, using the simplified analysis: count the arithmetic, part by part. First the direct solution: when n == 0 it returns 0 straight away, with no arithmetic.

Divide: making the smaller problem costs one subtraction, n-1.

Conquer: the recursive call solves a problem of size n-1. That costs T(n-1).

Combine: one addition puts the answer together, A[n] + ….

Put the parts together: T(n) = T(n-1) + 2 for n > 0, with T(0) = 0. Now we solve it, three ways.

Step 1 · Draft: write the recurrence again for n-1, n-2, n-3: each one subtracts one more.

Those are the drafts we will plug in.

Step 2 · Applying the draft: replace T(n-1) in the original with its draft.

Again, replace T(n-2):

And once more, replace T(n-3):

Step 3 · Making the general equation: the number inside T and the number of 2's change together. After k substitutions: T(n) = T(n-k) + 2k.

We know T(0), so make the inside zero: n - k = 0, that is k = n.

T(n) = T(0) + 2n = 0 + 2n = 2n. So 2n ∈ O(n).

The rest

Next: the same recurrence solved by forward chaining and by the characteristic polynomial, then Big Ω and Big Θ, with a free account.