smart recursion aka dynamic programming
play

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


  1. Smart Recursion aka Dynamic Programming Suresh Velagapudi 31 Jan 2015 licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

  2. We aim to . . . ● Write human comprehensible code ● Write machine executable code ● Write such code on a white board ● Do so within the specified time ● Convey the essence of code verbally

  3. Review of Recursion Factorial Fibonacci Recursive definitions give executable code. Recursion is natural in the presence of recursive data structures like trees

  4. Slogan of the day Recursion + Memoization = Dynamic Programming

  5. Question of the day Is Recursion always inefficient?

  6. Agenda ● Nth Fibonacci Number ● Investment Planning ● N queens on n x n chessboard

  7. Nth Fibonacci Number - Exponential public int nthFibExp(int n) { if (n == 0) return 0; return (n == 1) ? 1 : (nthFibExp(n - 1) + nthFibExp(n - 2)); }

  8. Nth Fib - Linear (Memoization) 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]); }

  9. Nth Fib - Linear (Accumulators) 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 ); }

  10. Investment Planning Problem 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.

  11. Investment Planning Problem(exp) // 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)); }

  12. Investment Planning Problem(dp) 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 }

  13. Investment Planning Problem(dp) ● Similar to Knapsack problem ● Matrix could be sparsely populated or densely populated. That aspect of optimization is a separate topic in itself.

  14. The N Queens Problem Given an 8 x 8 chessboard. How do you find all possible placements of 8 queens so none can kill the other?

  15. The 4 Queens Problem Source: http://www.fas.harvard.edu/~cscie119/lectures/recursion.pdf

  16. Template for Backtracking (pseud) 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.

  17. Interview Advice ● Think Fast. ● Talk Slow. ● Code Clean. ● Stick to just one language. Python, C, C++, Java ● Answer to the point. ● What is good for the compiler need not be good for the interviewer ● OSHI Other Side Human Intelligence ● ESHI Explaining Side Human Intelligence ● Never insult human intelligence!

  18. Further Involvement 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/

  19. Good Luck! Thank you for your attention!

  20. Appendix A 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]

  21. max sum - next steps ● Come up with a dynamic programming solution that takes quadratic time to calculate sums for all values of i and j indices. ● What are the edge cases for these problems? What alternate algorithms can you use to handle the edge cases. ● What is the iterative solution that uses accumulators? ● See next slide for a recursive solution. Work through an example to understand it further. ● Check out Jon Bentley’s Programming Pearls for more on the above.

  22. max sum - recursive solution 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

  23. Appendix B Find the longest common subsequence between two given strings. Example: AABABAA, ABAXXCA. Answer: ABA Check out the following slides and work through examples.

  24. Longest Common Subsequence l cs(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)); }

  25. Longest Common Su b sequence(arr) l cs(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]; }

  26. Appendix C Fun Problem 2: ● Given an 8 x 8 chessboard. How do you find all possible placements of 8 queens so none can kill the other? ● Related Class Notes: http://www.fas.harvard. edu/~cscie119/lectures/recursion.pdf

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend