Recursion
Recursion
Recursion
Recursion is the process of defining a problem (or the solution to a problem) in terms of (a
simpler version of) itself.
For example, we can define the operation "find your way home" as:
Here the solution to finding your way home is two steps (three steps). First, we don't go home
if we are already home. Secondly, we do a very simple action that makes our situation simpler
to solve. Finally, we redo the entire algorithm.
The above example is called tail recursion. This is where the very last statement is calling the recursive algorithm.
Tail recursion can directly be translated into loops.
How would you write a recursive "algorithm" for finding Temple Square?
Answer:
Another example of recursion would be finding the maximum value in a list of numbers. The
maximum value in a list is either the first number or the biggest of the remaining numbers.
Here is how we would write the pseudocode of the algorithm:
end
The "work toward base case" is where we make the problem simpler (e.g., divide list into two
parts, each smaller than the original). The recursive call, is where we use the same algorithm
to solve a simpler version of the problem. The base case is the solution to the "simplest"
possible problem (For example, the base case in the problem 'find the largest number in a list'
would be if the list had only one number... and by definition if there is only one number, it is
the largest).
Matlab
if ( nargin() == 2 )
result = a + b;
else if ( nargin() == 3 )
result = add_numbers(a+b, c);
else
error('oops');
end
end
This reduces the number of parameters (nargin) sent in to the function from 3 to 2,
and 2 is the base case!
Every function has its own workspace PER CALL of the function.
Maze Example:
Consider a rectangle grid of rooms, where each room may or may not have doors on the
North, South, East, and West sides.
How do you find your way out of a maze? Here is one possible "algorithm" for finding the
answer:
For every door in the current room, if the door leads to the exit, take that door.
The "trick" here is of course, how do we know if the door leads to a room that leads to the
exit? The answer is we don't but we can let the computer figure it out for us.
What is the recursive part about the above algorithm? Its the "door leads out of the maze".
How do we know if a door leads out of the maze? We know because inside the next room
(going through the door), we ask the same question, how do we get out of the maze?
What happens is the computer "remembers" all the "what ifs". What if I take the first door,
what if I take the second door, what if I take the next door, etc. And for every possible door
you can move through, the computer remembers those what ifs, and for every door after that,
and after that, etc, until the end is found.
Matlab
return true
end
end
end
Answer: (That was a trick question) There is no base case in the code. You need to check at
the start if the room is the exit. If it is, no recursion!
function success = find_way_out( maze, room )
% rest of code
...
end
Answer: There are various techniques. If the room is a structure (or object) you can add the
visited field direction to the room. (e.g., room.visited = true;) If you are not using objects,
you could have a matrix of boolean flags that is the same size/shape as the maze and use these
values.
Answer: The answer to that can be found by thinking about the following question: What
would happen if the maze was a giant grid of identically sized rectangular rooms, each with
doors on every wall? Imagine you go North through the first door, then East through the next
rooms door, then South through that rooms door, and then West through that rooms door.
Where do you end up? Back where you started! Worse, you might continue to make this loop
for ever. How would a intrepid adventurer solve this problem?
One answer to that is by using a piece of chalk and putting a big X on the floor of every room
you enter. Thus when you come back to a room with an X on the floor, you know you needn't
enter. In the case of the program, a boolean flag "seen" or "visited" should be used. Every
room has a flag. Every room starts with the flag being set to false. When you visit a room,
you set the flag to true. Finally, in the "base case" you have a line such as:
% exit chack
if room is visited → return false
% rest of code
...
end
Answer:
2. Factorial
What is the definition of Factorial?
Answer: factorial(X-1)
Answer:
3. Fibonacci
What is the definition of the Fibonacci number at location X in the sequence?
Lets write a recursive function to compute the Nth number in the Fibonacci sequence.
Answer:
4. Sorting
How can you sort a list of numbers using recursion?
Hint 2: If you have two sorted lists, can you put them back together?
Answer
5. Sudoku
How would you use a computer solve a Sudoku? How about trying every possible
combination of numbers? Note: this is a brute force approach. Here is one possible
"algorithm":
Starting from the upper left "bucket" and moving across each row one column at a
time (then down to the start of the next row, and so forth).
Hypothesize a valid number (what the heck, just try all 9 numbers) for the bucket. IF,
you can solve the rest of the puzzle, BASED on the HYPOTHESIZED number, you
have SOLVED the puzzle!
end