Smart Recursion aka Dynamic Programming Suresh Velagapudi 31 Jan - - PowerPoint PPT Presentation

smart recursion aka dynamic programming
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Smart Recursion aka Dynamic Programming

Suresh Velagapudi 31 Jan 2015

licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

slide-2
SLIDE 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
slide-3
SLIDE 3

Review of Recursion

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

slide-4
SLIDE 4

Slogan of the day Recursion + Memoization = Dynamic Programming

slide-5
SLIDE 5

Question of the day Is Recursion always inefficient?

slide-6
SLIDE 6

Agenda

  • Nth Fibonacci Number
  • Investment Planning
  • N queens on n x n chessboard
slide-7
SLIDE 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)); }

slide-8
SLIDE 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]); }

slide-9
SLIDE 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 ); }

slide-10
SLIDE 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.

slide-11
SLIDE 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)); }

slide-12
SLIDE 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 }

slide-13
SLIDE 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.

slide-14
SLIDE 14

The N Queens Problem

Given an 8 x 8 chessboard. How do you find all possible placements

  • f 8 queens so none can kill the other?
slide-15
SLIDE 15

The 4 Queens Problem

Source: http://www.fas.harvard.edu/~cscie119/lectures/recursion.pdf

slide-16
SLIDE 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.

slide-17
SLIDE 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!
slide-18
SLIDE 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/

slide-19
SLIDE 19

Good Luck!

Thank you for your attention!

slide-20
SLIDE 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]

slide-21
SLIDE 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.
slide-22
SLIDE 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

slide-23
SLIDE 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.

slide-24
SLIDE 24

Longest Common Subsequence

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)); }

slide-25
SLIDE 25

Longest Common Subsequence(arr)

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]; }

slide-26
SLIDE 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