Top Banner
Recursion Recursion
31

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.

Dec 17, 2015

Download

Documents

Piers Webster
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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.

RecursionRecursion

Page 2: 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.

n! (n factorial)n! (n factorial)

The number of ways n objects can be The number of ways n objects can be permuted (arranged).permuted (arranged).

For example, consider 3 things, A, B, and C.For example, consider 3 things, A, B, and C. 3! = 63! = 61.1. ABCABC2.2. ACBACB3.3. CABCAB4.4. CBACBA5.5. BCABCA6.6. BACBAC

The first few factorials for n=0, 1, 2, ... are 1, The first few factorials for n=0, 1, 2, ... are 1, 1, 2, 6, 24, 120, ...1, 2, 6, 24, 120, ...

Page 3: 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.

n! (n factorial)n! (n factorial)

n! for some non negative integer n is defined n! for some non negative integer n is defined as:as: n! = n * (n-1) * (n-2) * … * 2 * 1n! = n * (n-1) * (n-2) * … * 2 * 1 0! is defined as 1.0! is defined as 1.

From From http://mathworld.wolfram.com/Factorial.htmlhttp://mathworld.wolfram.com/Factorial.html

Page 4: 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.

n! (n factorial)n! (n factorial)

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

Page 5: 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.

Triangular numbersTriangular numbers

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

Page 6: 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.

Triangular numbersTriangular numbers

which can also be expressed as:which can also be expressed as: TTnn = 1 = 1 for n = 1for n = 1 TTnn = n + T = n + Tn-1n-1 for n > 1for n > 1

From From http://mathworld.wolfram.com/Triangularhttp://mathworld.wolfram.com/TriangularNumber.htmlNumber.html

n

kn kT

0

Page 7: 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.

Triangular numbersTriangular numbersA plot of the first few triangular numbers A plot of the first few triangular numbers

represented as a sequence of binary bits is represented as a sequence of binary bits is shown below. The top portion shows Tshown below. The top portion shows T11 (1, 3, (1, 3,

6, 10, 15, 21, …) to T6, 10, 15, 21, …) to T255255, and the bottom shows , and the bottom shows

the next 510 values.the next 510 values.0 0 0 0 0 10 0 0 1 1 00 0 1 0 1 10 1 1 1 1 01 1 0 0 1 1

Page 8: 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.

Recurrence relationRecurrence relation

In mathematics, a recurrence relation is an In mathematics, a recurrence relation is an equation that defines a sequence recursively: equation that defines a sequence recursively: each term of the sequence is defined as a each term of the sequence is defined as a function of the preceding terms.function of the preceding terms.

A difference equation is a specific type of A difference equation is a specific type of recurrence relation.recurrence relation.

Some simply defined recurrence relations can Some simply defined recurrence relations can have very complex (chaotic) behaviors and are have very complex (chaotic) behaviors and are sometimes studied by physicists and sometimes studied by physicists and mathematicians in a field of mathematics mathematicians in a field of mathematics known as nonlinear analysis.known as nonlinear analysis.

From From http://en.wikipedia.org/wiki/Recurrence_relationhttp://en.wikipedia.org/wiki/Recurrence_relation

Page 9: 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.

Fibonacci numbersFibonacci numbers

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

21

1

0

1

0

nnn FFF

F

F

Page 10: 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.

Mathematical inductionMathematical induction

The idea of sequences in which later terms are The idea of sequences in which later terms are deduced from earlier ones, which is implicit in the deduced from earlier ones, which is implicit in the principle of mathematical induction, dates to antiquity.principle of mathematical induction, dates to antiquity.

The truth of an infinite sequence of propositions PThe truth of an infinite sequence of propositions Pii for for i=1, ..., i=1, ..., is established if is established if

1. P1. P11 is true, and is true, and2. P2. Pkk implies P implies Pk+1k+1 for all k. for all k.

This principle is sometimes also known as the method This principle is sometimes also known as the method of induction.of induction.

From From http://mathworld.wolfram.com/RecursiveSequence.hthttp://mathworld.wolfram.com/RecursiveSequence.html and ml and http://mathworld.wolfram.com/PrincipleofMathematicalhttp://mathworld.wolfram.com/PrincipleofMathematicalInduction.htmlInduction.html

Page 11: 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.

Mathematical inductionMathematical induction

The idea of sequences in which later The idea of sequences in which later terms are deduced from earlier ones, terms are deduced from earlier ones, which is implicit in the principle of which is implicit in the principle of mathematical induction, dates to mathematical induction, dates to antiquity.antiquity.

The truth of an infinite sequence of The truth of an infinite sequence of propositions Ppropositions Pii for i=1, ..., for i=1, ..., is is established ifestablished if1.1. PP11 is true, and is true, and

2.2. PPkk implies P implies Pk+1k+1 for all k. for all k.

base case(s)

inductive case(s)

Page 12: 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.

Back to n! (n factorial)Back to n! (n factorial)

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

base cases

inductive case

Page 13: 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.

Let’s code n! (n factorial)Let’s code n! (n factorial)

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

public static int nFactorial ( int n ) {public static int nFactorial ( int n ) {

}}

base cases

inductive case

Page 14: 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.

Let’s code n! (n factorial)Let’s code n! (n factorial)

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

public static int nFactorial ( int n ) {public static int nFactorial ( int n ) {//base cases//base casesif (n==0)if (n==0) return 1;return 1;

}}

base cases

inductive case

Page 15: 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.

Let’s code n! (n factorial)Let’s code n! (n factorial)

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

public static int nFactorial ( int n ) {public static int nFactorial ( int n ) {//base cases//base casesif (n==0)if (n==0) return 1;return 1;if (n==1)if (n==1) return 1;return 1;

}}

base cases

inductive case

Page 16: 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.

Let’s code n! (n factorial)Let’s code n! (n factorial)

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

public static int nFactorial ( int n ) {public static int nFactorial ( int n ) {//base cases//base casesif (n==0)if (n==0) return 1;return 1;if (n==1)if (n==1) return 1;return 1;return n * nFactorial( n-1 );return n * nFactorial( n-1 );

}}

base cases

inductive case

Page 17: 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.

Let’s code n! (n factorial)Let’s code n! (n factorial)

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

public static int nFactorial ( int n ) {public static int nFactorial ( int n ) {//base cases//base casesif (n==0)if (n==0) return 1;return 1;if (n==1)if (n==1) return 1;return 1;return n * nFactorial( n-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 );

Page 18: 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.

Back to Triangular Back to Triangular numbersnumbers

TTnn = 1 = 1 for n = 1for n = 1

TTnn = n + T = n + Tn-1n-1 for n > 1for n > 1

What is the base case(s)?What is the base case(s)? What is the inductive case?What is the inductive case? How can we write the code for this?How can we write the code for this?

Page 19: 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.

Back to Fibonacci Back to Fibonacci numbersnumbers

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

21

1

0

1

0

nnn FFF

F

FWhat is the base case(s)?

What is the inductive case?

How can we code this?

Page 20: 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.

A more interesting A more interesting exampleexample

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

Page 21: 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.

A more interesting A more interesting exampleexample

In how many different ways can we select 2 out of 3 In how many different ways can we select 2 out of 3 playing cards {A,B,C} (w/out regard to order)?playing cards {A,B,C} (w/out regard to order)? A BA B A CA C B CB C

Generally called Combinations w/out Repetitions (the Generally called Combinations w/out Repetitions (the Binomial Coefficient):Binomial Coefficient):

where n is the number of objects from which you can where n is the number of objects from which you can choose, and k is the number to be chosen.choose, and k is the number to be chosen.

Page 22: 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.

CombinatoricsCombinatorics

Say we don’t have a closed-form solution Say we don’t have a closed-form solution for the “n choose k” problem.for the “n choose k” problem.

Let’s develop the base cases first.Let’s develop the base cases first.

How many ways can we choose k things out How many ways can we choose k things out of n things (without regard to order) when k of n things (without regard to order) when k = 1?= 1?

Page 23: 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.

CombinatoricsCombinatorics

Let’s develop the base cases first.Let’s develop the base cases first.B1. How many ways can we choose k things B1. How many ways can we choose k things

out of n things (without regard to order) out of n things (without regard to order) when k = 1 (i.e., choose 1 from n things)?when k = 1 (i.e., choose 1 from n things)?

Answer: n Answer: n so ways( k, n ) = n for k=1.so ways( k, n ) = n for k=1.

Page 24: 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.

CombinatoricsCombinatorics

Let’s develop the base cases first.Let’s develop the base cases first.B2. How many ways can we choose k things B2. How many ways can we choose k things

out of n things (without regard to order) out of n things (without regard to order) when k = n (i.e., choose all n things from n when k = n (i.e., choose all n things from n things)?things)?

Page 25: 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.

CombinatoricsCombinatorics

Let’s develop the base cases first.Let’s develop the base cases first.B2. How many ways can we choose k things B2. How many ways can we choose k things

out of n things (without regard to order) out of n things (without regard to order) when k = n (i.e., choose all n things from n when k = n (i.e., choose all n things from n things)?things)?

Answer: 1Answer: 1 so ways( k, n ) = 1 for k=n.so ways( k, n ) = 1 for k=n.

Page 26: 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.

CombinatoricsCombinatorics

Now let’s develop the inductive case.Now let’s develop the inductive case. Ex. ways( 2, 3 ) = 3Ex. ways( 2, 3 ) = 3

1 21 2 1 31 3 2 32 3

Say we always pick card 3.Say we always pick card 3. Then we can only get 1 3 and 2 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 So we are only free to pick 1 or 2 and we have already

said that ways(1,2)=2 which more generally is:said that ways(1,2)=2 which more generally is:

ways( k-1, n-1 )ways( k-1, n-1 )

Page 27: 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.

CombinatoricsCombinatorics

Now let’s develop the inductive case.Now let’s develop the inductive case. Ex. ways( 2, 3 ) = 3Ex. ways( 2, 3 ) = 3

1 21 2 1 31 3 2 32 3

Say we don’t pick card 3.Say we don’t pick card 3. Then we can only pick 1 2.Then we can only pick 1 2. So we can only pick 2 things out of two things.So we can only pick 2 things out of two things. We have already noted that ways( 2, 2 ) = 1 which more We have already noted that ways( 2, 2 ) = 1 which more

generally is:generally is:

ways( k, n-1 )ways( k, n-1 )

Page 28: 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.

CombinatoricsCombinatorics

So our inductive case is:So our inductive case is: ways( k, n ) = ways( k-1, n-1 )ways( k, n ) = ways( k-1, n-1 )

+ ways( k, n-1 )+ ways( k, n-1 ) for 1<k<nfor 1<k<n

Page 29: 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.

CombinatoricsCombinatorics

Putting it all together . . .Putting it all together . . . ways( k, n ) = n for k=1ways( k, n ) = n for k=1 ways( k, n ) = 1 for k=nways( k, n ) = 1 for k=n ways( k, n ) = ways( k-1, n-1 )ways( k, n ) = ways( k-1, n-1 )

+ ways( k, n-1 )+ ways( k, n-1 ) for 1<k<nfor 1<k<n

Page 30: 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.

CombinatoricsCombinatorics

Rules:Rules: ways( k, n ) = n for k=1ways( k, n ) = n for k=1 ways( k, n ) = 1 for k=nways( k, n ) = 1 for k=n ways( k, n ) = ways( k-1, n-1 ) + ways( k, n-1 )ways( k, n ) = ways( k-1, n-1 ) + ways( k, n-1 )

for 1<k<nfor 1<k<n Code: (example: ways( 5, 10 ) = 252)Code: (example: ways( 5, 10 ) = 252)

public static int ways ( int k, int n ) {public static int ways ( int k, int n ) {if (k==1)if (k==1) return n;return n;if (k==n)if (k==n) return 1;return 1;return ways( k-1, n-1 ) + ways( k, n-1 );return ways( k-1, n-1 ) + ways( k, n-1 );

}}

Page 31: 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.

A final note regarding A final note regarding recursion . . .recursion . . .

Calculations such as factorial, Fibonacci Calculations such as factorial, Fibonacci numbers, etc. are fine for introducing the numbers, etc. are fine for introducing the idea of recursion.idea of recursion.

But the real power of recursion (IMHO) is But the real power of recursion (IMHO) is in traversing advanced data structures in traversing advanced data structures such as trees (covered in more advanced such as trees (covered in more advanced classes and used in such as applications classes and used in such as applications as language parsing, games, etc.).as language parsing, games, etc.).