Problem Solving with Recursion • Recursion is a powerful tool for solving repetitive problems • Recursion is never required to solve a problem • Any problem that can be solved recursively can be solved with a loop • Recursive algorithms usually less efficient than iterative ones • Due to overhead of each function call
Problem Solving with Recursion (cont’d.) • Some repetitive problems are more easily solved with recursion • General outline of recursive function: • If the problem can be solved now without recursion, solve and return • Known as the base case • Otherwise, reduce problem to smaller problem of the same structure and call the function again to solve the smaller problem • Known as the recursive case
Using Recursion to Calculate the Factorial of a Number • In mathematics, the n! notation represents the factorial of a number n • For n = 0, n! = 1 • For n > 0, n! = 1 x 2 x 3 x … x n • The above definition lends itself to recursive programming • n = 0 is the base case • n > 0 is the recursive case • factorial(n) = n x factorial(n-1)
Direct and Indirect Recursion • Direct recursion: when a function directly calls itself • All the examples shown so far were of direct recursion • Indirect recursion: when function A calls function B, which in turn calls function A
Examples of Recursive Algorithms • Summing a range of list elements with recursion • Function receives a list containing range of elements to be summed, index of starting item in the range, and index of ending item in the range • Base case: • if start index > end index return 0 • Recursive case: • return current_number + sum(list, start+1, end)
The Fibonacci Series • Fibonacci series: has two base cases • if n = 0 then Fib(n) = 0 • if n = 1 then Fib(n) = 1 • if n > 1 then Fib(n) = Fib(n-1) + Fib(n-2)
Finding the Greatest Common Divisor • Calculation of the greatest common divisor (GCD) of two positive integers • If x can be evenly divided by y, then • gcd(x,y) = y • Otherwise, gcd(x,y) = gcd(y, remainder of x/y) • Corresponding function code:
The Towers of Hanoi • Mathematical game commonly used to illustrate the power of recursion • Uses three pegs and a set of discs in decreasing sizes • Goal of the game: move the discs from leftmost peg to rightmost peg • Only one disc can be moved at a time • A disc cannot be placed on top of a smaller disc • All discs must be on a peg except while being moved
Recursion versus Looping • Reasons not to use recursion: • Less efficient: entails function calling overhead that is not necessary with a loop • Usually a solution using a loop is more evident than a recursive solution • Some problems are more easily solved with recursion than with a loop • Example: Fibonacci, where the mathematical definition lends itself to recursion
Summary • This chapter covered: • Definition of recursion • The importance of the base case • The recursive case as reducing the problem size • Direct and indirect recursion • Examples of recursive algorithms • Recursion versus looping