cs 115 lecture 20
play

CS 115 Lecture 20 Recursion Neil Moore Department of Computer - PowerPoint PPT Presentation

CS 115 Lecture 20 Recursion Neil Moore Department of Computer Science University of Kentucky Lexington, Kentucky 40506 neil@cs.uky.edu 1 December 2015 Recursion Problemscomputational, mathematical, and otherwisecan be defined and


  1. CS 115 Lecture 20 Recursion Neil Moore Department of Computer Science University of Kentucky Lexington, Kentucky 40506 neil@cs.uky.edu 1 December 2015

  2. Recursion Problems—computational, mathematical, and otherwise—can be defined and solved recursively. That is, in terms of themselves. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 2 / 12

  3. Recursion Problems—computational, mathematical, and otherwise—can be defined and solved recursively. That is, in terms of themselves. A compound sentence is two sentences with “and” between them. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 2 / 12

  4. Recursion Problems—computational, mathematical, and otherwise—can be defined and solved recursively. That is, in terms of themselves. A compound sentence is two sentences with “and” between them. A Python expression may contain two expressions with an operator between them: (3 + 2) * (4 - 9) . Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 2 / 12

  5. Recursion Problems—computational, mathematical, and otherwise—can be defined and solved recursively. That is, in terms of themselves. A compound sentence is two sentences with “and” between them. A Python expression may contain two expressions with an operator between them: (3 + 2) * (4 - 9) . Point a video camera at its own display—hall of mirrors. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 2 / 12

  6. Recursion Problems—computational, mathematical, and otherwise—can be defined and solved recursively. That is, in terms of themselves. A compound sentence is two sentences with “and” between them. A Python expression may contain two expressions with an operator between them: (3 + 2) * (4 - 9) . Point a video camera at its own display—hall of mirrors. Many mathematical structures are defined recursively. ◮ Fibonacci numbers, factorials, fractals, . . . Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 2 / 12

  7. Recursion Problems—computational, mathematical, and otherwise—can be defined and solved recursively. That is, in terms of themselves. A compound sentence is two sentences with “and” between them. A Python expression may contain two expressions with an operator between them: (3 + 2) * (4 - 9) . Point a video camera at its own display—hall of mirrors. Many mathematical structures are defined recursively. ◮ Fibonacci numbers, factorials, fractals, . . . ◮ Mathematicians call this induction (same thing as recursion). ◮ It’s also a common method of mathematical proof. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 2 / 12

  8. Recursion Problems—computational, mathematical, and otherwise—can be defined and solved recursively. That is, in terms of themselves. A compound sentence is two sentences with “and” between them. A Python expression may contain two expressions with an operator between them: (3 + 2) * (4 - 9) . Point a video camera at its own display—hall of mirrors. Many mathematical structures are defined recursively. ◮ Fibonacci numbers, factorials, fractals, . . . ◮ Mathematicians call this induction (same thing as recursion). ◮ It’s also a common method of mathematical proof. Search for recursion on Google. ◮ Note the search suggestion. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 2 / 12

  9. Recursion Problems—computational, mathematical, and otherwise—can be defined and solved recursively. That is, in terms of themselves. A compound sentence is two sentences with “and” between them. A Python expression may contain two expressions with an operator between them: (3 + 2) * (4 - 9) . Point a video camera at its own display—hall of mirrors. Many mathematical structures are defined recursively. ◮ Fibonacci numbers, factorials, fractals, . . . ◮ Mathematicians call this induction (same thing as recursion). ◮ It’s also a common method of mathematical proof. Search for recursion on Google. ◮ Note the search suggestion. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 2 / 12

  10. Recursion in programming The idea behind recursion in programming: ◮ Break down a complex problem into a simpler version of the same problem . Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 3 / 12

  11. Recursion in programming The idea behind recursion in programming: ◮ Break down a complex problem into a simpler version of the same problem . ◮ Implemented by functions that call themselves . Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 3 / 12

  12. Recursion in programming The idea behind recursion in programming: ◮ Break down a complex problem into a simpler version of the same problem . ◮ Implemented by functions that call themselves . ⋆ Recursive functions . Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 3 / 12

  13. Recursion in programming The idea behind recursion in programming: ◮ Break down a complex problem into a simpler version of the same problem . ◮ Implemented by functions that call themselves . ⋆ Recursive functions . ◮ The same computation recurs (occurs repeatedly). ⋆ This is not the same as iteration (looping)! Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 3 / 12

  14. Recursion in programming The idea behind recursion in programming: ◮ Break down a complex problem into a simpler version of the same problem . ◮ Implemented by functions that call themselves . ⋆ Recursive functions . ◮ The same computation recurs (occurs repeatedly). ⋆ This is not the same as iteration (looping)! ⋆ But it is possible to convert iteration to recursion, and vice versa. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 3 / 12

  15. Recursion in programming The idea behind recursion in programming: ◮ Break down a complex problem into a simpler version of the same problem . ◮ Implemented by functions that call themselves . ⋆ Recursive functions . ◮ The same computation recurs (occurs repeatedly). ⋆ This is not the same as iteration (looping)! ⋆ But it is possible to convert iteration to recursion, and vice versa. Recursion is often the most natural way of thinking about a problem. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 3 / 12

  16. Recursion in programming The idea behind recursion in programming: ◮ Break down a complex problem into a simpler version of the same problem . ◮ Implemented by functions that call themselves . ⋆ Recursive functions . ◮ The same computation recurs (occurs repeatedly). ⋆ This is not the same as iteration (looping)! ⋆ But it is possible to convert iteration to recursion, and vice versa. Recursion is often the most natural way of thinking about a problem. ◮ Some computations are very difficult to perform without recursion. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 3 / 12

  17. Recursion in programming The idea behind recursion in programming: ◮ Break down a complex problem into a simpler version of the same problem . ◮ Implemented by functions that call themselves . ⋆ Recursive functions . ◮ The same computation recurs (occurs repeatedly). ⋆ This is not the same as iteration (looping)! ⋆ But it is possible to convert iteration to recursion, and vice versa. Recursion is often the most natural way of thinking about a problem. ◮ Some computations are very difficult to perform without recursion. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 3 / 12

  18. Thinking recursively Suppose we want to write a function that prints a triangle of stars. print triangle(4) → * * * * * * * * * * We could use nested loops, but let’s try using recursion instead. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 4 / 12

  19. Thinking recursively Suppose we want to write a function that prints a triangle of stars. print triangle(4) → * * * * * * * * * * We could use nested loops, but let’s try using recursion instead. ◮ Pretend someone else has already written a function to print a triangle of size 3. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 4 / 12

  20. Thinking recursively Suppose we want to write a function that prints a triangle of stars. print triangle(4) → * * * * * * * * * * We could use nested loops, but let’s try using recursion instead. ◮ Pretend someone else has already written a function to print a triangle of size 3. How would you print a triangle of size 4? Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 4 / 12

  21. Thinking recursively Suppose we want to write a function that prints a triangle of stars. print triangle(4) → * * * * * * * * * * We could use nested loops, but let’s try using recursion instead. ◮ Pretend someone else has already written a function to print a triangle of size 3. How would you print a triangle of size 4? ⋆ First call that function. ⋆ Then print a row of four stars. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 4 / 12

  22. Thinking recursively Suppose we want to write a function that prints a triangle of stars. print triangle(4) → * * * * * * * * * * We could use nested loops, but let’s try using recursion instead. ◮ Pretend someone else has already written a function to print a triangle of size 3. How would you print a triangle of size 4? ⋆ First call that function. ⋆ Then print a row of four stars. ◮ What about size 5? ⋆ Print a triangle of size 4. ⋆ Then print a row of five stars. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 4 / 12

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