Video lecture
Watch on YouTube(opens in a new tab)Sub topic 13
Describing Algorithms Recursively
This chapter assumes you can read simple Python, know what the remainder operator % does, and have seen Euclid's algorithm and the idea of a decreasing measure from earlier chapters.
So far, Euclid's algorithm has been written as a loop that updates a and b until b reaches zero. You can also describe the same algorithm recursively. A recursive description defines the answer for a problem in terms of the answer to a smaller instance of the same problem.
The Two Parts of a Recursive Description
Every recursive description has two parts:
- A base case is an input simple enough to answer directly, with no further recursion.
- A recursive case reduces the input to a smaller instance, solves that instance by applying the same description, and uses its answer.
For Euclid's algorithm, the mathematical fact that drives the recursion is that gcd(a, b) = gcd(b, a mod b) when b > 0. When b = 0, the answer is simply a.
def gcd_recursive(a, b):
if b == 0:
return a
return gcd_recursive(b, a % b)
print(gcd_recursive(48, 18)) # 6The pseudocode reads almost the same way:
function GCD(a, b)
// Precondition: a >= 0, b >= 0, not both zero
// Postcondition: returns gcd(a, b)
if b = 0 then
return a
else
return GCD(b, a mod b)
end if
end functionThere is no loop. Selection picks between the base case and the recursive case, and the repetition comes from the function calling itself.
Tracing a Recursive Call
To trace a recursive algorithm, list each call with its arguments, then follow the return values back up.
| Call | a | b | a mod b | Result |
|---|---|---|---|---|
| 1 | 48 | 18 | 12 | result of call 2 |
| 2 | 18 | 12 | 6 | result of call 3 |
| 3 | 12 | 6 | 0 | result of call 4 |
| 4 | 6 | 0 | n/a | 6 (base case) |
The argument pairs (48, 18), (18, 12), (12, 6), (6, 0) are exactly the states the loop version passes through. In this algorithm each call hands back its result unchanged, so a 6 returns from every level.
Why It Terminates
A recursive description terminates if every chain of calls reaches a base case. You argue this the same way you argue a loop terminates: find a decreasing measure. Here the measure is b. It is a non-negative integer, and in each recursive call the new second argument is a % b, which is strictly less than b. A non-negative integer cannot decrease forever, so b eventually becomes 0 and the base case runs.
Recursion or Iteration?
Recursion and iteration can express the same algorithms. A recursive description usually fits best when the problem's definition is itself recursive: gcd, factorials, or structures such as trees that contain smaller trees. An iterative description is often better for implementation in languages like Python. Python does not optimize tail calls, so very deep recursion uses memory for each pending call and can hit the recursion limit. A practical approach is to state the algorithm recursively when you reason about it and implement it as a loop when depth could be large.
Exercise
Trace gcd_recursive(0, 5). How many calls are made and what is returned? Does the precondition allow this input?
Show answer
The precondition allows it, because the inputs are non-negative and not both zero. Call 1 has b = 5, which is not 0, so it calls gcdrecursive(5, 0 % 5) = gcdrecursive(5, 0). Call 2 has b = 0, so it returns 5. That makes two calls, and the result is 5, which is gcd(0, 5).
Exercise
Write a recursive Python function total(xs) that returns the sum of a list of numbers. Name its base case and its decreasing measure.
Show answer
def total(xs): return 0 if len(xs) == 0 else xs[0] + total(xs[1:]). The base case is the empty list, which returns 0. The decreasing measure is len(xs), a non-negative integer that drops by 1 on each call, so the recursion terminates.