 Download Download Presentation Recursion

# Recursion

Download Presentation ## Recursion

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Recursion

2. Review • Recursive solutions, by definition, are built off solutions to sub-problems. • Many times, this will mean simply to compute f(n) by adding something, removing something, or otherwise changing the solution for f(n-1). • In other cases, you might have to do something more complicated.

3. Recommended Approach • Think about what the sub-problem is. • How many sub-problems does f(n) depend on? • That is, in a recursive binary tree problem, each part will likely depend on two problems. • In a linked list problem, it’ll probably be just one. • Solve for a “base case.” • That is, if you need to compute f(n), first compute it for f(0) or f(1).This is usually just a hard-coded value. • Solve for f(2). • Understand how to solve for f(3) using f(2) (or previous solutions). • That is, understand the exact process of translating the solutions for sub-problems into the real solution. • Generalize for f(n).

4. Things to Watch Out For • All problems that can be solved recursively can also be solved iteratively (though the code may be much more complicated). • Before diving into a recursive code, ask yourself how hard it would be to implement this algorithm iteratively. • Discuss the trade-offs with your interviewer. • Recursive algorithms can be very space inefficient. • Each recursive call adds a new layer to the stack, which means that if your algorithm has O(n) recursive calls then it uses O(n) memory. Ouch! This is one reason why an iterative algorithm may be better.

5. Problem • Write a method to generate the nth Fibonacci number.

6. Hints • There are three potential approaches: • (1) recursive approach • (2) iterative approach • (3) using matrix math. • We have described the recursive and iterative approach below, as you would not be expected to be able to derive the matrix-based approach in an interview. • For the interested math-geeks, • you may read about the (most efficient) matrix-based algorithm at http://en.wikipedia.org/wiki/Fibonacci_number#Matrix_form.

7. Your task • Write a method to compute all permutations of a string

8. Hints • Let’s assume a given string S represented by the letters A1, A2, A3, ..., An • To permute set S, we can select the first character, A1, permute the remainder of the string to get a new list. Then, with that new list, we can “push” A1 into each possible position. • For example, if our string is “abc”, we would do the following: • Let first = “a” and let remainder = “bc” • Let list = permute(bc) = {“bc”, “cd”} • Push “a” into each location of “bc” (--> “abc”, “bac”, “bca”) and “cb” (--> “acb”, “cab”, “cba”) • Return our new list

9. Implement getPerms public static ArrayList<String> getPerms(String s) { ArrayList<String> permutations = new ArrayList<String>(); return permutations; }