Lecture9 Recursive Algorithms

# Lecture9 Recursive Algorithms - Recursion CMPT 126 Lecture...

• Notes
• 10

This preview shows pages 1–3. Sign up to view the full content.

CMPT 126: Lecture 10 Recursion Tamara Smyth, [email protected] School of Computing Science, Simon Fraser University October 25, 2007 1 Recursion Recursion is the process of defining something in terms of itself. Andrew Plotkin (interactive fiction writer, aka Zarf) said: “If you already know what recursion is, just remember the answer. Otherwise, find someone who is standing closer to Douglas Hofstadter than you are; then ask him or her what recursion is.” Consider the following ‘dictionary’ definitions: recursion: See “recursion”. decoration: n. any ornament or adornment used to decorate something. Though such recursive definitions are not always helpful when used in the dictionary, they can be very useful when representing and/or solving mathematical or computer programming problems. CMPT 126: Recursion, Lecture 10 2 Thinking Recursively To program recursively, we must learn to think recursively. Consider the following definition of a List : “a list is one or more numbers separated by commas”. An alternate, equivalent recursive definition is: A List is a: number or a: number comma List The recursive definition defines each of the following lists: 24, 45, 9 2, 56 34, 45, 3, 7, 76, 94, 54, 1 20 The last of list containing a single element is described by the non-recursive part of the definition, that is, the base case. CMPT 126: Recursion, Lecture 10 3 Tracing the Recursive Definition of List 37 LIST LIST comma number comma LIST number LIST number comma number 24, 88, 40, 37 88, 40, 37 24 , 88 , 40, 37 40 , 37 Figure 1: Tracing through a recursive List . Note the definition of List contains one option that is recursive and one that is not. The non-recursive part is called the base case . Without a base case , the recursion would never end, a problem referred to as infinite recursion . CMPT 126: Recursion, Lecture 10 4

This preview has intentionally blurred sections. Sign up to view the full version.

Recursion in Math Consider the factorial function, N ! , defined as the product of all integers between 1 and N . 3! = 3 × 2 × 1 = 6 N ! = N × ( N - 1) × ( N - 2) × ... × 1 What is the base case? Look for the case that’s not defined in terms of N : eg. 1! = 1 or 0! = 1 . What is the recursive case? Define the problem in terms of itself, while reducing the problem size (i.e., reduce N ! to ( N - 1)! ) The factorial function can be expressed recursively as 1! = 1 ( base case ) N ! = N × ( N - 1)! for N > 1 ( recursive case ) The base case is 1!. All other values of N ! are defined recursively as N × ( N - 1)! CMPT 126: Recursion, Lecture 10 5 Recursive Factorial Function Given here in pseudocode: function factorial(n) { if (n<=1) return 1; else return n * factorial(n-1); } Eventually the base case will be reached: 4! = 4 × 3! = 4 × 3 × 2! = 4 × 3 × 2 × 1! (base case reached) = 4 × 3 × 2 × 1 (recursion is terminated) CMPT 126: Recursion, Lecture 10 6 Recursive Programming Recursion is a programming technique in which a method calls itself, that is, a method is defined in tems of itself .
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern