Section 5.3 1. Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.

Slides:



Advertisements
Similar presentations
22C:19 Discrete Structures Induction and Recursion Spring 2014 Sukumar Ghosh.
Advertisements

22C:19 Discrete Structures Induction and Recursion Fall 2014 Sukumar Ghosh.
Recursive Definitions and Structural Induction
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.
UCI ICS/Math 6A, Summer Recursion -1 after Strong Induction “Normal” Induction “Normal” Induction: If we prove.
UCI ICS/Math 6D5-Recursion -1 Strong Induction “Normal” Induction “Normal” Induction: If we prove that 1) P(n 0 ) 2) For any k≥n 0, if P(k) then P(k+1)
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.
CSE 321 Discrete Structures Winter 2008 Lecture 12 Induction.
CSE115/ENGR160 Discrete Mathematics 03/31/11
Lecture Recursive Definitions. Fractals fractals are examples of images where the same elements is being recursively.
Lecture Recursive Definitions. Fractals fractals are examples of images where the same elements is being recursively.
Chapter Mathematical Induction
Section Section Summary Recursive Algorithms Proving Recursive Algorithms Correct Recursion and Iteration (not yet included in overheads) Merge.
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,
Climbing an Infinite Ladder
Induction and recursion
22C:19 Discrete Math Induction and Recursion Fall 2011 Sukumar Ghosh.
Induction and recursion
Discrete Mathematics Chapter 4 Induction and Recursion 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)
Applied Discrete Mathematics Week 9: Relations
Recursive Definitions and Structural Induction
Discrete Mathematics CS 2610 March 26, 2009 Skip: structural induction generalized induction Skip section 4.5.
Induction and recursion
Discrete Mathematics and Its Applications Sixth Edition By Kenneth Rosen Chapter 4 Induction and Recursion 歐亞書局.
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.
Induction and recursion
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.
4.3 Recursive Definitions and Structural Induction Sometimes it is difficult to define an object explicitly. However, it may be easy to define this object.
Reading and Writing Mathematical Proofs Spring 2015 Lecture 4: Beyond Basic Induction.
Induction Proof. Well-ordering A set S is well ordered if every subset has a least element. [0, 1] is not well ordered since (0,1] has no least element.
Recursive Algorithms &
ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department.
CS 103 Discrete Structures Lecture 13 Induction and Recursion (1)
1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield.
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.
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.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Chapter 5 With Question/Answer Animations 1. Chapter Summary Mathematical Induction - Sec 5.1 Strong Induction and Well-Ordering - Sec 5.2 Lecture 18.
Induction and Recursion CSC-2259 Discrete Structures Konstantin Busch - LSU1.
Recursive Definitions and Structural Induction ICS 6D Sandy Irani.
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.
(Proof By) Induction Recursion
Chapter 4: Induction and Recursion
Induction and recursion
Recursive Algorithms Section 5.4.
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
CSE15 Discrete Mathematics 04/12/17
Induction and Recursion Chapter 5
CS2210:0001Discrete Structures Induction and Recursion
Induction and Recursion
Mathematical Induction Recursion
Discrete Structures for Computer Science
Chapter 5 Induction and Recursion
Applied Discrete Mathematics Week 9: Integer Properties
Chapter 4 Induction and Recursion
Mathematical Induction
Math/CSE 1019: Discrete Mathematics for Computer Science Fall 2011
Induction and recursion
Induction and recursion
Chapter 4 (Part 2): Mathematical Reasoning, Induction & Recursion
Recursion.
Presentation transcript:

Section 5.3 1

Recursion 2

 Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences Previously sequences were defined using a specific formula, e.g., a n = 2 n for n = 0,1,2,... This sequence can also be defined by giving the first term of the sequence, namely a 0 = 1, and a rule for finding a term of the sequence for the previous one, namely, a n+1 = 2 a n for n = 0,1,2,... 3

Recursively Defined Functions  A recursive or inductive definition of a function consists of two steps. BASIS STEP: Specify the value of the function at zero. RECURSIVE STEP: Give a rule for finding its value at an integer from its values at smaller integers.  A function f ( n ) is the same as a sequence a 0, a 1, …, where a i, where f ( i ) = a i. This was done using recurrence relations in Section

Recursively Defined Functions  Example: Suppose f is defined by: f(0) = 3, f(n + 1) = 2f(n) + 3 Find f(1), f(2), f(3), f(4)  Solution: f ( 1 ) = 2 f ( 0 ) + 3 = 2∙3 + 3 = 9 f ( 2 ) = 2 f ( 1 ) + 3 = 2∙9 + 3 = 21 f ( 3 ) = 2 f ( 2 ) + 3 = 2∙ = 45 f ( 4 ) = 2 f ( 3 ) + 3 = 2∙ = 93 5

Fibonacci Numbers  Example : The Fibonacci numbers are defined as follows: f 0 = 0 f 1 = 1 f n = f n−1 + f n−2 Find f 2, f 3, f 4, f 5.  Solution : 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

Recursively Defined Functions  Example: Give a recursive definition of the factorial function n! (n is a nonnegative integer):  Solution: f ( 0 ) = 1 f ( n + 1 ) = ( n + 1 ) ∙ f ( n ) 7

Recursively Defined Functions  Example: Give a recursive definition of:  Solution: The first part of the definition is The second part is 8

Recursively Defined Sets and Structures  Recursive definitions of sets have two parts: basis step: specifies an initial collection of elements. recursive step: gives the rules for forming new elements in the set from those already known to be in the set.  Sometimes the recursive definition has an exclusion rule, which specifies that the set contains nothing other than those elements specified in the basis step and generated by applications of the rules in the recursive step.  assume the exclusion rule holds, even if it is not explicitly mentioned.  structural induction is used to prove results about recursively defined sets. 9

 Example : Subset of Integers S: BASIS STEP : 3 ∊ S. RECURSIVE STEP: If x ∊ S and y ∊ S, then x + y is in S.  Initially 3 is in S, then = 6, then = 9, etc. All positive multiples of 3.  Example: BASIS STEP : 0 ∊ N. RECURSIVE STEP: If n is in N, then n + 1 is in N.  Solution: The natural numbers N. Initially 0 is in S, then = 1, then = 2, etc. Recursively Defined Sets and Structures

Strings  Definition 1: The set Σ* of strings over the alphabet Σ:  BASIS STEP: λ ∊ Σ* (λ is the empty string)  RECURSIVE STEP: If w is in Σ* and x is in Σ, then wx  Σ*.  Example: Σ = {0,1},  the strings in Σ* are the set of all bit strings, λ,0,1, 00,01,10, 11, etc.  Example: If Σ = {a,b}, show that aab is in Σ*. Since λ ∊ Σ* and a ∊ Σ, a ∊ Σ*. Since a ∊ Σ* and a ∊ Σ, aa ∊ Σ*. Since aa ∊ Σ* and b ∊ Σ, aab ∊ Σ*.

Strings  Definition 2:

Balanced Parentheses  Example: Give a recursive definition of the set of balanced parentheses P.  Solution: BASIS STEP : () ∊ P RECURSIVE STEP: If w ∊ P, then () w ∊ P, ( w ) ∊ P and w () ∊ P.  Show that (() ()) is in P.  Why is ))(() not in P ?

Rooted Trees  Definition 3: The set of rooted trees, where a rooted tree consists of a set of vertices containing a distinguished vertex called the root, and edges connecting these vertices, can be defined recursively by these steps: BASIS STEP : A single vertex r is a rooted tree. RECURSIVE STEP : Suppose that T 1, T 2,..., T n are disjoint rooted trees with roots r 1, r 2,..., r n, respectively. Then the graph formed by starting with a root r, which is not in any of the rooted trees T 1, T 2,..., T n, and adding an edge from r to each of the vertices r 1, r 2,..., r n, is also a rooted tree. 14

Full Binary Trees  Definition 5: The set of full binary trees can be defined recursively by these steps. BASIS STEP: There is a full binary tree consisting of only a single vertex r. RECURSIVE STEP: If T 1 and T 2 are disjoint full binary trees, there is a full binary tree, denoted by T 1 ∙ T 2, consisting of a root r together with edges connecting the root to each of the roots of the left subtree T 1 and the right subtree T 2. 15

Structural Induction  structural induction is used to prove a property of the elements of a recursively defined set. BASIS STEP: Show that the result holds for all elements specified in the basis step of the recursive definition. RECURSIVE STEP: Show that if the statement is true for each of the elements used to construct new elements in the recursive step of the definition, the result holds for these new elements.  The validity of structural induction can be shown to follow from the principle of mathematical induction. 16

Full Binary Trees  Definition 6: The height h(T) of a full binary tree T is defined recursively as: BASIS STEP: The height of a full binary tree T consisting of only a root r is h(T) = 0. RECURSIVE STEP: If T 1 and T 2 are full binary trees, then the full binary tree T = T 1 ∙T 2 has height h(T) = 1 + ma x( h(T 1 ),h ( T 2 )).  The number of vertices n(T) of a full binary tree T satisfies the following recursive formula: BASIS STEP: The number of vertices of a full binary tree T consisting of only a root r is n(T) = 1. RECURSIVE STEP: If T1 and T2 are full binary trees, then the full binary tree T = T1∙T2 has the number of vertices n(T) = 1 + n(T1) + n(T2).

Structural Induction and Binary Trees  Theorem 2: If T is a full binary tree, then n(T) ≤ 2h(T)+1 – 1.  Proof: Use structural induction. BASIS STEP: The result holds for a full binary tree consisting only of a root, n ( T ) = 1 and h ( T ) = 0. Hence, n ( T ) = 1 ≤ – 1 = 1. RECURSIVE STEP: Assume n ( T 1 ) ≤ 2 h ( T 1 )+ 1 – 1 and also n ( T 2 ) ≤ 2 h ( T 2 )+ 1 – 1 whenever T 1 and T 2 are full binary trees. n(T) = 1 + n(T 1 ) + n(T 2 ) (by recursive formula of n(T)) ≤ 1 + ( 2 h(T1)+ 1 – 1 ) + ( 2 h(T2)+ 1 – 1 ) (by inductive hypothesis) ≤ 2 ∙max( 2 h(T1)+ 1, 2 h(T2)+ 1 ) – 1 = 2∙2 max(h(T1),h(T2))+ 1 – 1 (max( 2 x, 2 y )= 2 max(x,y) ) = 2∙2 h(t) – 1 (by recursive definition of h(T)) = 2 h(t)+ 1 – 1 −2. −2.

Section

Recursive Algorithms  Definition 1: An algorithm is called recursive if it solves a problem by reducing it to an instance of the same problem with smaller input.  For the algorithm to terminate, the instance of the problem must eventually be reduced to some initial case for which the solution is known. 20

Recursive Factorial Algorithm Example: Give a recursive algorithm for computing n !, where n is a nonnegative integer.  Solution: Use the recursive definition of the factorial function. 21 procedure factorial(n: nonnegative integer) if n = 0 then factorial(n):=1 else factorial(n):=n*factorial(n-1)

Recursive Binary Search Algorithm Example: Construct a recursive version of a binary search algorithm. Solution: Assume we have a1,a2,…, an, an increasing sequence of integers. Initially i is 1 and j is n. We are searching for x. 22 procedure binary search(i, j, x : integers, 1≤ i ≤ j ≤ n ) m := ⌊( i + j )/2⌋ if x = a m then return m else if (x < a m and i < m) then return binary search(i,m −1,x ) else if (x > a m and j >m) then return binary search(m +1,j,x ) else return 0 {output is location of x in a 1, a 2,…, a n if it appears, otherwise 0 }

Proving Recursive Algorithms Correct  Both mathematical and strong induction are useful techniques to show that recursive algorithms always produce the correct output.  Example: Prove that the algorithm for computing the powers of real numbers is correct.  Solution: Use mathematical induction on the exponent n. BASIS STEP: a 0 = 1 for every nonzero real number a, and power ( a, 0 ) = 1. INDUCTIVE STEP: The inductive hypothesis is that power ( a, k ) = a k, for all a ≠0. Assuming the inductive hypothesis, the algorithm correctly computes a k+ 1, since power(a,k + 1) = a∙ power (a, k) = a∙ a k = a k procedure power(a: nonzero real number, n: nonnegative integer) if n = 0 then return 1 else return a ∙ power (a, n − 1 ) {output is a n } −.−.

Merge Sort  Merge Sort works by iteratively splitting a list (with an even number of elements) into two sublists of equal length until each sublist has one element.  Each sublist is represented by a balanced binary tree.  At each step a pair of sublists is successively merged into a list with the elements in increasing order. The process ends when all the sublists have been merged.  The succession of merged lists is represented by a binary tree. 24

Merge Sort Example: Use merge sort to put the list 8,2,4,6,9,7,10, 1, 5, 3 into increasing order. Solution: 25

Recursive Merge Sort Example: Construct a recursive merge sort algorithm. Solution: Begin with the list of n elements L. 26 procedure mergesort(L = a 1, a 2,…, a n ) if n > 1 then m := ⌊n/2⌋ L 1 := a 1, a 2,…, a m L 2 := a m +1, a m +2,…, a n L := merge(mergesort(L 1 ), mergesort(L 2 )) {L is now sorted into elements in increasing order} procedure merge(L 1, L 2 :sorted lists ) L := empty list while L 1 and L 2 are both nonempty remove smaller of first elements of L 1 and L 2 from its list; put at the right end of L if this removal makes one list empty then remove all elements from the other list and append them to L return L {L is the merged list with the elements in increasing order}

Merging Two Lists Example: Merge the two lists 2,3,5,6 and 1,4. Solution: 27