Recursive Algorithms
Recursive Algorithms
Algorithms
NAVNEET GOYA L
Background
1. Recursion
2. Defining Sequences, Functions, and Sets using recursion
Recursive Algorithms
1. Fibonacci Sequence
2. Factorial
3. Linear Search
4. Binary Search
5. Merge-sort (& Merging of 2 sorted lists)
6. Towers of Hanoi
Recursive Algorithms
Today we will try to identify recursion in Towers of Hanoi problem:
o Given 3 pegs and 64 different sized disks sorted on peg 1 such that
the smallest (largest) disk is on top (bottom)
Q: How fast can we move the disks from peg 1 to peg 2 such that
(1) We can move only one disk at a time.
(2) No larger disk can be on top of a smaller one.
o Let Hn be the minimum # of moves required to move n disk from
peg i to peg j, i != j
Recursive Algorithms
Please verify that:
H1=1
H2=3
H3=7
H4= ???
…
Hn= ???
Recursive Algorithms
Observation:
Observe that, in order to move n disks from peg 1 to peg 2, we must
somehow move the largest disk from peg 1 to peg 2 (1 move) after
first moving the smaller n-1 disks from peg 1 to peg 3 (Hn-1 moves).
Once the largest disk is moved to peg 2, we can then move the n-1
disks from peg 3 to peg 2 (Hn-1 moves)
Please verify:
# lines: 1 2 3 4 5 6 7 8 …
# regions: 2 4 7 11 16 22 29 37 …
Recursive Algorithms
Plane Division Problem:
Q: How many regions can a plane be divided by n straight lines such
that:
(1) Every pair of lines must intersect.
(2) No more than 2 lines can intersect at a common point.
Recursive Algorithms
Observations:
1. The 2nd line interests the first line to yield 2 more regions.
2. The 3rd line interests the first 2 lines to yield 3 more regions.
3. The 4th line interests the first 3 lines to yield 4 more regions.
Q: Do you see the pattern?
Recursive Algorithms
In general, the nth line interests the first n-1 lines to yield n more
regions!
Let Rn be the number of regions divided by n lines.
Recursive Algorithms
Recursive Algorithms
Observations:
1. The 2nd line interests the first line to yield 2 more regions.
2. The 3rd line interests the first 2 lines to yield 3 more regions.
3. The 4th line interests the first 3 lines to yield 4 more regions.
Q: Do you see the pattern?
Recursive Algorithms
The Rabbit Problem:
Given a pair of new-born rabbits, we assume that, after two months,
this pair of rabbits will give birth to another pair of rabbits, and this
pair of new rabbits will then go through the same cycle again.
For simplicity, we assume that the rabbits will never die and this
process will go on indefinitely.
Q: How many pairs of rabbits do we have after n months?
Recursive Algorithms
Let an be the number of rabbits we have after n months.
Recursive Algorithms
Let an be the number of rabbits we have after n months
Example:
Month: 0 1 2 3 4 5 6 7 …
Pair Rabbits: 0 1 1 2 3 5 8 13 …
DO it Yourself!!
Recursive Algorithms: Problems
Let an be the number of ternary strings of length n that do not contain two consecutive O's or two
consecutive l's.
start with a 2 and follow with a string of length n - 1 not containing two consecutive O's or two
consecutive l's,
or
start with 02 or 12 and follow with a string of length n - 2 not containing two consecutive O's or two
consecutive l's,
or
start with 012 or 102 and follow with a string of length n - 3 not containing two consecutive O's or
two consecutive l's,
or
start with 0102 or 1012 and follow with a string of length n - 4 not containing two consecutive O's or
two consecutive l's, and so on…
Recursive Algorithms: Problems
Once we encounter a 2, we can, in effect, start fresh, but the first 2 may
not appear for a long time.
Before the first 2 there are always two possibilities-the sequence must
alternate between 0's and 1's, starting with either a 0 or a 1.
Furthermore, there is one more possibility-that the sequence contains no
2's at all, and there are two cases
in which this can happen: 0101 ... and 1010 ....
Putting this all together we can write down the recurrence relation, valid
for all n>=2:
an = an-1 + 2an-2 + 2an-3 + 2an-4 + ... + 2ao + 2
Recursive Algorithms: Problems
It turns out that the sequence also satisfies the recurrence relation a,, =
2an-1 + an-2, which can be derived algebraically from the recurrence
relation we just gave by subtracting the recurrence for an-1from
the recurrence for an.
Can you find a direct argument for it?
Recurrence Relations
Many recursive algorithms take a problem with a given input and divide
it into one or more smaller problems.
Reduction is successively applied until the solutions of the smaller
problems can be found quickly.
Recurrence Relations
BINARY SEARCH
We perform a binary search by reducing the search for an element in a
list to the search for this element in a list half as long. We successively
apply this reduction until one element is left.
MERGE SORT
When we sort a list of integers using the merge sort, we split the list
into two halves of equal size and sort each half separately. We then
merge the two sorted halves.
Recurrence Relations
These procedures follow an important algorithmic paradigm known as
divide-and-conquer, and are called divide-and-conquer algorithms,
because they divide a problem into one or more instances of the same
problem of smaller size and they conquer the problem by using the
solutions of the smaller problems to find a solution of the original
problem, perhaps with some additional work.