General Algorithm (problem solving tool) of Divide and Conquer
Postpone work
(if defined recursively (inductively), work is easy)
For a web-oriented example, consider designing a "web-crawler" that will search every hyperlink that is accessible from a specific page.
Pseudo-code
When ... why recursion?
If problem itself is recursively defined
To describe a backtracking procedure
Provability of correctness (induction)
Ease of programming
Why not?
One can always take a recursive program and do it non-recursively.
Recursion Example
Problem: n factorial n!
Iterative procedure: explicit repetition of some process until certain
condition is met
recursive definition
(circular definition ...uses itself)
Recursive procedure
For any recursive problem one needs to consider two features:
I.e., find a method to solve the "complex" case in terms of a "simpler" case
(the conquer is easier if divide)
An interesting problem for recursive algorithms is the computation of
Fibonacci numbers. It's one you probably wouldn't need to actually
solve, but simple enough that it's easy to understand and maybe
surprising that there are many different solutions.
The Fibonacci story: Leonardo of Pisa was also known as Fibonacci. He invented the Fibonacci numbers while studying the population dynamics of rabbits.
More "scientifically", he was interested in many things, including a subject we
now know as population dynamics: For instance, how quickly
would a population of rabbits expand under appropriate
conditions? As is typical in mathematics (and analysis of algorithms is a
form of mathematics), we make the problem more abstract to get an
idea of the general features without getting lost in detail: We then express the number of pairs of rabbits as a function of
time (measured as a number of years since the start of the
experiment): Sounds confusing, but all you have to do is take that equation and provide a recursive algorithm. That is easy!
0! = 1
1! = 1 * 0!
2! = 2 * 1!
3! = 3 * 2!
4! = 4 * 3!
...
n! = n * (n-1)! if n>0
(need to identify a "trivial" case)
Exercises: Binary Search, Book page 13, # 5, 6, 7, 8, 12
Exercise 6: The Fibonacci numbers
From David Eppstein's lecture notes at UC Irvine here but don't freak out. We will not get that far into it.
(The last assumption sounds stupid, but makes the problem simpler.
After we have analyzed the simpler version, we could go back and
add an assumption e.g. that rabbits die in ten years, but it
wouldn't change the overall behavior of the problem very much.)
In general F(n) = F(n-1) + F(n-2): all the previous rabbits are
still there (F(n-1)) plus we get one pair of children for every
pair of rabbits we had two years ago (F(n-2)).
Induction
We often validate algorithms through proofs by induction on the input size. When algorithms are recursive, it makes them quite easy to prove with induction.