0% found this document useful (0 votes)
9 views20 pages

Lab 2 - Recursion

The document discusses recursion, explaining its characteristics and applications through examples like drinking coffee, computing powers, and checking for palindromes. It also covers the Fibonacci series, file directory structures, and the Towers of Hanoi problem, emphasizing the efficiency of recursive methods and their Big O notations. Additionally, it encourages the reader to implement recursive solutions for various problems and analyze their performance.

Uploaded by

mellaelvi23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views20 pages

Lab 2 - Recursion

The document discusses recursion, explaining its characteristics and applications through examples like drinking coffee, computing powers, and checking for palindromes. It also covers the Fibonacci series, file directory structures, and the Towers of Hanoi problem, emphasizing the efficiency of recursive methods and their Big O notations. Additionally, it encourages the reader to implement recursive solutions for various problems and analyze their performance.

Uploaded by

mellaelvi23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

The Second Lab

What is a recursion and how do we use it?


Recursion is everywhere. It is fun to think recursively. Consider drinking coffee. You may describe the procedure
recursively as follows:

Assume cup is an object for a cup of coffee with the instance methods isEmpty() and takeOneSip(). You can break the
problem into two subproblems: one is to drink one sip of coffee, and the other is to drink the rest of the coffee in the
cup. The second problem is the same as the original problem, but smaller in size.
The base case for the problem is when the cup is empty.
What should we keep in mind?
All recursive methods have the following characteristics:
• The method is implemented using an if−else or a switch statement that leads to different cases.
• One or more base cases (the simplest case) are used to stop recursion.
• Every recursive call reduces the original problem, bringing it increasingly closer to a base case until it becomes that
case.
In general, to solve a problem using recursion, you break it into subproblems. Each subprob lem is the same as the
original problem, but smaller in size. You can apply the same approach to each subproblem to solve it recursively.
1. Write a recursive mathematical definition for computing 2^n for a positive integer n.
2. Now it’s your turn. Write a recursive mathematical definition for computing x^n for a
positive integer n and a real number x.
2. SOLUTION - Now it’s your turn. Write a recursive mathematical definition for
computing x^n for a positive integer n and a real number x.
3. Recursive Palindrome. Let’s find out if a word is palindrome or not.
Helper Methods!
Sometimes you can find a solution to the original problem by defining a recursive function to a problem similar to the
original problem. This new method is called a recursive helper method. The original problem can be solved by
invoking the recursive helper method.
The recursive isPalindrome method, as you already know, is not as efficient as we would like, because it creates a new
string for every recursive call. To avoid creating new strings, you can use two parameters, the low and high indices, to
indicate the range of the substring. These two indices must be passed to the recursive method.
Since the original method is isPalindrome(String s), we can create the new method isPalindrome(String s, int low, int
high) to accept additional information on the string.
4. Let’s see RecursivePalindromeHelper
Fibonacci Series
The Fibonacci series was named for Leonardo Fibonacci, a medieval mathematician, who originated it to model the
growth of the rabbit population. It can be applied in numeric optimization and in various other areas.
How do you find fib(index) for a given index? It is easy to find fib(2) because you know fib(0) and fib(1). Assuming
you know fib(index − 2) and fib(index − 1), you can obtain fib(index) immediately.
Thus, the problem of computing fib(index) is reduced to computing fib(index − 2) and fib(index − 1). When doing so,
you apply the idea recursively until index is reduced to 0 or 1.
5. Naive Fibonacci Series. What is the Big O notation of the following code?
Why is it called Naïve Fibonacci? The term “naive” is used to describe a simple and straightforward approach to
solving a problem. In the context of the Fibonacci sequence, the naive approach is to use recursion to calculate the nth
term of the sequence.
This method is simple to understand and implement, but it is not the most efficient way to calculate the Fibonacci
sequence. Can you find the Big O Notation?
Naive Fibonacci Series.

The Big O Notation is O(2^n)


6. Your Turn. Implement a more efficient Fibonacci using Helper Methods. What is the
Big O Notation?
6. Solution - Implement a more efficient Fibonacci using Helper Methods. What is the Big
O Notation?

The Big O Notation now is O(n)


7. File Directory Recursive Disk Space
Modern operating systems define file-system directories (also called “folders”) in a recursive way. Namely, a file
system consists of a top-level directory, and the contents of this directory consists of files and other directories,
which in turn can contain files and other directories, and so on.
The cumulative disk space for an entry can be computed with
a simple recursive algorithm. It is equal to the immediate disk
space used by the entry plus the sum of the cumulative disk
space usage of any entries that are stored directly within the
entry.
For example, the cumulative disk space for cs016 is 249K
because it uses 2K itself, 8K cumulatively in grades, 10K
cumulatively in homeworks, and 229K cumulatively in
programs.
7. Solution of File Directory and Big O Notation
The file system portrayed in the previous figure has n = 19
entries.
To characterize the cumulative time spent for an initial call
to diskUsage, we must analyze the total number of
recursive invocations that are made, as well as the number
of operations that are executed within those invocations.
It may look like the Big O Notation is n^2, but in fact it is
n, because each item is checked only one time and no
more.
8. Create a method to print the digits in an integer reversely. Write a recursive method
that displays an int value reversely on the console.
For example, reverseDisplay(12345) displays 54321. What is the Big O notation for the reverseDisplay method?
9. Towers of Hanoi
The Tower of Hanoi problem is a classic problem that can be solved easily using recursion, but it is difficult to
solve otherwise.
The problem involves moving a specified number of disks of distinct sizes from one tower to another while
observing the following rules:
• There are n disks labeled 1, 2, 3, . . . , n and three towers labeled A, B, and C.
• No disk can be on top of a smaller disk at any time.
• All the disks are initially placed on tower A.
• Only one disk can be moved at a time and it must be the smallest disk on a tower.
The objective of the problem is to move all the disks from A to B with the assistance of C.
9. Towers of Hanoi – Visual Representation (GIF)

Create a recursive method that solves this problem


by asking the user to enter the number of disks.
On the screen you should show the current moves.
For example:
• Move disk 1 from A to C
• Move disk 2 from A to B
• Move disk 1 from C to B
• Move disk 3 from A to C
What is the Big O notation?

You might also like