Smart Recursion aka Dynamic Programming
Suresh Velagapudi 31 Jan 2015
licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Smart Recursion aka Dynamic Programming Suresh Velagapudi 31 Jan - - PowerPoint PPT Presentation
Smart Recursion aka Dynamic Programming Suresh Velagapudi 31 Jan 2015 licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. We aim to . . . Write human comprehensible code Write machine
licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
public int nthFibExp(int n) { if (n == 0) return 0; return (n == 1) ? 1 : (nthFibExp(n - 1) + nthFibExp(n - 2)); }
public int nthFibMem(int n, int[] answers){ if (n == 0) return 0; if(answers[n] != 0) return answers[n]; answers[n] = (n == 1) ? 1 : (nthFibMem(n - 1,answers) + nthFibMem(n - 2,answers)); return answers[n]; } public int calcNthFib(int n) { return nthFibMem(n, new int[n + 1]); }
int nthFibAcc(int n, int m, int previous, int current){ return (n == m) ? current : nthFibAcc(n, m + 1, current, previous+current); } int calcNthFib(int n) { return nthFibAcc(n, 1, 0, 1 ); }
Ventura Kapitalista visits hackerdojo to fund universe changing projects. Many hackers including a White Haired Pony Tail propose projects. VK has a limited budget B. Every project i has a funding need fund(i) and a universe changing value ucv(i). VK would like to maximize the sum of values of the projects funded.
// either VK uses the last project or not. // budget = Budget left, n = # projects still to choose from int value(int n, int budget) { if (n == 0) return 0; return (fund(n) > budget) ? value(n-1, budget) // can’t use nth : Math.max(value(n-1, budget - fund(n)) + ucv(n), value(n-1, budget)); }
value(int n, int [] [] arr) { if (n == 0) return 0; if (arr[n][b] != 0) return arr[n][b]; arr[n][b] = fund(n) > b ? value(n-1, b): : Math.max(value(n-1, b - fund(n)) + ucv(n), value(n-1, b)); return arr[n][b]; // Max returned value for }
aspect of optimization is a separate topic in itself.
Given an 8 x 8 chessboard. How do you find all possible placements
Source: http://www.fas.harvard.edu/~cscie119/lectures/recursion.pdf
void findSolutions(n, other params) { if (found a solution) { solutionsFound++; displaySolution(); if (solutionsFound >= solutionTarget) System.exit(0); return; } // end of if for (val = first to last) { if (isValid(val, n)) { applyValue(val, n); findSolutions(n + 1, other params); removeValue(val, n);}} } // Thanks to David G. Sullivan, Ph.D.
interviewer
http://web.engr.illinois.edu/~jeffe/teaching/algorithms/ http://introcs.cs.princeton.edu/java/23recursion/ http://algs4.cs.princeton.edu/40graphs/ http://thinkdynamik.blogspot.com/ http://www.meetup.com/Mountain-View-Algorithm-Design/
Thank you for your attention!
A = [ 1, 3, 4, -9, 11, 12, -1, 3] Given the real vector x [ n ], compute the maximum sum found in any contiguous subvector. Answer: 25 from [11, 12, -1, 3]
sums for all values of i and j indices.
use to handle the edge cases.
further.
int maxSum(int current, int maxEndingHere, int maxSoFar, List<Integer> li){ return (li.length == current) ? maxSoFar : maxSum(current + 1, Math.max(maxEndingHere + li(m),0), Math.max(maxEndingHere, maxSoFar), li) } int maxSum(List<Integer> li) { return maxSum(0, 0, 0, li); } // The above assumes the list has positives and negatives
Find the longest common subsequence between two given strings. Example: AABABAA, ABAXXCA. Answer: ABA Check out the following slides and work through examples.
lcs(String S, String T) { int n = S.length, m = T.length; if (n==0 || m==0) return 0; return (S.charAt(n -1) == T.charAt(m -1)) ? 1 + lcs(S.substring(0, n - 1), T.substring(0, n - 1)) : Math.max(lcs(S.substring(0, n - 1),T), lcs(S,T.substring(0, n - 1)); }
lcs(String S, int n, String T, int m, int [] [] arr){ if (n==0 || m==0) return 0; if (arr[n][m] != 0) return arr[n][m]; arr[n][m] = (S.charAt(n -1) == T.charAt(m -1)) ? 1 + lcs(S.substring(0, n - 1), T.substring(0, n - 1)) : Math.max(lcs(S.substring(0, n - 1),T), lcs(S,T.substring(0, n - 1) ); return arr[n][m]; }
Fun Problem 2:
placements of 8 queens so none can kill the other?
edu/~cscie119/lectures/recursion.pdf