Fall 2002CMSC 203 - Discrete Structures1 If I told you once, it must be... Recursion.

Slides:



Advertisements
Similar presentations
Fall 2002CMSC Discrete Structures1 Enough Mathematical Appetizers! Let us look at something more interesting: Algorithms.
Advertisements

CSE 211- Discrete Structures
Fall 2002CMSC Discrete Structures1 … and now for something completely different… Set Theory Actually, you will see that logic and set theory are.
Fall 2002CMSC Discrete Structures1 Follow me for a walk through... MathematicalInduction.
Mathematical Induction
Fall 2002CMSC Discrete Structures1 You Never Escape Your… Relations.
Fall 2002CMSC Discrete Structures1 Now it’s Time for… RecurrenceRelations.
22C:19 Discrete Structures Induction and Recursion Spring 2014 Sukumar Ghosh.
22C:19 Discrete Structures Induction and Recursion Fall 2014 Sukumar Ghosh.
Recursive Definitions and Structural Induction
Recursively Defined Functions
Induction and Recursion. Odd Powers Are Odd Fact: If m is odd and n is odd, then nm is odd. Proposition: for an odd number m, m k is odd for all non-negative.
CSE115/ENGR160 Discrete Mathematics 04/03/12 Ming-Hsuan Yang UC Merced 1.
CSE115/ENGR160 Discrete Mathematics 04/05/11 Ming-Hsuan Yang UC Merced 1.
Recursive Definitions Rosen, 3.4. Recursive (or inductive) Definitions Sometimes easier to define an object in terms of itself. This process is called.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
CSE115/ENGR160 Discrete Mathematics 03/31/11
Section Section Summary Recursive Algorithms Proving Recursive Algorithms Correct Recursion and Iteration (not yet included in overheads) Merge.
Lecture 4 Discrete Mathematics Harper Langston. Algorithms Algorithm is step-by-step method for performing some action Cost of statements execution –Simple.
Module #1 - Logic 1 Based on Rosen, Discrete Mathematics & Its Applications. Prepared by (c) , Michael P. Frank. Modified By Mingwu Chen Induction.
1 Section 3.4 Recursive Definitions. 2 Recursion Recursion is the process of defining an object in terms of itself Technique can be used to define sequences,
Induction and Recursion by: Mohsin tahir (GL) Numan-ul-haq Waqas akram Rao arslan Ali asghar.
22C:19 Discrete Math Induction and Recursion Fall 2011 Sukumar Ghosh.
Induction and recursion
Discrete Mathematics Chapter 4 Induction and Recursion 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)
Mathematical Maxims and Minims, 1988
12-CRS-0106 REVISED 8 FEB 2013 KUG1C3 Dasar Algoritma dan Pemrograman.
Mathematics Review Exponents Logarithms Series Modular arithmetic Proofs.
Discrete Mathematics and Its Applications Sixth Edition By Kenneth Rosen Chapter 4 Induction and Recursion 歐亞書局.
Week #7 – 7/9/11 October 2002 Prof. Marie desJardins
Chapter 4: Induction and Recursion
Chapter 3 (Part 3): Mathematical Reasoning, Induction & Recursion  Recursive Algorithms (3.5)  Program Correctness (3.6)
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 4 (Part 3): Mathematical Reasoning, Induction.
Section 5.3. Section Summary Recursively Defined Functions Recursively Defined Sets and Structures Structural Induction.
Recursive. 2 Recursive Definitions In a recursive definition, an object is defined in terms of itself. We can recursively define sequences, functions.
INDUCTION AND RECURSION. PRINCIPLE OF MATHEMATICAL INDUCTION To prove that P(n) is true for all positive integers n, where P(n) is a propositional function,
Module #14: Recursion Rosen 5 th ed., §§ In this class, we will study recursion, one of the most important topics in computer science. In the last.
March 3, 2015Applied Discrete Mathematics Week 5: Mathematical Reasoning 1Arguments Just like a rule of inference, an argument consists of one or more.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Chap 3 –A theorem is a statement that can be shown to be true –A proof is a sequence of statements to show that a theorem is true –Axioms: statements which.
ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department.
Inductive Proofs and Inductive Definitions Jim Skon.
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.
CompSci 102 Discrete Math for Computer Science March 13, 2012 Prof. Rodger Slides modified from Rosen.
1 Discrete Mathematical Mathematical Induction ( الاستقراء الرياضي )
12-CRS-0106 REVISED 8 FEB 2013 KUG1C3 Dasar Algoritma dan Pemrograman.
6/12/2016 Prepared by Dr.Saad Alabbad1 CS100 : Discrete Structures Proof Techniques(2) Mathematical Induction & Recursion Dr.Saad Alabbad Department of.
Chapter 5 With Question/Answer Animations 1. Chapter Summary Mathematical Induction - Sec 5.1 Strong Induction and Well-Ordering - Sec 5.2 Lecture 18.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Mathematical Induction And Recursion Discrete Math Team KS MATEMATIKA DISKRIT (DISCRETE MATHEMATICS ) 1.
Fall 2002CMSC Discrete Structures1 Chapter 3 Sequences Mathematical Induction Recursion Recursion.
(Proof By) Induction Recursion
Chapter 4: Induction and Recursion
Recursive Algorithms Section 5.4.
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
CS2210:0001Discrete Structures Induction and Recursion
Induction and Recursion
Recursively Defined Functions
Mathematical Induction Recursion
Chapter 5 Induction and Recursion
Follow me for a walk through...
Applied Discrete Mathematics Week 9: Integer Properties
Follow me for a walk through...
Advanced Analysis of Algorithms
Mathematical Induction
Follow me for a walk through...
Recursion.
Presentation transcript:

Fall 2002CMSC Discrete Structures1 If I told you once, it must be... Recursion

Fall 2002CMSC Discrete Structures2 Recursive Definitions Recursion is a principle closely related to mathematical induction. In a recursive definition, an object is defined in terms of itself. We can recursively define sequences, functions and sets.

Fall 2002CMSC Discrete Structures3 Recursively Defined Sequences Example: The sequence {a n } of powers of 2 is given by a n = 2 n for n = 0, 1, 2, …. The same sequence can also be defined recursively: a 0 = 1 a n+1 = 2a n for n = 0, 1, 2, … Obviously, induction and recursion are similar principles.

Fall 2002CMSC Discrete Structures4 Recursively Defined Functions We can use the following method to define a function with the natural numbers as its domain: 1. Specify the value of the function at zero. 2. Give a rule for finding its value at any integer from its values at smaller integers. Such a definition is called recursive or inductive definition.

Fall 2002CMSC Discrete Structures5 Recursively Defined Functions Example: f(0) = 3 f(n + 1) = 2f(n) + 3 f(0) = 3 f(1) = 2f(0) + 3 = 2  = 9 f(2) = 2f(1) + 3 = 2  = 21 f(3) = 2f(2) + 3 = 2  = 45 f(4) = 2f(3) + 3 = 2  = 93

Fall 2002CMSC Discrete Structures6 Recursively Defined Functions How can we recursively define the factorial function f(n) = n! ? f(0) = 1 f(n + 1) = (n + 1)f(n) f(0) = 1 f(1) = 1f(0) = 1  1 = 1 f(2) = 2f(1) = 2  1 = 2 f(3) = 3f(2) = 3  2 = 6 f(4) = 4f(3) = 4  6 = 24

Fall 2002CMSC Discrete Structures7 Recursively Defined Functions A famous example: The Fibonacci numbers f(0) = 0, f(1) = 1 f(n) = f(n – 1) + f(n - 2) f(0) = 0 f(1) = 1 f(2) = f(1) + f(0) = = 1 f(3) = f(2) + f(1) = = 2 f(4) = f(3) + f(2) = = 3 f(5) = f(4) + f(3) = = 5 f(6) = f(5) + f(4) = = 8

Fall 2002CMSC Discrete Structures8 Recursively Defined Sets If we want to recursively define a set, we need to provide two things: an initial set of elements, an initial set of elements, rules for the construction of additional elements from elements in the set. rules for the construction of additional elements from elements in the set. Example: Let S be recursively defined by: 3  S (x + y)  S if (x  S) and (y  S) S is the set of positive integers divisible by 3.

Fall 2002CMSC Discrete Structures9 Recursively Defined Sets Proof: Let A be the set of all positive integers divisible by 3. To show that A = S, we must show that A  S and S  A. Part I: To prove that A  S, we must show that every positive integer divisible by 3 is in S. We will use mathematical induction to show this.

Fall 2002CMSC Discrete Structures10 Recursively Defined Sets Let P(n) be the statement “3n belongs to S”. Basis step: P(1) is true, because 3 is in S. Inductive step: To show: If P(n) is true, then P(n + 1) is true. Assume 3n is in S. Since 3n is in S and 3 is in S, it follows from the recursive definition of S that 3n + 3 = 3(n + 1) is also in S. Conclusion of Part I: A  S.

Fall 2002CMSC Discrete Structures11 Recursively Defined Sets Part II: To show: S  A. Basis step: To show: All initial elements of S are in A. 3 is in A. True. Inductive step: To show: (x + y) is in A whenever x and y are in S. If x and y are both in A, it follows that 3 | x and 3 | y. From Theorem I, Section 2.3, it follows that 3 | (x + y). Conclusion of Part II: S  A. Overall conclusion: A = S.

Fall 2002CMSC Discrete Structures12 Recursively Defined Sets Another example: The well-formed formulae of variables, numerals and operators from {+, -, *, /, ^} are defined by: x is a well-formed formula if x is a numeral or variable. (f + g), (f – g), (f * g), (f / g), (f ^ g) are well- formed formulae if f and g are.

Fall 2002CMSC Discrete Structures13 Recursively Defined Sets With this definition, we can construct formulae such as: (x – y) ((z / 3) – y) ((z / 3) – (6 + 5)) ((z / (2 * 4)) – (6 + 5))

Fall 2002CMSC Discrete Structures14 Recursive Algorithms An algorithm is called recursive if it solves a problem by reducing it to an instance of the same problem with smaller input. Example I: Recursive Euclidean Algorithm procedure gcd(a, b: nonnegative integers with a < b) if a = 0 then gcd(a, b) := b else gcd(a, b) := gcd(b mod a, a)

Fall 2002CMSC Discrete Structures15 Recursive Algorithms Example II: Recursive Fibonacci Algorithm procedure fibo(n: nonnegative integer) if n = 0 then fibo(0) := 0 else if n = 1 then fibo(1) := 1 else fibo(n) := fibo(n – 1) + fibo(n – 2)

Fall 2002CMSC Discrete Structures16 Recursive Algorithms Recursive Fibonacci Evaluation: f(4) f(3) f(2) f(1) f(0) f(1) f(2) f(1) f(0)

Fall 2002CMSC Discrete Structures17 Recursive Algorithms procedure iterative_fibo(n: nonnegative integer) if n = 0 then y := 0 elsebegin x := 0 y := 1 for i := 1 to n-1 begin z := x + y x : = y y := z end end {y is the n-th Fibonacci number}

Fall 2002CMSC Discrete Structures18 Recursive Algorithms For every recursive algorithm, there is an equivalent iterative algorithm. Recursive algorithms are often shorter, more elegant, and easier to understand than their iterative counterparts. However, iterative algorithms are usually more efficient in their use of space and time.