L06cs2110fa09-6up - 24/08/2009 RECURSION Lecture 6 CS2110...

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

View Full Document Right Arrow Icon
24/08/2009 1 RECURSION Lecture 6 CS2110 – Fall 2009 2 3 Recursion Overview 4 ± Recursion is a powerful technique for specifying functions, sets, and programs ± Example recursively-defined functions and programs ± factorial factorial ± combinations ± exponentiation (raising to an integer power) ± Example recursively-defined sets ± grammars ± expressions ± data structures (lists, trees, . ..) The Factorial Function (n!) 5 ± 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!) 6 ± 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)!
Background image of page 1

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

View Full DocumentRight Arrow Icon
24/08/2009 2 Permutations of 7 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 Observation 8 ± 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 9 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 10 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 11 ± Keep in mind that each instance of your recursive function has its own local variables ± Also, remember that “higher” instances are waiting while “lower” instances run ± Not such a good idea to touch global variables from within recursive functions ± Legal… but a common source of errors ± Must have a really clear mental picture of how
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 03/08/2010 for the course CS 2110 taught by Professor Francis during the Spring '07 term at Cornell University (Engineering School).

Page1 / 6

L06cs2110fa09-6up - 24/08/2009 RECURSION Lecture 6 CS2110...

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