Module #14: Recursion Rosen 5 th ed., §§3.4-3.5 In this class, we will study recursion, one of the most important topics in computer science. In the last.

Slides:



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

Jessie Zhao Course page: 1.
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
Recursion, Divide and Conquer Lam Chi Kit (George) HKOI2007.
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.
10.1 CompSci 102© Michael Frank Today’s topics RecursionRecursion –Recursively defined functions –Recursively defined sets –Structural Induction Reading:
Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 1/18 Module.
Recursion Rosen 5 th ed., § Recursion Sometimes, defining an object explicitly might be difficult.Sometimes, defining an object explicitly might.
Recursion. Recursive Definitions In recursive definitions, we define a function, a predicate, a set, or a more complex structure over an infinite domain.
Chapter Mathematical Induction
Section Section Summary Recursive Algorithms Proving Recursive Algorithms Correct Recursion and Iteration (not yet included in overheads) Merge.
Module #1 - Logic 1 Based on Rosen, Discrete Mathematics & Its Applications. Prepared by (c) , Michael P. Frank. Modified By Mingwu Chen Induction.
22C:19 Discrete Math Induction and Recursion Fall 2011 Sukumar Ghosh.
Induction and recursion
Discrete Mathematics Chapter 4 Induction and Recursion 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)
Discrete Mathematics and Its Applications Sixth Edition By Kenneth Rosen Chapter 4 Induction and Recursion 歐亞書局.
Chapter 4: Induction and Recursion
CSE 311 Foundations of Computing I Lecture 15 Recursive Definitions and Structural Induction Autumn 2011 CSE 3111.
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.
강원대학교 컴퓨터과학전공 문양세 이산수학 (Discrete Mathematics) 재귀 호출 (Recursion)
Section 5.3. Section Summary Recursively Defined Functions Recursively Defined Sets and Structures Structural Induction.
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,
이산수학 (Discrete Mathematics) 3.5 재귀 알고리즘 (Recursive Algorithms) 2006 년 봄학기 문양세 강원대학교 컴퓨터과학과.
Analyzing algorithms & Asymptotic Notation BIO/CS 471 – Algorithms for Bioinformatics.
Divide & Conquer  Themes  Reasoning about code (correctness and cost)  recursion, induction, and recurrence relations  Divide and Conquer  Examples.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Inductive Proofs. Mathematical Induction A powerful, rigorous technique for proving that a predicate P(n) is true for every natural number n, no matter.
Chapter 5 With Question/Answer Animations. Section 5.1.
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.
1 Lecture 3.3: Recursion CS 250, Discrete Structures, Fall 2012 Nitesh Saxena Adopted from previous lectures by Cinda Heeren, Zeph Grunschlag.
ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
Recursive Algorithm (4.4) An algorithm is called recursive if it solves a problem by reducing it to an instance of the same problem with smaller input.
CompSci 102 Discrete Math for Computer Science March 13, 2012 Prof. Rodger Slides modified from Rosen.
Chapter 5 Kenneth Rosen, Discrete Mathematics and its Applications, 7th edition, McGraw Hill Instructor: Longin Jan Latecki, Copyright.
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.
1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
CSE 311 Foundations of Computing I Lecture 15 Strong Induction and Recursive Definitions Spring
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
CS 3343: Analysis of Algorithms
CS2210:0001Discrete Structures Induction and Recursion
Induction and Recursion
Recursively Defined Functions
Mathematical Induction Recursion
CS201: Data Structures and Discrete Mathematics I
Rosen 5th ed., §§ ~18 slides, ~1 lecture
Data Structures Review Session
Applied Discrete Mathematics Week 9: Integer Properties
CS 250, Discrete Structures, Fall 2015 Nitesh Saxena
Chapter 4 Induction and Recursion
Rosen 5th ed., §§ ~18 slides, ~1 lecture
Trevor Brown CS 341: Algorithms Trevor Brown
Math/CSE 1019: Discrete Mathematics for Computer Science Fall 2011
Discrete Mathematics and its Applications
Induction and recursion
Discrete Mathematics and its Applications
Recursion.
Presentation transcript:

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 class, we talk about mathematical induction: when P(n-1) is true, P(n) is also true Now recursion is a little bit more advanced concept than induction

Why Recursion? Sometimes it is difficult to define an object explicitly However, it may be easy to define this object in terms of itself This process is called recursion An object can be any mathematical concept, a function, a set, a sequence, even an algorithm. An object is explained or described using something about itself or some part of itself.

Recursive Definitions In induction, we prove all members of an infinite set have some property P by proving the truth for larger members in terms of that of smaller members. In recursive definitions, we similarly define a function, a predicate or a set over an infinite number of elements by defining the function or predicate value or set-membership of larger elements in terms of that of smaller ones. This slide shows similarity and difference between mathematical induction and recursion The truth for larger member can be derived from the truth for smaller member In recursion, that relationship is generalized for any mathematical object In mathematical induction we talk about some properties of a sequence

Recursion Recursion is a general term for the practice of defining an object in terms of itself (or of part of itself). An inductive proof establishes the truth of P(n+1) recursively in terms of P(n). There are also recursive algorithms, definitions, functions, sequences, and sets.

Recursively Defined Functions Simplest case: One way to define a function f:N  S (for any set S) or series a n =f(n) is to: –Define f(0). –For n>0, define f(n) in terms of f(0),…,f(n−1). E.g.: Define the series a n :≡ 2 n recursively: –Let a 0 :≡ 1. –For n>0, let a n :≡ 2a n-1. Recurrence relation in Ch. 6

Another Example Suppose we define f(n) for all n  N recursively by: –Let f(0)=3 –For all n  N, let f(n+1)=2f(n)+3 What are the values of the following? –f(1)= f(2)= f(3)= f(4)=

Recursive definition of Factorial Give an inductive definition of the factorial function F(n) :≡ n! :≡ 2  3  …  n. –Base case: F(0) :≡ 1 –Recursive part: F(n) :≡ n  F(n-1). F(1)=1 F(2)=2 F(3)=6

The Fibonacci Series The Fibonacci series f n≥0 is a famous series defined by: f 0 :≡ 0, f 1 :≡ 1, f n≥2 :≡ f n−1 + f n−

Inductive Proof about Fib. series Theorem: f n < 2 n. Proof: By induction. Base cases:f 0 = 0 < 2 0 = 1 f 1 = 1 < 2 1 = 2 Inductive step: Use 2 nd principle of induction (strong induction). Assume  k<n, f k < 2 k. Then f n = f n−1 + f n−2 is < 2 n−1 + 2 n−2 < 2 n−1 + 2 n−1 = 2 n. ■ Note use of base cases of recursive def’n. Implicitly for all n  N

Recursively Defined Sets An infinite set S may be defined recursively, by giving: –A small finite set of base elements of S. –A rule for constructing new elements of S from previously-established elements. –Implicitly, S has no other elements but these. Example: Let 3  S, and let x+y  S if x,y  S. What is S?

The Set of All Strings Given an alphabet Σ, the set Σ * of all strings over Σ can be recursively defined as: ε  Σ * (ε :≡ “”, the empty string) w  Σ *  x  Σ → wx  Σ * textbook uses λ instead of ε

Recursive Algorithms (§3.5) Recursive definitions can be used to describe algorithms as well as functions and sets. An algorithm is called recursive if it solves a problem by reducing it to an instance of the same problem with smaller input Example: A procedure to compute a n. procedure power(a≠0: real, n  N) if n = 0 then return 1 else return a · power(a, n−1)

Efficiency of Recursive Algorithms The time complexity of a recursive algorithm may depend critically on the number of recursive calls it makes. Example: Modular exponentiation to a power n can take log(n) time if done right, but linear time if done slightly differently. –Task: Compute b n mod m, where m≥2, n≥0, and 1≤b<m.

Modular Exponentiation Alg. #1 Uses the fact that b n = b·b n−1 and that x·y mod m = x·(y mod m) mod m. (Prove the latter theorem at home.) procedure mpower(b≥1,n≥0,m>b  N) {Returns b n mod m.} if n=0 then return 1 else return (b·mpower(b,n−1,m)) mod m Note this algorithm takes Θ(n) steps!

Modular Exponentiation Alg. #2 Uses the fact that b 2k = b k·2 = (b k ) 2. x·y mod m = (x mod m)·(y mod m) mod m procedure mpower(b,n,m) {same signature} if n=0 then return 1 else if 2|n then return mpower(b,n/2,m) 2 mod m else return (mpower(b,n−1,m)·b) mod m What is its time complexity? Θ(log n) steps

A Slight Variation Nearly identical but takes Θ(n) time instead! procedure mpower(b,n,m) {same signature} if n=0 then return 1 else if 2|n then return (mpower(b,n/2,m)· mpower(b,n/2,m)) mod m else return (mpower(b,n−1,m)·b) mod m The number of recursive calls made is critical.

Recursive Euclid’s Algorithm procedure gcd(a,b  N) if a = 0 then return b else return gcd(b mod a, a) Note recursive algorithms are often simpler to code than iterative ones… However, they can consume more stack space, if your compiler is not smart enough.

Merge Sort Strategy: Recursively split the list in half and merge the two returned segments.

A = splitmerge

A = B = if (A[first] < A[last]) B[next] = A[first]; first++; else B[next] = A[last]; last++; next++; first last next Merge Process

A = Levels = ?log(n) Work = ?n Total Complexity = n*log(n) Number of compares at each level during merge

Merge Sort procedure sort(L = 1,…, n ) if n>1 then m :=  n/2  {this is rough ½-way point} L := merge(sort( 1,…, m ), sort( m+1,…, n )) return L The merge takes Θ(n) steps, and merge-sort takes Θ(n log n).

Merge Routine procedure merge(A, B: sorted lists) L = empty list i:=0, j:=0, k:=0 (i:first, j:last, k:next in slide 20) while i<|A| v j<|B| {|A| is length of A} if i=|A| then L k := B j ; j := j + 1 else if j=|B| then L k := A i ; i := i + 1 else if A i < B j then L k := A i ; i := i + 1 else L k := B j ; j := j + 1 k := k+1 return L Takes Θ(|A|+|B|) time