DSA Recursion
DSA Recursion
Introduction
• Recursion is the process of repeating items in a self similar way.
• For instance, when the surfaces of two mirrors are exactly parallel with
each other the nested images that occur are a form of infinite
recursion.
• The term has a variety of meanings specific to a variety of disciplines
ranging from linguistics to logic. The most common application of
recursion is in mathematics and computer science.
• It means, that variables are local and are valid just inside the single
procedure.
Example
Example
Example
Example
Trace of the Program
Fibonacci Numbers
• In mathematics, things are often defined recursively. For example, the
Fibonacci numbers are often defined recursively.
• The Fibonacci numbers are defined as the sequence beginning with
two 1's, and where each succeeding number in the sequence is the
sum of the two preceding numbers.
Recursive Code
Recursive Code
Iterative Code
Iterative Code
Summary
• Recursive function always has:
• recursive part (contains one or more recursive calls)
• base case (handles a simple input that can be solved without resorting to a
recursive call)
• For Fibonacci numbers, the base case is when n == 0 and n==1, then
the program returns 1 without any further recursive calls.
• Now that we know fib(1), we need to compute fib(0). 0 is less than or equal to 1, so we
need to return 1.
Now we know fib(2 - 1) + fib(2 - 2) = 1 + 1 = 2. We return this.
• Now that we know fib(2) is 2, we need to compute fib(1). 1 is less than or equal to 1, so
I need to return 1.
We now know fib(3 - 1) + fib(3 - 2) = 2 + 1 = 3. We return this.
Tower of Hanoi
• The Towers of Hanoi puzzle begins with three poles and n rings, where all
rings start on the leftmost pole (Pole A).
• The rings each have a different size, and are stacked in order of decreasing
size with the largest ring at the bottom.
• The problem is to move the rings from the leftmost pole to the rightmost
pole (Pole C) in a series of steps.
• At each step the top ring on some pole is moved to another pole.
• There is one limitation on where rings may be moved: a ring can never be
moved on top of a smaller ring.
• “Towers of Hanoi” natural algorithm to solve this problem has multiple
recursive calls. It cannot be rewritten easily using while loops.
Tower of Hanoi Details
• Problem: You are given three posts labelled A, B, and C.
• On Post A there are n rings of different sizes, in the order of the largest ring
on the bottom to the smallest one on top. Posts B and C are empty.
• The object is to move the n rings from Post A to Post B by successively
moving a ring from one post to another post that is empty or has a larger
diameter
• Inductive proof that the puzzle has a solution:
• (basis) One ring can always be moved.
• (inductive step) Assume that we can move n–1 rings from any given post to
any other post. Since any of the rings 1 through n–1 can be placed on top
of ring n, all n rings can be moved as follows:
1. Move n–1 rings from Post A to Post B.
2. Move ring n from Post A to Post C.
3. Move n–1 rings from Post B to Post C.
Tower of Hanoi
Example:
• The solution for 3 disks takes 7 steps:
Recursive algorithm
• The proof by induction is constructive because it tells you how to
solve a problem with n rings in terms of solutions to problems with n–
1 rings; it corresponds directly to a recursive program.
• The procedure TOWER(n,x,y,z) moves n rings from Post x to Post y
(using Post z as a "scratch" post):
Tower of Hanoi
Recursive Code
Advantages & Disadvantages
• Advantages of the recursion
vConvenient way to control sequence of tasks
vSimple code
• Disadvantages of the recursion
vStack overflow may appear
vRecursion can lead to not efficient way of the algorithm