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.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms II
Advertisements

1 Counting Techniques: Possibility Trees, Multiplication Rule, Permutations.
3.5 Paradox  1.Russell’s paradox  A  A, A  A 。  Russell’s paradox: Let S={A|A  A}. The question is, does S  S?  i.e. S  S or S  S?  If S  S,
CSCE 2100: Computing Foundations 1 Combinatorics Tamara Schneider Summer 2013.
Introductory Lecture. What is Discrete Mathematics? Discrete mathematics is the part of mathematics devoted to the study of discrete (as opposed to continuous)
Kavita Math231 Recursion and Iteration. Kavita Math231 We use Recursion when we have to perform a complex task that can be broken into the several subtasks.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
CS70 L23 Hashing (1)Dan Garcia © UCB Dan Garcia ( inst.eecs.berkeley.edu/~cs70/ 1 Handout: notes Computer Science.
1 More Counting Techniques Possibility trees Multiplication rule Permutations Combinations.
Discrete Mathematics Recursion and Sequences
Copyright © Ed2Net Learning Inc.1. 2 Warm Up 1.List all combinations of Roshanda, Shelli, Toshi, and Hector, taken three at a time 2. In how many ways.
Recursive Definitions Rosen, 3.4 Recursive (or inductive) Definitions Sometimes easier to define an object in terms of itself. This process is called.
IA32 Addressing Modes Chapter 5 The ISA Level cont’d.
Game Theory.
Copyright © Cengage Learning. All rights reserved.
Applied Discrete Mathematics Week 9: Relations
Rev.S08 MAC 1140 Module 12 Introduction to Sequences, Counting, The Binomial Theorem, and Mathematical Induction.
CS 490 Mathematical Logic, Combinatorics, Counting Arguments, Graph Theory, Number Theory, Discrete Probability, Recurrence Relation. Thao Tran.
Set, Combinatorics, Probability & Number Theory Mathematical Structures for Computer Science Chapter 3 Copyright © 2006 W.H. Freeman & Co.MSCS Slides Set,
Sequences and Series. Quick Review.
Discrete Mathematics, 1st Edition Kevin Ferland
1 Permutations and Combinations. 2 In this section, techniques will be introduced for counting the unordered selections of distinct objects and the ordered.
Discrete Mathematics, Part II CSE 2353 Fall 2007 Margaret H. Dunham Department of Computer Science and Engineering Southern Methodist University Some slides.
The importance of sequences and infinite series in calculus stems from Newton’s idea of representing functions as sums of infinite series.  For instance,
On the Symmetries of Pascal’s Pyramid
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
Discrete Mathematical Structures (Counting Principles)
Section 5.3. Section Summary Recursively Defined Functions Recursively Defined Sets and Structures Structural Induction.
Stephen P. Carl - CS 2421 Recursion Reading : Chapter 4.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Slide 7- 1 Copyright © 2006 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Unit – 1 : FOUNDATIONS Syllabus: Sets – relations – equivalence relations partial orders – functions – recursive functions sequences – induction principle.
Relationships Between Structures “→” ≝ “Can be defined in terms of” Programs Groups Proofs Trees Complex numbers Operators Propositions Graphs Real.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 9- 1.
Foundations of Discrete Mathematics Chapters 5 By Dr. Dalia M. Gil, Ph.D.
DISCRETE COMPUTATIONAL STRUCTURES CS Fall 2005.
© The McGraw-Hill Companies, Inc., Chapter 4 Counting Techniques.
Permutations and Combinations
Unit 7 Permutation and Combination IT Disicipline ITD1111 Discrete Mathematics & Statistics STDTLP 1 Unit 7 Permutation and Combination.
Copyright © Cengage Learning. All rights reserved. CHAPTER 7 FUNCTIONS.
IB Computer Science Unit 5 – Advanced Topics Recursion.
Sequences & Series MATH Precalculus S. Rook.
Sequences and Summations Section 2.4. Section Summary Sequences. – Examples: Geometric Progression, Arithmetic Progression Recurrence Relations – Example:
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
The two way frequency table The  2 statistic Techniques for examining dependence amongst two categorical variables.
CS201: Data Structures and Discrete Mathematics I
Discrete Mathematics Recurrence Relations Chapter 5 R. Johnsonbaugh
ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department.
Combinatorics Sep. 23, 2013.
Chapter 8: Probability: The Mathematics of Chance Lesson Plan Probability Models and Rules Discrete Probability Models Equally Likely Outcomes Continuous.
Counting Principles Multiplication rule Permutations Combinations.
1 CS 140 Discrete Mathematics Combinatorics And Review Notes.
Mathematical Induction Section 5.1. Climbing an Infinite Ladder Suppose we have an infinite ladder: 1.We can reach the first rung of the ladder. 2.If.
Permutations and Combinations. 2 In this section, techniques will be introduced for counting the unordered selections of distinct objects and the ordered.
Recursion and Combinatorial Algorithms academy.zariba.com 1.
CompSci 102 Discrete Math for Computer Science March 13, 2012 Prof. Rodger Slides modified from Rosen.
1 Section 5.3 Permutations and Combinations Permutations (order counts) A permutation of a set is an arrangement of the objects from a set. There are n!
5 Lecture in math Predicates Induction Combinatorics.
Introductory Lecture. What is Discrete Mathematics? Discrete mathematics is the part of mathematics devoted to the study of discrete (as opposed to continuous)
(Proof By) Induction Recursion
Pramada Ramachandran, Assistant Professor, Department of Mathematics,
More Mathematical Techniques
More Simple Recursive Problems
Combinatorics.
Trees.
Lecture 7 Functions.
Combinatorics.
COUNTING AND PROBABILITY
Combinatorics.
Combinatorics.
Presentation transcript:

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 3.CAB 4.CBA 5.BCA 6.BAC  The first few factorials for n=0, 1, 2,... are 1, 1, 2, 6, 24, 120,...

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

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

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, ,..., so the first few triangle numbers are 1, 3, 6, 10, 15, 21,...

Triangular numbers  which can also be expressed as:  T n = 1for n = 1  T n = n + T n-1 for n > 1  From Number.html Number.html Number.html

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

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

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

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 ml and Induction.html ml Induction.html ml Induction.html

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)

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

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

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

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

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

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 );

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?

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?

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

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.

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?

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.

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)?

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.

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 )

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 )

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

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

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 ); }

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.).