CS 46B - Lecture 8
Pre-class reading
Fibonacci Sequence
- Fibonacci sequence is a sequence of numbers defined by
f1 = 1
f2 = 1
fn = fn-1 +
fn-2
- First ten terms:
1, 1, 2, 3, 5, 8, 13, 21, 34, 55
The Efficiency of Recursion
- Recursive implementation of fib is straightforward
- Watch the output closely as you run the test program
- First few calls to fib are quite fast
- For larger values, the program pauses an amazingly long time between
outputs
- To find out the problem, insert trace messages (see
lab)
Call Tree for Computing fib(6)
The Efficiency of Recursion
The Efficiency of Recursion
- Occasionally, a recursive solution runs much slower than its iterative
counterpart
- In most cases, the recursive solution is only slightly slower
- The iterative isPalindrome performs only slightly better than
recursive solution
- Each recursive method call takes a certain amount of processor time
- Smart compilers can avoid recursive method calls if they follow simple
patterns
- Most Java compilers don't do that
- In many cases, a recursive solution is easier to understand and implement
correctly than an iterative solution
To iterate is human, to recurse divine.
, L. Peter Deutsch
Iterative isPalindrome Method
public boolean isPalindrome()
{
int start = 0;
int end = text.length() - 1;
while (start < end)
{
char first = Character.toLowerCase(text.charAt(start));
char last = Character.toLowerCase(text.charAt(end);
if (Character.isLetter(first) && Character.isLetter(last))
{
// Both are letters.
if (first == last)
{
start++;
end--;
}
else
{
return false;
}
}
if (!Character.isLetter(last)) { end--; }
if (!Character.isLetter(first)) { start++; }
}
return true;
}
Lecture 8 Clicker Question 1
Consider the triangle number computation in the
Triangle.getArea method.
- The recursive method is faster than a loop that computes 1 + 2 + 3 +
. . . + width
- A loop is a little bit faster than the recursive method
- A loop is a lot faster than the recursive method
- Who cares? It is much faster to compute width * (width
+ 1) / 2.
To Generate All Permutations
- Generate all permutations that start with 'e' , then
'a', then 't'
- To generate permutations starting with 'e', we need to find all
permutations of "at"
- This is the same problem with simpler inputs
- Use recursion
To Generate All Permutations
- getPermutations: loop through all positions in the word to be
permuted
- For each position, compute the shorter word obtained by removing i-th
letter:
String shorterWord = word.substring(0, i) + word.substring(i + 1);
- Construct a permutation generator to get permutations of the shorter
word:
PermutationGenerator shorterPermutationGenerator
= new PermutationGenerator(shorterWord);
ArrayList<String> shorterWordPermutations
= shorterPermutationGenerator.getPermutations();
To Generate All Permutations
Lecture 8 Clicker Question 2
What are all permutations of the four-letter word beat?
- beat, eatb, atbe, tbea
- beat, beta, baet, bate, btea, btae
- beat, beta, baet, bate, btea, btae, eatb, etab, aetb, ateb, teab,
taeb
- b followed by the six permutations of eat, e
followed by the six permutations of bat, a followed by
the six permutations of bet, and t followed by the six
permutations of bea
Lecture 8 Clicker Question 3
Homework 3 asks you to provide all subsequences of a word, in the same
order in which they appear. For example, "brat" -> [, a, at, b, ba, bat, br,
bra, brat, brt, bt, r, ra, rat, rt, t]
If the word has n letters, how many subsequences are there?
- n
- n!
- n2
- 2n