CSIS 10B
Assignment 5 Recursion and Algorithm Analysis
Due: next Wed
In Class Demo (0 points):
1) Recursion Demo:
a. recursive summation
b. fibonacci
c. Towers of Hanoi
d. vector insertion
2) Hand Tracing Recursive Functions
a. recursive summation
b. Towers of Hanoi
3) Algorithm Analysis
a. Speed of various Java statements
b. Speed of Towers of Hanoi vs N in Java and C++
c. Big O complexity
Assignment (10 points) Do either Basic or Advanced.
Please place your answers to all of these exercises in the file Lab5.java
1) Trace the recReduce function in Lab5.java with an n of 100
2) Estimate the Big-O of the recReduce function
3) Estimate the Big-O of the matrix multiplication algorithm in exercise 5.5
4) Trace the recursive GCD algorithm with x = 36 and y = 24 (read specification below).
5) Code the recursive algorithm for GCD
6) With x = 10000, find the y that gives the worst case performance of GCD(x,y) (use a loop and
count number of recursive calls). Use this approach to estimate the Big-O of GCD (worst
case order of magnitude).
7) Code, test, then estimate the Big-O of isPalindrome (read specification below)
Specification of the recursive GCD method:
Note than in Java, the remainder of x divided by y is performed with the modulus (%) operator.
In other words x % y. Make sure x and y are declared as int.
Test your function with a few values. Does it matter if we execute gcd(60,24) or gcd(24, 60) ?
If so, can you make the function work the same in either case?
Specification of the isPalindrome method:
A palindrome is a String that reads the same forwards or backwards, such as
“2365445632” and “madamImadam”
In this problem you will write a recursive function isPalindrome that takes a String and
determines whether it is a palindrome. If it is a palindrome it returns true, if it is not, it returns
false. For example isPalindrome(“ratsliveonnoevilstar”) will return true while
isPalindrome(“183454321”) will return false.
A recursive approach to this problem is to examine the beginning and ending characters in the
String you are given. For this you can use the .charAt(int index) method of the String class. For
example if str is a String, str.charAt(0) and str.charAt(str.length()-1) will give you the characters
at the beginning and end of a string. If they are not the same, then we know this isn’t a
palindrome so we can return false.
However, if they are the same, then we can just return isPalindrome of the subString made from
stripping the end points from our original String. Read the subString method in the String
javadoc (you can find by Googling “java String class”) for more information on how to do this.
Another possibility is that we have a String of one letter (always a Palindrome!!) or a String of
zero letters (always a palindrome!). These are the “base cases” of your recurive algorithm.
When you have a solution, try it out on a few Strings to see if it works properly and debug if
necessary.
Advanced: Telephone Mnemonic Generator
If you do this problem, also do a Big-O analysis of your solution, and back up your analysis with
data from a bench test of execution speed as the size of n increases. Make sure you don’t include
the print out in your time measurements.
Write and test a function
Vector<String> listMnemonics(String number)
(From Thinking Recursively by Eric Roberts.) On the standard Touch-Tone™ telephone dial, the
digits are mapped onto the alphabet (minus the letters Q and Z) as shown in the diagram below:
In order to make their phone numbers more memorable, service
providers like to find numbers that spell out some word (called a
mnemonic) appropriate to their business that makes that phone
number easier to remember. For example, the phone number for
a recorded time-of-day message in some localities is 637-8687
(NERVOUS). Write a method listMnemonics that will generate all
possible letter combinations that correspond to a given number,
represented as a string of digits. For example, if you call
Vector<String> result = listMnemonics("723")
your method shall generate and return a vector containing the
following 27 possible letter combinations (strings) that
correspond to that prefix:
PAD PBD PCD RAD RBD RCD SAD SBD SCD
PAE PBE PCE RAE RBE RCE SAE SBE SCE
PAF PBF PCF RAF RBF RCF SAF SBF SCF
Note, the order of the strings in the result vector is unimportant.
You will use the following helper function:
String digitLetters(char ch)
This function returns the letters associated with a given digit.
You will find it useful to create a method that does most of the real work:
void recursiveMnemonics(Vector<String> result, String
String digitsLeft);
mnemonicSoFar,
To kick off the recursion you would call method recursiveMnemonics from listMnemonics:
Vector<string> result;
recursiveMnemonics(result, "", number);
// when starting the mnemonic is the empty string
return result;
© Copyright 2025 Paperzz