CSE115/ENGR160 Discrete Mathematics 04/05/11 Ming-Hsuan Yang UC Merced 1.

Slides:



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

22C:19 Discrete Structures Induction and Recursion Spring 2014 Sukumar Ghosh.
22C:19 Discrete Structures Induction and Recursion Fall 2014 Sukumar Ghosh.
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.
UCI ICS/Math 6A, Summer Recursion -1 after Strong Induction “Normal” Induction “Normal” Induction: If we prove.
Discrete Structures & Algorithms More on Methods of Proof / Mathematical Induction EECE 320 — UBC.
CSE115/ENGR160 Discrete Mathematics 04/19/12 Ming-Hsuan Yang UC Merced 1.
CSE115/ENGR160 Discrete Mathematics 04/21/11 Ming-Hsuan Yang UC Merced 1.
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/12/11 Ming-Hsuan Yang UC Merced 1.
CSE115/ENGR160 Discrete Mathematics 02/03/11 Ming-Hsuan Yang UC Merced 1.
CSE115/ENGR160 Discrete Mathematics 02/24/11 Ming-Hsuan Yang UC Merced 1.
Algorithm Design Techniques: Induction Chapter 5 (Except Sections 5.6 and 5.7)
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
CSE115/ENGR160 Discrete Mathematics 03/22/12 Ming-Hsuan Yang UC Merced 1.
1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such.
CSE115/ENGR160 Discrete Mathematics 03/10/11
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.
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)
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.
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,
Induction and recursion
4.4 Recursive Algorithms A recursive algorithm is one which calls itself to solve “smaller” versions of an input problem. How it works: – The current status.
CP104 Introduction to Programming Recursion 2 Lecture 29 __ 1 Recursive Function for gcd Recursive formula for the greatest common divisor of m and n,
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.
Recursive Algorithms &
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.
7.3 Divide-and-Conquer Algorithms and Recurrence Relations If f(n) represents the number of operations required to solve the problem of size n, it follow.
Application: Algorithms Lecture 20 Section 3.8 Wed, Feb 21, 2007.
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.
Application: Algorithms Lecture 19 Section 3.8 Tue, Feb 20, 2007.
Chapter 5 Kenneth Rosen, Discrete Mathematics and its Applications, 7th edition, McGraw Hill Instructor: Longin Jan Latecki, Copyright.
Induction and Recursion CSC-2259 Discrete Structures Konstantin Busch - LSU1.
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.
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
CPSC 121: Models of Computation 2008/9 Winter Term 2
CS2210:0001Discrete Structures Induction and Recursion
Induction and Recursion
Recursively Defined Functions
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett
Mathematical Induction Recursion
Chapter 5 Induction and Recursion
Quiz 6 The way I expected it..
Applied Discrete Mathematics Week 9: Integer Properties
Chapter 4 Induction and Recursion
Application: Algorithms
Application: Algorithms
Induction and recursion
Recursion.
Presentation transcript:

CSE115/ENGR160 Discrete Mathematics 04/05/11 Ming-Hsuan Yang UC Merced 1

4.4 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 Give a recursive algorithm for computing n! where n is a non-negative integer We can compute n!=n ∙(n-1)! Where n is a positive integer, and that 0!=1 for a particular integer We use the recursive step n times 2

Recursive algorithm for n! procedure factorial(n: non-negative integer) if n=0 then factorial(n):=1 else factorial(n):=n ∙ factorial(n-1) 3

Recursive algorithm for a n procedure power(a: nonzero real number, n: non-negative integer) if n=0 then power(a,n):=1 else power(a,n):=a ∙ power(a, n-1) 4

Example Formulate a recursive algorithm for computing b n mod m, where b, n and m are integers with m≥2, n≥0, 1≤b<m Let m be a positive integer and let a and b be integers, then (a+b) mod m=((a mod m) + (b mod m)) mod m ab mod m = ((a mod m)(b mod m)) mod m Thus, b n mod m = (b (b n-1 mod m)) mod m, and b 0 mod m = 1 5

Example However, we can have a more efficient recursive algorithm procedure mpower(b, n, m: integers with m≥2, n≥0) if n=0 then mpower(b, n, m)=1 else if n is even then mpower(b, n, m)=mpower(b, n/2, m) 2 mod m else mpower(b, n, m)=mpower(b, ⌞ n/2 ⌟, m) 2 mod m ∙ b mod m) mod m {mpower(b, n, m)=b n mod m} 6

Example procedure mpower(b, n, m: integers with m≥2, n≥0) if n=0 then mpower(b, n, m)=1 else if n is even then mpower(b, n, m)=mpower(b, n/2, m) 2 mod m else mpower(b, n, m)=mpower(b, ⌞ n/2 ⌟, m) 2 mod m ∙ b mod m) mod m {mpower(b, n, m)=b n mod m} Trace the above algorithm with b=2, n=5, and m=3 Since n is odd, we have mpower(2, 5, 3)=(mpower(2,2,3) 2 mod 3 ∙ 2 mod 3) mod 3 Next, mpower(2,2,3)=mpower(2,1,3) 2 mod 3, and mpower(2,1,3)= (mpower(2,0,3) 2 mod 3 ∙ 2 mod 3) mod 3, and mpower(2,0,3)=1 So, mpower(2,1,3)=(1 2 mod 3 ∙ 2 mod 3) mod 3 = 2 So, power(2, 2, 3)=2 2 mod 3 =1 and finally mpower(2, 5, 3)=(1 2 mod 3 ∙ 2 mod 3) mod 3 =2 7

Example Give a recursive algorithm for gcd(a, b) with a < b A recursive version of Euclidean algorithm procedure gcd(a, b: non-negative integers with a < b) if a=0 then gcd(a, b):=b else gcd(a, b):=gcd(b mod a, a) Let a=5 and b=8. gcd(5, 8)=gcd(8 mod 5, 5)=gcd(3, 5) Next, gcd(3, 5)=gcd(5 mod 3, 3)=gcd(2, 3), then gcd(2, 3)=gcd (3 mod 2, 2)=gcd(1, 2). Finally gcd(1,2)=gcd(2 mod 1, 2) = gcd(0, 1)=1. Thus, gcd(5,8)=1 8

Binary search algorithm procedure binary_search(i, j, x: integers, 1≤i≤n, 1 ≤j≤n) m= ⌞(i+j)/2⌟ if x=a m then location:=m else if (x < a m and i < m) then binary_search(x, i, m-1) else if (x > a m and j > m) then binary_search(x, m+1, j) else location:=0 9

Proving recursive algorithm Prove that the recursive algorithm power(a, n) is correct procedure power(a: nonzero real number, n: non-negative integer) if n=0 then power(a,n):=1 else power(a,n):=a ∙ power(a, n-1) Basis step: If n=0, power(a,0)=1. This is correct as a 0 =1 10

Proving recursive algorithm Inductive step: The inductive hypothesis is power(a,k)=a k for a≠0 and non-negative k. To complete the proof, we need to show power(a,k+1)= a k+1 As k+1 is a positive integer, the algorithms sets power(a, k+1)=a power(a, k)=a ∙ a k = a k+1 This completes the inductive step 11

Recursion and iteration Often an iterative algorithm is more efficient than a recursive one (unless special-purpose machines are used) procedure fibonacci(n: nonzero integer) if n=0 then fibonacci(n):=0 else if n=1 then fibonacci(1):=1 else fibonacci(n)=fibonacci(n-1)+fibonacci(n- 2) 12

Recursion and iteration fibonacci(4) 13

Iterative algorithm for Fibonacci Number procedure iterative_fibonacci(n: nonzero integer) if n=0 then y:=0 else begin x:=0 y:=1 for i:=1 to n-1 z:=x+y x:=y y:=z end {y is the n-th Fibonacci number} 14

Recursion and iteration The above algorithm initializes x as f 0 =0, and y as f 1 =1 When the loop is traversed, the sum of x and y is assigned to the auxiliary variable z Then x is assigned the value of y and y is assigned the value of the auxiliary variable z Thus, after going through the loop the first time, it follows x equals f 1 and y equals f 0 +f 1 =f 2 Next, f 1 +f 2 =f 3, … After going through the algorithm n-1 times, x equals f n-1 and y equals f n Only n-1 additions have been used to find f n 15

Example: Sort the list 8, 2, 4, 6, 9, 7, 10, 1, 5, 3 Merge sort: split the list into individual elements by successively splitting lists into two Sorting is done by successively merging pairs of lists 16 Merge sort

In general, a merge sort proceeds by iteratively splitting lists into two sbulists We can also describe the merge sort recursively procedure mergesort(L=a 1, …, a n ) if n>1 then m:= ⌞n/2⌟ L 1 =a 1, a 2, …, a m L 2 =a m+1, …, a n L=merge(mergesort(L 1 ), mergesort(L 2 )) {L is a sorted list in non-decreasing order} 17

Merge sort Merge two lists, 2, 3, 5, 6, and 1, 4 18

Merge sort procedure merge(L 1, L 2 : sorted lists) L:=empty set while L 1 and L 2 are both non-empty begin remove smaller of first element of L 1 and L 2 from the list it is in and put it at the right end of L if removal of this element makes one list empty then remove all elements from the other list and append them to L end {L is a sorted list in non-decreasing order} 19

Merge sort Two sorted lists with m elements and n elements can be merged into a sorted list using no more than m+n-1 comparisons 20