Recursion Rosen 5 th ed., §3.4-3.5. Recursion Sometimes, defining an object explicitly might be difficult.Sometimes, defining an object explicitly might.

Slides:



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

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.
Recursion. Binary search example postponed to end of lecture.
Recursion CS 308 – Data Structures. What is recursion? smaller version Sometimes, the best way to solve a problem is by solving a smaller version of the.
Programming with Recursion
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Recursion.
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.
Recursion. Recursive Definitions In recursive definitions, we define a function, a predicate, a set, or a more complex structure over an infinite domain.
Module #1 - Logic 1 Based on Rosen, Discrete Mathematics & Its Applications. Prepared by (c) , Michael P. Frank. Modified By Mingwu Chen Induction.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Chapter 3: Recursive Algorithms
Recursion CS Goals Discuss recursion as another form of repetition Do the following tasks, given a recursive routine Determine whether the routine.
CS Discrete Mathematical Structures Mehdi Ghayoumi MSB rm 132 Ofc hr: Thur, 9:30-11:30a.
22C:19 Discrete Math Induction and Recursion Fall 2011 Sukumar Ghosh.
Induction and recursion
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
1 Storage Classes, Scope, and Recursion Lecture 6.
12-CRS-0106 REVISED 8 FEB 2013 KUG1C3 Dasar Algoritma dan Pemrograman.
Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
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,
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.
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
Recursion CS 3358 – Data Structures. Big Picture Our objective is to write a recursive program and convince ourselves it is correct with the minimum amount.
CIS 068 Welcome to CIS 068 ! Stacks and Recursion.
Computer Science and Software Engineering University of Wisconsin - Platteville 9. Recursion Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++
Recursion CS 302 – Data Structures Chapter 7. What is recursion? smaller A technique that solves problem by solving smaller versions of the same problem!
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
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.
Recursion.
Recurrences David Kauchak cs161 Summer Administrative Algorithms graded on efficiency! Be specific about the run times (e.g. log bases) Reminder:
Recursive Algorithms A recursive algorithm calls itself to do part of its work The “call to itself” must be on a smaller problem than the one originally.
1 Lecture 3 Part 2 Storage Classes, Scope, and Recursion.
Chapter 7 Programming with Recursion. What Is Recursion? Recursive call A method call in which the method being called is the same as the one.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
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.
Recursion. What is recursion? smaller version Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first.
Lecture #3 Analysis of Recursive Algorithms
12-CRS-0106 REVISED 8 FEB 2013 KUG1C3 Dasar Algoritma dan Pemrograman.
Chapter 6 (Lafore’s Book) Recursion Hwajung Lee.  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:
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.
Data Structures I (CPCS-204) Week # 5: Recursion Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
递归算法的效率分析. When a function is called... A transfer of control occurs from the calling block to the code of the function --It is necessary that there be.
Recursion Powerful Tool
Programming with Recursion
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
Computer Science 4 Mr. Gerb
CS 3343: Analysis of Algorithms
CS2210:0001Discrete Structures Induction and Recursion
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
CS 3343: Analysis of Algorithms
Rosen 5th ed., §§ ~18 slides, ~1 lecture
Recursion Data Structures.
Applied Discrete Mathematics Week 9: Integer Properties
Rosen 5th ed., §§ ~18 slides, ~1 lecture
Recursion.
Discrete Mathematics and its Applications
ITEC324 Principle of CS III
Discrete Mathematics and its Applications
Recursion.
Presentation transcript:

Recursion Rosen 5 th ed., §

Recursion Sometimes, defining an object explicitly might be difficult.Sometimes, defining an object explicitly might be difficult. Recursion is a process of defining an object in terms of itself (or of part of itself).Recursion is a process of defining an object in terms of itself (or of part of itself). Recursion can be used to define:Recursion can be used to define: –Functions –Sequences –Sets –Problems –Algorithms

Recursion vs Induction Using recursion, we define an object (e.g., a function, a predicate or a set) over an infinite number of elements by defining large size objects in terms of smaller size ones.Using recursion, we define an object (e.g., a function, a predicate or a set) over an infinite number of elements by defining large size objects in terms of smaller size ones. In induction, we prove all members of an infinite set have some predicate P by proving the truth for large size objects in terms of smaller size ones.In induction, we prove all members of an infinite set have some predicate P by proving the truth for large size objects in terms of smaller size ones.

Recursively Defined Functions One way to define a function f:N  S (for any set S) or series a n =f(n) is to: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:E.g.: Define the series a n :≡ 2 n recursively: –Let a 0 :≡ 1. –For n>0, let a n :≡ 2a n-1.

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−2The 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−2 Leonardo Fibonacci

Use Recursive Definition to Prove Various Properties Theorem: f n < 2 n.Theorem: f n < 2 n. Proof: By induction.Proof: By induction. Base cases:f 0 = 0 < 2 0 = 1 f 1 = 1 < 2 1 = 2 Inductive step: Use 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: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?Example: Let 3  S, and let x+y  S if x,y  S. What is S?

Recursive Problems and Algorithms (§3.5) Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first.Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first. Recursion is a technique that solves a problem by solving a smaller problem of the same type.Recursion is a technique that solves a problem by solving a smaller problem of the same type.

There are many problems whose solution can be defined recursively:There are many problems whose solution can be defined recursively: Example: n factorial Example: n factorial Problems defined recursively 1if n = 0 n!= (recursive solution) (n-1)!*nif n > 0 1if n = 0 n!= (closed form solution) 1*2*3*…*(n-1)*nif n > 0

Coding the factorial function Recursive implementationRecursive implementation int Factorial(int n) { if (n==0) // base case if (n==0) // base case return 1; return 1; else else return n * Factorial(n-1); return n * Factorial(n-1);}

Another example: n choose k (combinations) Given n things, how many different sets of size k can be chosen?Given n things, how many different sets of size k can be chosen? n n-1 n-1 = +, 1 < k < n(recursive solution) = +, 1 < k < n(recursive solution) k k k-1 n n! =, 1 < k < n(closed-form solution) =, 1 < k < n(closed-form solution) k k!(n-k)! with base cases: n n = n (k = 1), = 1 (k = n) = n (k = 1), = 1 (k = n) 1 n 1 n

int Combinations(int n, int k) { if(k == 1) // base case 1 if(k == 1) // base case 1 return n; return n; else if (n == k) // base case 2 else if (n == k) // base case 2 return 1; return 1; else else return(Combinations(n-1, k) + Combinations(n-1, k-1)); return(Combinations(n-1, k) + Combinations(n-1, k-1));} n choose k (combinations)

How do I write a recursive function? Determine the size factorDetermine the size factor Determine the base case(s)Determine the base case(s) (the one(s) for which you know the answer) Determine the general case(s)Determine the general case(s) (the one(s) where the problem is expressed as a smaller version of itself) Verify the algorithmVerify the algorithm (use the "Three-Question-Method")

Three-Question Verification Method 1.The Base-Case Question: Is there a non-recursive way out of the function, and does the routine work correctly for this "base" case? Is there a non-recursive way out of the function, and does the routine work correctly for this "base" case? 2.The Smaller-Caller Question: Does each recursive call to the function involve a smaller case of the original problem, leading inescapably to the base case? Does each recursive call to the function involve a smaller case of the original problem, leading inescapably to the base case? 3.The General-Case Question: Assuming that the recursive call(s) work correctly, does the whole function work correctly?

Compute a n recursively Some algorithms are intuitively iterative, however, they could be written recursively too!Some algorithms are intuitively iterative, however, they could be written recursively too! procedure power(a≠0: real, n  N) if n = 0 then return 1 else return a · power(a, n−1)

Recursive Linear Search Another exampleAnother example procedure search(i, j, x) if =x then location:= i else if i=j then location:=0 else search(i+1, j, x) else search(i+1, j, x).... i j

What is the size factor?What is the size factor? The number of elements in (info[first]... info[last]) What is the base case(s)?What is the base case(s)? (1) If first > last, return false (1) If first > last, return false (2) If item==info[midPoint], return true (2) If item==info[midPoint], return true What is the general case?What is the general case? if item < info[midPoint] search the first half if item > info[midPoint], search the second half Recursive binary search

bool BinarySearch(A[], item, first, last) { int midPoint; int midPoint; if(first > last) // base case 1 if(first > last) // base case 1 return false; return false; else { else { midPoint = (first + last)/2; midPoint = (first + last)/2; if(item < A[midPoint]) if(item < A[midPoint]) return BinarySearch(A, item, first, midPoint-1); return BinarySearch(A, item, first, midPoint-1); else if (item == A[midPoint]) { // base case 2 else if (item == A[midPoint]) { // base case 2 item = A[midPoint]; item = A[midPoint]; return true; return true; } else else return BinarySearch(A, item, midPoint+1, last); return BinarySearch(A, item, midPoint+1, last); }} Recursive binary search

Divide and Conquer

MergeSort

MergeSort (cont’d)

The merge takes Θ(n) steps, and merge-sort takes Θ(n log n).The merge takes Θ(n) steps, and merge-sort takes Θ(n log n).

Efficiency of Recursive Algorithms The time and space complexity of a recursive algorithm depend critically on the number of recursive calls it makes.The time and space complexity of a recursive algorithm depend critically on the number of recursive calls it makes. What happens when a function gets called?What happens when a function gets called? int f(int x) { int y; int y; if(x==0) if(x==0) return 1; return 1; else { else { y = 2 * f(x-1); y = 2 * f(x-1); return y+1; return y+1; }}

=f(3) =f(2) =f(1) 2*f(2) 2*f(1) =f(0)

Recursion vs. Iteration Iteration can be used in place of recursionIteration can be used in place of recursion –An iterative algorithm uses a looping construct –A recursive algorithm uses a branching structure Recursive solutions are often less efficient, in terms of both time and space, than iterative solutionsRecursive solutions are often less efficient, in terms of both time and space, than iterative solutions Recursion can simplify the solution of a problem, often resulting in shorter, more easily understood source codeRecursion can simplify the solution of a problem, often resulting in shorter, more easily understood source code

Recursion can be very inefficient is some cases 15

Deciding whether to use a recursive solution When the depth of recursive calls is relatively "shallow"When the depth of recursive calls is relatively "shallow" The recursive version does about the same amount of work as the non-recursive versionThe recursive version does about the same amount of work as the non-recursive version The recursive version is shorter and simpler than the nonrecursive solutionThe recursive version is shorter and simpler than the nonrecursive solution