recursion ii
play

Recursion II Fundamentals of Computer Science Outline Recursion A - PowerPoint PPT Presentation

Recursion II Fundamentals of Computer Science Outline Recursion A method calling itself A new way of thinking about a problem A powerful programming paradigm Examples: Last time: Factorial, binary search, H-tree,


  1. Recursion II Fundamentals of Computer Science

  2. Outline  Recursion  A method calling itself  A new way of thinking about a problem  A powerful programming paradigm  Examples:  Last time:  Factorial, binary search, H-tree, Fibonacci  Today:  Brownian Motion  Sorting

  3. Recursion Walkthrough def mystery(n): Call Stack n print(n) if n <= 0: return #Pop mystery(n - 1); #Push # Return here mystery(n - 2); #Push # Return here #Pop main if __name__ == "__main__": mystery(3) #Push # Return here #Pop

  4. Recursion Walkthrough def mystery(n): Call Stack n print(n) if n <= 0: return #Pop mystery(n - 1); #Push # Return here mystery(n - 2); #Push # Return here 3 #Pop mystery(3) main if __name__ == "__main__": mystery(3) #Push # Return here #Pop

  5. Recursion Walkthrough def mystery(n): Call Stack n print(n) if n <= 0: return #Pop mystery(n - 1); #Push # Return here 2 mystery(n - 2); #Push # Return here mystery(3-1) 3 #Pop mystery(3) main if __name__ == "__main__": mystery(3) #Push # Return here 3 #Pop

  6. Recursion Walkthrough def mystery(n): Call Stack n print(n) if n <= 0: return #Pop mystery(n - 1); #Push 1 # Return here mystery(2-1) 2 mystery(n - 2); #Push # Return here mystery(3-1) 3 #Pop mystery(3) main if __name__ == "__main__": mystery(3) #Push # Return here 3 #Pop 2

  7. Recursion Walkthrough def mystery(n): Call Stack n print(n) if n <= 0: return #Pop 0 mystery(n - 1); #Push mystery(1-1) 1 # Return here mystery(2-1) 2 mystery(n - 2); #Push # Return here mystery(3-1) 3 #Pop mystery(3) main if __name__ == "__main__": mystery(3) #Push # Return here 3 #Pop 2 1

  8. Recursion Walkthrough def mystery(n): Call Stack n print(n) if n <= 0: return #Pop 0 mystery(n - 1); #Push mystery(1-1) 1 # Return here mystery(2-1) 2 mystery(n - 2); #Push # Return here mystery(3-1) 3 #Pop mystery(3) main if __name__ == "__main__": mystery(3) #Push # Return here 3 #Pop 2 1 0

  9. Recursion Walkthrough def mystery(n): Call Stack n print(n) if n <= 0: return #Pop mystery(n - 1); #Push 1 # Return here mystery(2-1) 2 mystery(n - 2); #Push # Return here mystery(3-1) 3 #Pop mystery(3) main if __name__ == "__main__": mystery(3) #Push # Return here 3 #Pop 2 1 0

  10. Recursion Walkthrough def mystery(n): Call Stack n print(n) if n <= 0: return #Pop -1 mystery(n - 1); #Push mystery(1-2) 1 # Return here mystery(2-1) 2 mystery(n - 2); #Push # Return here mystery(3-1) 3 #Pop mystery(3) main if __name__ == "__main__": mystery(3) #Push # Return here 3 #Pop 2 1 0

  11. Recursion Walkthrough def mystery(n): Call Stack n print(n) if n <= 0: return #Pop mystery(n - 1); #Push 1 # Return here mystery(2-1) 2 mystery(n - 2); #Push # Return here mystery(3-1) 3 #Pop mystery(3) main if __name__ == "__main__": mystery(3) #Push # Return here 3 #Pop 2 1 0 -1

  12. Recursion Walkthrough def mystery(n): Call Stack n print(n) if n <= 0: return #Pop mystery(n - 1); #Push # Return here 2 mystery(n - 2); #Push # Return here mystery(3-1) 3 #Pop mystery(3) main if __name__ == "__main__": mystery(3) #Push # Return here 3 #Pop 2 1 0 -1

  13. Recursion Walkthrough def mystery(n): Call Stack n print(n) if n <= 0: return #Pop mystery(n - 1); #Push 0 # Return here mystery(2-2) 2 mystery(n - 2); #Push # Return here mystery(3-1) 3 #Pop mystery(3) main if __name__ == "__main__": mystery(3) #Push # Return here 3 #Pop 2 1 0 -1

  14. Recursion Walkthrough def mystery(n): Call Stack n print(n) if n <= 0: return #Pop mystery(n - 1); #Push # Return here 2 mystery(n - 2); #Push # Return here mystery(3-1) 3 #Pop mystery(3) main if __name__ == "__main__": mystery(3) #Push # Return here 3 #Pop 2 1 0 -1 0

  15. Recursion Walkthrough def mystery(n): Call Stack n print(n) if n <= 0: return #Pop mystery(n - 1); #Push # Return here mystery(n - 2); #Push # Return here 3 #Pop mystery(3) main if __name__ == "__main__": mystery(3) #Push # Return here 3 #Pop 2 1 0 -1 0

  16. Recursion Walkthrough def mystery(n): Call Stack n print(n) if n <= 0: return #Pop mystery(n - 1); #Push # Return here 1 mystery(n - 2); #Push # Return here mystery(3-2) 3 #Pop mystery(3) main if __name__ == "__main__": mystery(3) #Push # Return here 3 #Pop 2 1 0 -1 0

  17. Recursion Walkthrough def mystery(n): Call Stack n print(n) if n <= 0: return #Pop mystery(n - 1); #Push 0 # Return here mystery(1-1) 1 mystery(n - 2); #Push # Return here mystery(3-2) 3 #Pop mystery(3) main if __name__ == "__main__": mystery(3) #Push # Return here 3 #Pop 2 1 0 -1 0 1

  18. Recursion Walkthrough def mystery(n): Call Stack n print(n) if n <= 0: return #Pop mystery(n - 1); #Push # Return here 1 mystery(n - 2); #Push # Return here mystery(3-2) 3 #Pop mystery(3) main if __name__ == "__main__": mystery(3) #Push # Return here 3 #Pop 2 1 0 -1 0 1 0

  19. Recursion Walkthrough def mystery(n): Call Stack n print(n) if n <= 0: return #Pop mystery(n - 1); #Push -1 # Return here mystery(1-2) 1 mystery(n - 2); #Push # Return here mystery(3-2) 3 #Pop mystery(3) main if __name__ == "__main__": mystery(3) #Push # Return here 3 #Pop 2 1 0 -1 0 1 0

  20. Recursion Walkthrough def mystery(n): Call Stack n print(n) if n <= 0: return #Pop mystery(n - 1); #Push # Return here 1 mystery(n - 2); #Push # Return here mystery(3-2) 3 #Pop mystery(3) main if __name__ == "__main__": mystery(3) #Push # Return here 3 #Pop 2 1 0 -1 0 1 0

  21. Recursion Walkthrough def mystery(n): Call Stack n print(n) if n <= 0: return #Pop mystery(n - 1); #Push # Return here mystery(n - 2); #Push # Return here 3 #Pop mystery(3) main if __name__ == "__main__": mystery(3) #Push # Return here 3 #Pop 2 1 0 -1 0 1 0

  22. Recursion Walkthrough def mystery(n): Call Stack n print(n) if n <= 0: return #Pop mystery(n - 1); #Push # Return here mystery(n - 2); #Push # Return here #Pop if __name__ == "__main__": mystery(3) #Push # Return here 3 #Pop 2 1 0 -1 0 1 0

  23. 23 Brownian Motion  Models many natural and artificial phenomenon  Motion of pollen grains in water  Price of stocks  Rugged shapes of mountains and clouds

  24. 24 Simulating Brownian Motion  Midpoint displacement method:  Track interval (x 0 , y 0 ) to (x 1 , y 1 )  Choose d displacement randomly from Gaussian  Divide in half, x m = (x 0 +x 1 )/2 and y m = (y 0 +y 1 )/2 + d  Recur on the left and right intervals

  25. 25 Recursive Midpoint Displacement Algorithm def curve(x0, y0, x1, y1, var): #Base case: stop if interval is sufficiently small if x1 - x0 < .005: base case StdDraw.line(x0, y0, x1, y1) StdDraw.show(10) return xm = (x0 + x1) / 2.0 ym = (y0 + y1) / 2.0 # Randomly displace the y coordinate of the midpoint ym = ym + random.gauss(0, math.sqrt(var)) curve(x0, y0, xm, ym, var / 2.0) reduction step curve(xm, ym, x1, y1, var / 2.0)

  26. 26 Plasma Cloud  Same idea, but in 2D  Each corner of square has some color value  Divide into four sub-squares  New corners: avg of original corners, or all 4 + random  Recur on four sub-squares

  27. 27

  28. Brownian Landscape 28

  29. 29 Divide and Conquer  Divide and conquer paradigm  Break big problem into small sub-problems  Solve sub-problems recursively  Combine results “ Divide et impera. Vendi, vidi, vici. ” -Julius Caesar  Used to solve many important problems  Sorting things, mergesort: O(N log N)  Parsing programming languages  Discrete FFT, signal processing  Multiplying large numbers  Traversing multiply linked structures (stay tuned)

  30. 30 Divide and Conquer: Sorting  Goal: Sort by number, ignore suit, aces high Approach 1) Split in half (or as close as possible) 2) Give each half to somebody to sort 3) Take two halves and merge together Unsorted pile #1 Unsorted pile #2

  31. 31 Approach 1) Split in half (or as close as possible) 2) Give each half to somebody to sort 3) Take two halves and merge together Sorted pile #1 Sorted pile #2 Merging Take card from whichever pile has lowest card

  32. 32 Approach 1) Split in half (or as close as possible) 2) Give each half to somebody to sort 3) Take two halves and merge together Sorted pile #1 Sorted pile #2

  33. 33 Approach 1) Split in half (or as close as possible) 2) Give each half to somebody to sort 3) Take two halves and merge together Sorted pile #1 Sorted pile #2

  34. 34 Approach 1) Split in half (or as close as possible) 2) Give each half to somebody to sort 3) Take two halves and merge together Sorted pile #1 Sorted pile #2

  35. 35 Approach 1) Split in half (or as close as possible) 2) Give each half to somebody to sort 3) Take two halves and merge together Sorted pile #1 Sorted pile #2

Recommend


More recommend