Search
Browse
Create
Log in
Sign up
Log in
Sign up
Upgrade to remove ads
Only $2.99/month
Recursion and Dynamic Programming
STUDY
Flashcards
Learn
Write
Spell
Test
PLAY
Match
Gravity
Terms in this set (12)
Fibonacci (recursive bottom up)
n = n-1 + n-2 + ... + 1
int fibonacci(int i){
if(i==0) return 0;
if(i==1) return 1;
return fibonacci(i-1) + fibonacci(i-2);
}
*
O(2^n)
*
Memoization
is top down dynamic programming where we cache some of the recursive calls results to improve performance or solve a separate problem.
Fibonacci (recrusive w/ memoization)
int fibonacci(int n){
return fibonacci(int i, new int[n-1]);
}
int fibonacci(int n, int[] memo){
if(n == 0 || n == 1) return n;
if(memo[n] == 0 ){
memo[n] = fibonacci(n-1, memo) + fibonacci(n-2,memo);
}
return memo[n];
}
*
O(n)
*
Fibonacci (bottom up dynamic programming)
int fibonacci(int n){
if(n == 0) return 0;
int a = 0;
int b = 1;
for(int i=2; i < n; i++){
int c = a + b; // fib(n-1) + fib(n-2)
a=b;
b=c;
}
return a + b;
}
Finding all subsets of a set - Complexity
Each element has a choice of being in the set or not, so for each element of the set there are 2 options.
So for a given set with n elements, it would take at least
O(2^n) time or space complexity
PowerSet
All subsets of a set
Finding all subsets of a set - recursive
For a set S = { A1, A2, A3 }, start with base case n=0
* to get the subsets of n, P(n), we first compute the subsets of P(n-1), then clone them and add A<sub>n</sub> to each of the cloned sets.
---
private List<List<String>> genSubsetsRecursive(List<String> set, int index) {
List<List<String>> allSubsets;
// base case: n = 0
if(set.size() == index){
allSubsets = new ArrayList<>();
allSubsets.add(new ArrayList<String>());
}else {
// all over cases: n > 0
allSubsets = genSubsetsRecursive(set, index + 1);
// get next element in set
String nextItem = set.get(index);
// build subsets for nextItem
List<List<String>> nextSubsets = new ArrayList<>();
// go through each set for n+1, clone it and add nextItem
for(List<String> subset : allSubsets){
List<String> newSet = new ArrayList<>();
newSet.addAll(subset);
newSet.add(nextItem);
nextSubsets.add(newSet);
}
// add all new subsets for next item to allSubsets
allSubsets.addAll(nextSubsets);
}
return allSubsets;
}
Recursive Multiply
private int multiply(int n, int m) {
int a = (n < m) ? n : m;
int b = (n < m) ? m : n;
int prod = 0;
if(a == 0) return 0;
if(a == 1)
return b;
prod = multiply(a-1, b) + b;
return prod;
}
Recursive Multiply with Bit Shifting
* run in O( log n) - n is min of n and m.
private int minProduct(int n, int m) {
int a = (n < m) ? n : m;
int b = (n < m) ? m : n;
return minProductHelper(a, b);
}
private int minProductHelper(int a, int b) {
if(a == 0) return 0;
if(a == 1) return b;
int a2 = a >> 1; // a divided by 2
int halfProd = minProductHelper(a2, b);
if(a % 2 == 0) // even
return halfProd + halfProd;
else
return halfProd + halfProd + b;
}
Recursive Multiply with memoization
private int multiplyWithMemoization(int n, int m) {
int a = (n < m) ? n : m;
int b = (n < m) ? m : n;
int [] cache = new int[a + 1];
return multiplyWithMemoHelper(a, b, cache);
}
private int multiplyWithMemoHelper(int a, int b, int[] cache) {
if(a == 0) return 0;
if(a == 1) return b;
if(cache[a] > 0) return cache[a];
// compute half. If uneven, compute other half. If even, double it.
int a2 = a >> 1;
int half = multiplyWithMemoHelper(a2, b, cache);
int otherHalf = half;
if(a % 2 == 1) { // odd
otherHalf = multiplyWithMemoHelper(a - a2, b, cache);
}
cache[a] = half + otherHalf;
return cache[a];
}
Generate All Permutations of a String (no dups)
// O(n!)
private List<String> genPerms(String remainder) {
int len = remainder.length();
List<String> result = new ArrayList<>();
// base case
if(len == 0){
result.add("");
return result;
}
for(int i=0; i < len; i++){
// remove ith char and find permutations of remaining chars
String before = remainder.substring(0, i);
String after = remainder.substring(i + 1, len);
List<String> partials = genPerms(before + after);
// for each partial try charAt(i) + partial
for(String sp : partials){
result.add(remainder.charAt(i) + sp);
}
}
return result;
}
coins problem: find number of ways to make change with different coins
private int makeChange(int amt, int[] coins, int index) {
if(index >= coins.length - 1) return 1;
int count = 0;
int coin = coins[index];
for(int i=0; i * coin < amt; i++){
int rem = amt - (i * coin);
count += makeChange(rem, coins, index+1);
}
return count;
}
THIS SET IS OFTEN IN FOLDERS WITH...
Bit Manipulation
12 terms
Math and Logic Puzzles
10 terms
Arrays and Strings
4 terms
Java
3 terms
YOU MIGHT ALSO LIKE...
Chapter 13: Recursions
101 terms
Recursion Study Guide Questions
20 terms
Java Code (CodingBat) Array-1
27 terms
OTHER SETS BY THIS CREATOR
Amazon Leadership Principals
14 terms
Layer 7
24 terms
Book Outline
2 terms
Java - Collections
10 terms