6 Recursion - 12/09/2010 Recursion 2 Arises in two forms in...

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

View Full Document Right Arrow Icon
12/09/2010 1 RECURSION Lecture 6 CS2110 – Fall 2009 Recursion 2 ± Arises in two forms in computer science ± We’ll explore both ± Recursion as a mathematica tool for defining a Recursion as a mathematical tool for defining a function in terms of its own value in a simpler case ± Recursion as a programming tool. You’ve seen this previously but we’ll take it to mind-bending extremes (by the end of the class it will seem easy!) Recursion as a math technique 3 ± Broadly, recursion is a powerful technique for specifying functions, sets, and programs ± Example recursively-defined functions and programs ± factorial ± combinations ± exponentiation (raising to an integer power) ± Example recursively-defined sets ± grammars ± expressions ± data structures (lists, trees, . ..) The Factorial Function (n!) 4 ± Define n! = n·(n 1)·(n 2)···3·2·1 read: “n factorial” ± E.g., 3! = 3·2·1 = 6 ± By convention, 0! = 1 ± The function int int that gives n! on input n is called the factorial function The Factorial Function (n!) 5 ± n! is the number of permutations of n distinct objects ± There is just one permutation of one object. 1! = 1 ± There are two permutations of two objects: 2! = 2 There are two permutations of two objects: 2! = 2 1 2 2 1 ± There are six permutations of three objects: 3! = 6 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 ± If n > 0, n! = n·(n 1)! Permutations of 6 Permutations of non-orange blocks ± Total number = 4·3! = 4·6 = 24: 4! Each permutation of the three non- orange blocks gives four permutations when the orange block is included
Background image of page 1

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

View Full DocumentRight Arrow Icon
12/09/2010 2 Observation 7 ± One way to think about the task of permuting the four colored blocks was to start by computing all permutations of three blocks, then finding all ways to add a fourth block ± And this “explains” why the number of permutations turns out to be 4! ± Can generalize to prove that the number of permutations of n blocks is n! A Recursive Program 8 0! = 1 n! = n·(n 1)!, n > 0 6 Execution of fact(4) fact(4) 24 static int fact(int n) { if (n = = 0) return 1; else return n*fact(n-1); } 1 1 2 fact(1) fact(3) fact(0) fact(2) General Approach to Writing Recursive Functions 9 1. Try to find a parameter, say n, such that the solution for n can be obtained by combining solutions to the same problem using smaller values of n (e.g., (n-1) in our factorial example) 2. Find base case(s) – small values of n for which you can just write down the solution (e.g., 0! = 1) 3. Verify that, for any valid value of n, applying the reduction of step 1 repeatedly will ultimately hit one of the base cases A cautionary note 10 ± Keep in mind that each instance of your
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/30/2011 for the course CS 2110 taught by Professor Francis during the Fall '07 term at Cornell University (Engineering School).

Page1 / 6

6 Recursion - 12/09/2010 Recursion 2 Arises in two forms in...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online