1 / 43

APS105

APS105. Recursion. Recursion. A function is allowed to call itself! Example: . A function calling itself is called “recursion” Such a function is called a “recursive” function Why would you want to do this?. Recursion: Motivation. Sometimes a prob has self-similar sub-probs

garran
Download Presentation

APS105

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. APS105 Recursion

  2. Recursion • A function is allowed to call itself! • Example: . • A function calling itself is called “recursion” • Such a function is called a “recursive” function • Why would you want to do this?

  3. Recursion: Motivation • Sometimes a prob has self-similar sub-probs • E.g., problems-within-a-problem • “inner” problems have same description as outer • but inner problems are smaller in some way • inner problems themselves have inner problems • Recursion continues until: • some small indivisible problem reached • or some other stopping condition is reached • called the “base case” or “terminating case” • Note: • sometimes recursion can be expressed as a loop • or vice-versa

  4. Recursion: Walking Across the Room • Loop-based solution basic idea: • while not at the wall take another step • Recursive solution basic idea: .

  5. Recursion: Cake Cutting • Cut a jelly-roll into equal parts < 100g each .

  6. Reading/Evaluating Expressions • Recall: BEDMAS: order of evaluation • Brackets • Exponents • Division & Multiplication • Addition & Subtraction • Apply BEDMAS to every expression • and to every sub-expression within the expression

  7. Example: Evaluating Expressions • BEDMAS • Example: .

  8. Other Examples • Finding your way out of a maze • Fractals • Solving a sudoku

  9. Recursive Math Functions

  10. Recursive Math Functions • Example: f(3): .

  11. Implementing Recursion in C .

  12. Factorial Using a Loop .

  13. Factorial Using Recursion .

  14. Factorial Using Recursion (again) .

  15. Printing Patterns with Recursion

  16. Print a Row of n Stars (recursively) .

  17. Print a Row of n Stars (attempt2) .

  18. Print a Triangle of Stars **** *** ** * .

  19. Print an Inverted Triangle of Stars * ** *** **** .

  20. What Will This Print? .

  21. Recursion and Strings

  22. Recursion and Strings • Can think of strings using a recursive definition • Example: one of these recursive definitions: • a string is a character followed by a string • a string is a character preceded by a string • a string is two characters with a string in the middle

  23. Palindromes • Palindrome: • a string that is the same backwards and forwards • Examples: racecar, level, civic, madam, noon • Write a function to determine if a palindrome

  24. Function to test Palindrome .

  25. Palindrome tester with a Helper .

  26. Greatest Common Divisor (GCD)

  27. GCD Algorithm • GCD of two numbers is • if the numbers are the same: • the GCD is either number • if the numbers are different: • the GCD of the smaller of the two and the difference between the two • Example: GCD(20,8) = GCD(8,12) = GCD(8,4) = GCD(4,4) The GCD is 4

  28. Formalized GCD Alg., and Code . .

  29. Determining Powers Efficiently

  30. Determining Powers • Computing powers the easy (but slow) way: X5 = X*X*X*X*X X20 = X*X*X*X*X*X*X*X*X*X.... • The more efficient way: .

  31. Formula for Determining Powers • .

  32. Recursively Determining Power .

  33. Ackermann’s Function and Maze Routing

  34. Ackermann’s Function • .

  35. Maze Routing • Basic idea (see Ch8) .

  36. Towers of Hanoi

  37. Towers of Hanoi • Move all disks to another rod, but: • Only one disk may be moved at a time. • No disk may be placed on a smaller disk. Initial: Goal:

  38. Towers of Hanoi: Outer Problem =

  39. Towers of Hanoi: 1st Inner Problem =

  40. Towers of Hanoi: Ex. base problem =

  41. Towers of Hanoi • Write a program that prints a solution • assume rods 1,2,3 • assume some number of discs given by height

  42. Towers of Hanoi .

  43. Towers Algorithm • Legend: • Monks found 64 disk tower-game • Universe will end when they finish the game • number of moves is 2n-1 • for n=3: 7 moves • for n=20: 1,048,575 moves • for n=64: 1.8*1019 moves • == 585billion years at one move per second • note: 14billion years since big bang • The algorithm is “exponential” • roughly Xn moves where n is size of problem • exponential algorithms are impractical!

More Related