Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion. n! (n factorial)  The number of ways n objects can be permuted (arranged).  For example, consider 3 things, A, B, and C.  3! = 6 1.ABC 2.ACB.

Similar presentations


Presentation on theme: "Recursion. n! (n factorial)  The number of ways n objects can be permuted (arranged).  For example, consider 3 things, A, B, and C.  3! = 6 1.ABC 2.ACB."— Presentation transcript:

1 Recursion

2 n! (n factorial)  The number of ways n objects can be permuted (arranged).  For example, consider 3 things, A, B, and C.  3! = 6 1.ABC 2.ACB 3.CAB 4.CBA 5.BCA 6.BAC  The first few factorials for n=0, 1, 2,... are 1, 1, 2, 6, 24, 120,...

3 n! (n factorial)  n! for some non negative integer n is defined as:  n! = n * (n-1) * (n-2) * … * 2 * 1  0! is defined as 1.  From http://mathworld.wolfram.com/Factorial.html http://mathworld.wolfram.com/Factorial.html

4 n! (n factorial)  n! for some non negative integer n can be rewritten as:  0! = 1for n = 0  1! = 1for n = 1  n! = n * (n-1)! for all other n > 1

5 Triangular numbers  The triangular number T n can be represented in the form of a triangular grid of points where the first row contains a single element and each subsequent row contains one more element than the previous one. The triangular numbers are therefore 1, 1+2, 1+2+3, 1+2+3+4,..., so the first few triangle numbers are 1, 3, 6, 10, 15, 21,...

6 Triangular numbers  which can also be expressed as:  T n = 1for n = 1  T n = n + T n-1 for n > 1  From http://mathworld.wolfram.com/Triangular Number.html http://mathworld.wolfram.com/Triangular Number.html http://mathworld.wolfram.com/Triangular Number.html

7 Triangular numbers A plot of the first few triangular numbers represented as a sequence of binary bits is shown below. The top portion shows T 1 (1, 3, 6, 10, 15, 21, …) to T 255, and the bottom shows the next 510 values. 0 0 0 0 0 1 0 0 0 1 1 0 0 0 1 0 1 1 0 1 1 1 1 0 1 1 0 0 1 1

8 Recurrence relation  In mathematics, a recurrence relation is an equation that defines a sequence recursively: each term of the sequence is defined as a function of the preceding terms.  A difference equation is a specific type of recurrence relation.  Some simply defined recurrence relations can have very complex (chaotic) behaviors and are sometimes studied by physicists and mathematicians in a field of mathematics known as nonlinear analysis.  From http://en.wikipedia.org/wiki/Recurrence_relation http://en.wikipedia.org/wiki/Recurrence_relation

9 Fibonacci numbers  The sequence of Fibonacci numbers begins: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,...

10 Mathematical induction  The idea of sequences in which later terms are deduced from earlier ones, which is implicit in the principle of mathematical induction, dates to antiquity.  The truth of an infinite sequence of propositions P i for i=1,...,  is established if 1. P 1 is true, and 2. P k implies P k+1 for all k.  This principle is sometimes also known as the method of induction.  From http://mathworld.wolfram.com/RecursiveSequence.ht ml and http://mathworld.wolfram.com/PrincipleofMathematical Induction.html http://mathworld.wolfram.com/RecursiveSequence.ht ml http://mathworld.wolfram.com/PrincipleofMathematical Induction.html http://mathworld.wolfram.com/RecursiveSequence.ht ml http://mathworld.wolfram.com/PrincipleofMathematical Induction.html

11 Mathematical induction  The idea of sequences in which later terms are deduced from earlier ones, which is implicit in the principle of mathematical induction, dates to antiquity.  The truth of an infinite sequence of propositions P i for i=1,...,  is established if 1.P 1 is true, and 2.P k implies P k+1 for all k. base case(s) inductive case(s)

12 Back to n! (n factorial)  n! for some non negative integer n can be rewritten as:  0! = 1for n = 0  1! = 1for n = 1  n! = n * (n-1)! for all other n > 1 base cases inductive case

13 Let’s code n! (n factorial)  n! for some non negative integer n can be rewritten as:  0! = 1for n = 0  1! = 1for n = 1  n! = n * (n-1)! for all other n > 1 public static int nFactorial ( int n ) { } base cases inductive case

14 Let’s code n! (n factorial)  n! for some non negative integer n can be rewritten as:  0! = 1for n = 0  1! = 1for n = 1  n! = n * (n-1)! for all other n > 1 public static int nFactorial ( int n ) { //base cases if (n==0)return 1; } base cases inductive case

15 Let’s code n! (n factorial)  n! for some non negative integer n can be rewritten as:  0! = 1for n = 0  1! = 1for n = 1  n! = n * (n-1)! for all other n > 1 public static int nFactorial ( int n ) { //base cases if (n==0)return 1; if (n==1)return 1; } base cases inductive case

16 Let’s code n! (n factorial)  n! for some non negative integer n can be rewritten as:  0! = 1for n = 0  1! = 1for n = 1  n! = n * (n-1)! for all other n > 1 public static int nFactorial ( int n ) { //base cases if (n==0)return 1; if (n==1)return 1; return n * nFactorial( n-1 ); } base cases inductive case

17 Let’s code n! (n factorial)  n! for some non negative integer n can be rewritten as:  0! = 1for n = 0  1! = 1for n = 1  n! = n * (n-1)! for all other n > 1 public static int nFactorial ( int n ) { //base cases if (n==0)return 1; if (n==1)return 1; return n * nFactorial( n-1 ); } This is an example of a recursive function (a function that calls itself)! To use this function: int result = nFactorial( 10 );

18 Back to Triangular numbers  T n = 1for n = 1  T n = n + T n-1 for n > 1  What is the base case(s)?  What is the inductive case?  How can we write the code for this?

19 Back to Fibonacci numbers  The sequence of Fibonacci numbers begins: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,... What is the base case(s)? What is the inductive case? How can we code this?

20 A more interesting example  “Combinatorics is a branch of pure mathematics concerning the study of discrete (and usually finite) objects. It is related to many other areas of mathematics, such as algebra, probability theory, ergodic theory and geometry, as well as to applied subjects such as computer science and statistical physics. Aspects of combinatorics include "counting" the objects satisfying certain criteria (enumerative combinatorics), deciding when the criteria can be met, and constructing and analyzing objects meeting the criteria (as in combinatorial designs and matroid theory), finding "largest", "smallest", or "optimal" objects (extremal combinatorics and combinatorial optimization), and finding algebraic structures these objects may have (algebraic combinatorics).” from http://en.wikipedia.org/wiki/Combinatorics http://en.wikipedia.org/wiki/Combinatorics

21 A more interesting example  In how many different ways can we select 2 out of 3 playing cards {A,B,C} (w/out regard to order)?  A B  A C  B C  Generally called Combinations w/out Repetitions (the Binomial Coefficient):  where n is the number of objects from which you can choose, and k is the number to be chosen.

22 Combinatorics  Say we don’t have a closed-form solution for the “n choose k” problem.  Let’s develop the base cases first.  How many ways can we choose k things out of n things (without regard to order) when k = 1?

23 Combinatorics  Let’s develop the base cases first. B1. How many ways can we choose k things out of n things (without regard to order) when k = 1 (i.e., choose 1 from n things)?  Answer: n so ways( k, n ) = n for k=1.

24 Combinatorics  Let’s develop the base cases first. B2. How many ways can we choose k things out of n things (without regard to order) when k = n (i.e., choose all n things from n things)?

25 Combinatorics  Let’s develop the base cases first. B2. How many ways can we choose k things out of n things (without regard to order) when k = n (i.e., choose all n things from n things)?  Answer: 1so ways( k, n ) = 1 for k=n.

26 Combinatorics  Now let’s develop the inductive case.  Ex. ways( 2, 3 ) = 3  1 2  1 3  2 3  Say we always pick card 3.  Then we can only get 1 3 and 2 3.  So we are only free to pick 1 or 2 and we have already said that ways(1,2)=2 which more generally is:  ways( k-1, n-1 )

27 Combinatorics  Now let’s develop the inductive case.  Ex. ways( 2, 3 ) = 3  1 2  1 3  2 3  Say we don’t pick card 3.  Then we can only pick 1 2.  So we can only pick 2 things out of two things.  We have already noted that ways( 2, 2 ) = 1 which more generally is:  ways( k, n-1 )

28 Combinatorics  So our inductive case is:  ways( k, n ) = ways( k-1, n-1 ) + ways( k, n-1 )for 1<k<n

29 Combinatorics  Putting it all together...  ways( k, n ) = n for k=1  ways( k, n ) = 1 for k=n  ways( k, n ) = ways( k-1, n-1 ) + ways( k, n-1 )for 1<k<n

30 Combinatorics  Rules:  ways( k, n ) = n for k=1  ways( k, n ) = 1 for k=n  ways( k, n ) = ways( k-1, n-1 ) + ways( k, n-1 ) for 1<k<n  Code: (example: ways( 5, 10 ) = 252) public static int ways ( int k, int n ) { if (k==1)return n; if (k==n)return 1; return ways( k-1, n-1 ) + ways( k, n-1 ); }

31 A final note regarding recursion...  Calculations such as factorial, Fibonacci numbers, etc. are fine for introducing the idea of recursion.  But the real power of recursion (IMHO) is in traversing advanced data structures such as trees (covered in more advanced classes and used in such as applications as language parsing, games, etc.).


Download ppt "Recursion. n! (n factorial)  The number of ways n objects can be permuted (arranged).  For example, consider 3 things, A, B, and C.  3! = 6 1.ABC 2.ACB."

Similar presentations


Ads by Google