Kavita Math231 Recursion and Iteration. Kavita Math231 We use Recursion when we have to perform a complex task that can be broken into the several subtasks.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms II
Advertisements

Week 6 - Wednesday CS322.
Recursion Lecture 18: Nov 18.
Discrete Structures Chapter 6 Recurrence Relations
1 Chapter 1: Introduction What you have learnt in Comp1220 or Comp1170? What will be taught in Comp 1200? - more Abstract Data Types -efficient algorithms.
Kavita Hatwal Fall Sequences and Induction.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Recursion.
1 Section 6.1 Recurrence Relations. 2 Recursive definition of a sequence Specify one or more initial terms Specify rule for obtaining subsequent terms.
Copyright © Cengage Learning. All rights reserved. CHAPTER 5 SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION.
Recursion Lecture 17: Nov 11. Quiz int hello(int n) { if (n==0) return 0; else printf(“Hello World %d\n”,n); hello(n-1); } 1.What would the program do.
Induction and recursion
For a geometric sequence, , for every positive integer k.
Analysis of Recursive Algorithms October 29, 2014
Copyright © Cengage Learning. All rights reserved.
Copyright © Cengage Learning. All rights reserved. CHAPTER 11 ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS OF ALGORITHM EFFICIENCY.
Recursively Defined Sequences Lecture 35 Section 8.1 Wed, Mar 23, 2005.
Applied Discrete Mathematics Week 9: Relations
Advanced Counting Techniques
1 © 2010 Pearson Education, Inc. All rights reserved 10.1 DEFINITION OF A SEQUENCE An infinite sequence is a function whose domain is the set of positive.
Number Sequences Lecture 7: Sep 29 ? overhang. This Lecture We will study some simple number sequences and their properties. The topics include: Representation.
Sequences Informally, a sequence is a set of elements written in a row. – This concept is represented in CS using one- dimensional arrays The goal of mathematics.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Chapter 8. Section 8. 1 Section Summary Introduction Modeling with Recurrence Relations Fibonacci Numbers The Tower of Hanoi Counting Problems Algorithms.
The importance of sequences and infinite series in calculus stems from Newton’s idea of representing functions as sums of infinite series.  For instance,
Chapter 6 Sequences And Series Look at these number sequences carefully can you guess the next 2 numbers? What about guess the rule?
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.
13.2 Recursive Definitions Objective 1) Provide the recursive definition for sequences; 2) Identify the type of a sequence from a recursive definition.
Section 2.4. Section Summary Sequences. Examples: Geometric Progression, Arithmetic Progression Recurrence Relations Example: Fibonacci Sequence Summations.
Data Structures and Algorithms Discrete Math Review.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Lecture#16 Discrete Mathematics. Recursion Now, 1 is an odd positive integer by the definition base. With k = 1, = 3, so 3 is an odd positive integer.
RECURRENCE Sequence Recursively defined sequence
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Sequences and Summations
Section 2.4. Section Summary Sequences. Examples: Geometric Progression, Arithmetic Progression Recurrence Relations Example: Fibonacci Sequence Summations.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
CompSci 102 Discrete Math for Computer Science January 31, 2012 Prof. Rodger Slides modified from Rosen AB a b c d x y z.
Sequences and Summations Section 2.4. Section Summary Sequences. – Examples: Geometric Progression, Arithmetic Progression Recurrence Relations – Example:
Functions Section 2.3. Section Summary Definition of a Function. – Domain, Cdomain – Image, Preimage Injection, Surjection, Bijection Inverse Function.
In this lesson you will learn another way to define a sequence — by a recursive rule. So far you have worked with explicit rules for the n th term of a.
1 Topics Recursion sections 8.1 – Recursion A recursively defined sequence –First, certain initial values are specified –Later terms of the sequence.
Lecture - 8 On Stacks, Recursion. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline Quick sort Algorithm Recursion –Calculate n factorial –Fibonacci.
Discrete Mathematics Recurrence Relations Chapter 5 R. Johnsonbaugh
Sequences and Series (Section 9.4 in Textbook).
Section 11.1 Sequences. Sequence – list of values following a pattern Arithmetic – from term to term there is a common difference we’ll call d Geometric.
Foundations of Discrete Mathematics Chapters 5 By Dr. Dalia M. Gil, Ph.D.
Solving Recurrence Relations by Iteration Lecture 36 Section 8.2 Mon, Apr 17, 2006.
RECURRENCE Sequence Recursively defined sequence
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
Discrete Mathematics Lecture # 22 Recursion.  First of all instead of giving the definition of Recursion we give you an example, you already know the.
1 RECURRENCE 1. Sequence 2. Recursively defined sequence 3. Finding an explicit formula for recurrence relation.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Chapter 2 1. Chapter Summary Sets The Language of Sets - Sec 2.1 – Lecture 8 Set Operations and Set Identities - Sec 2.2 – Lecture 9 Functions and sequences.
Recursively Defined Sequences Lecture 40 Section 8.1 Wed, Apr 11, 2007.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
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.
Sequences Lecture 11. L62 Sequences Sequences are a way of ordering lists of objects. Java arrays are a type of sequence of finite size. Usually, mathematical.
Fall 2002CMSC Discrete Structures1 Chapter 3 Sequences Mathematical Induction Recursion Recursion.
Recursion.
Chapter 15 Recursion.
Chapter 15 Recursion.
Mathematical Induction Recursion
Algorithm Analysis (for Divide-and-Conquer problems)
Algorithm design and Analysis
Copyright © Cengage Learning. All rights reserved.
Mathematical Induction
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Recursively Defined Sequences
Presentation transcript:

Kavita Math231 Recursion and Iteration

Kavita Math231 We use Recursion when we have to perform a complex task that can be broken into the several subtasks. Recursion is implemented as a method that calls itself to solve subtasks. It often appears in functional-style definitions or divide-and-conquer explanations of an algorithm. T o understand recursion, we need to understand recurrence A recurrence relation for a sequence a 0,a 1,a 2,…is a formula that relates each term a k to certain of it predecessors a k-1, a k-2,…, a k-i where i is a fixed integer and k is any integer greater than or equal to I. The initial conditions for recurrence relation specify the values of a 0,a 1,a 2,…a i-1. For example a 0,a 1,a 2,…can be specified as. The advantage of defining a sequence by such an explicit formula is that each term of the sequence is uniquely determined and can be computed in a fixed, finite number of steps. Calculate a 0 and a 1. Another way of defining the terms of a sequence is recursion which requires an equation called recurrence relation that relates later terms in the sequence to earlier terms and a specification, called initial conditions. For example a sequence b 0,b 1,b 2 can be defined recursively as follows recurrence relation initial conditionCompute b 2,b 3,b 4

Kavita Math231 A function is said to be recursively defined if the function definition refers to itself. In order for the definition to be not circular, it must have the following two properties: 1.There must be certain arguments,called the base values, for which the function does not refer to itself. 2.Each time the function does refer to itself, the argument of the function must be closer to a base value. A recursive function with these two properties is also said to be well defined. Examples: Factorial(n) a.IF n is 0 Return 1 ELSE Return n* Factorial(n-1)

Kavita Math231 Factorial(4) N4N4 N3N3 N2N2 N1N1 N0N0

Kavita Math231 exponential (POWER) function X N POWER(x,n) IF n=1 return x ELSE return x* POWER(x,n - 1) POWER(2,3) x n 2 3 x n 2 x n 2 1

Kavita Math231 Towers of Hanoi: When the game begins, we’ve three pegs and all the circles are on the first peg in order by size, the biggest on the bottom and smallest on top. Object of the game: Is to move the circles one at a time to the third peg, the catch being that the order of size has to be maintained. The middle peg can be used as a helper, a go between but should be empty at the beginning and at the end of the game. And the algorithm for such a fancy schmancy game is: Get N circles moved from peg 1 to peg 3 Get n-1 circles removed from peg 1 and placed in peg 2 Move n’th circle from peg 1 to peg 3 Get n-1 circles moved from peg 2 to peg 3

Kavita Math231 the FIBONACCI sequence: Fib(N) = Fib(N-1) + Fib(N-2) for n, and Fib(0) = Fib(1) = 1 1.If n=0 or n =1, then F n = 1. 2.If n > 1, then F n = F n-2 + F n-1 example: Fib(10) = 1,1,2,3,5,8,13,21,33, Compound interest: Amount accumulated at Compound Interest on P at rate r over n intervals Amt.C.I(P, r, n) 1.IF n = 1, A 0 = P(1+ r) 2.IF n > 1, A k = A k-1 (1+ r)

Kavita Math231 Example: Counting Strings Addition rules Let  = {0, 1}. Let a k be the number of strings in  * of length k that do not contain 11. –a 0 = 1, –a 1 = 2, –a 2 = 3.

Kavita Math231 Example: Counting Strings Consider strings of length k, for some k  2, that do not contain 11. If the first character is 0, then the remainder of the string is a string of length k – 1 which does not contain 11. If the first character is 1, then the next character must be 0 and the remainder is a string that does not contain 11.

Kavita Math231 Example: Counting Strings Therefore, –a k = a k – 1 + a k – 2, for all k  2. The first few terms –a 3 = a 2 + a 1 = 5, –a 4 = a 3 + a 2 = 8, –a 5 = a 4 + a 3 = 13. k = 3: {000, 001, 010, 100, 101}

Kavita Math231 Example: Counting r-Partitions An r-partition of a set is a partition of the set into r nonempty subsets. Let A be a set of size n. Let a n, r be the number of distinct r-partitions of A. Special cases –a n, n = 1 for all n  1. –a n, 1 = 1 for all n  1.

Kavita Math231 Example: Counting r-Partitions Let A = {a, b, c, d}. a 4, 2 = 7 since the 2-partitions are –{{a}, {b, c, d}} –{{b}, {a, c, d}} –{{c}, {a, b, d}} –{{d}, {a, b, c}} –{{a, b}, {c, d}} –{{a, c}, {b, d}} –{{a, d}, {b, c}}

Kavita Math231 Example: Counting r-Partitions Consider an r-partition of a set A. Let x  A. Either x is in a set {x} by itself or it isn’t. If it is, then the remaining sets form an (r – 1)-partition of A – {x}. If it isn’t, then if we remove x, we have an r-partition of A – {x}.

Kavita Math231 Example: Counting r-Partitions In fact, we get the same r-partition of A – {x} that we would get had x been a member of any other set in the partition. Thus, each r-partition of A – {x} gives rise to r r-partitions of A. Therefore, a n, r = a n – 1, r – 1 + r  a n – 1, r, for all n  1 and for all r, 1 < r < n.

Kavita Math231 Example: Counting r-Partitions Compute a 4, 2. and a 5, 2. –a 4, 2 = a 3, 1 + 2a 3, 2 = 1 + 2(a 2, 1 + 2a 2, 2 ) = 1 + 2(1 + 2  1) = 7. –a 5, 2 = a 4, 1 + 2a 4, 2 =  7 = 15.

Kavita Math231 Section 8.2 Solving Recurrence Relations by Iteration

Kavita Math231 Guessing the Answer Write out the first several terms. Look for a pattern. Two strategies –Do the arithmetic. –Postpone the arithmetic.

Kavita Math231 Example: Do the Arithmetic Define {a n } by –a 1 = 2, –a n = 2a n – 1 – 1, for all n  2. Find a formula for a n. First few terms: 2, 3, 5, 9, 17, 33, 65. Compare to: 1, 2, 4, 8, 16, 32, 64. Guess that a n = 2 n –

Kavita Math231 Example: Postpone the Arithmetic Define {a n } by –a 1 = 1, –a n = 5a n – 1 + 2, for all n  2. Find a formula for a n. First few terms: 1, 7, 37, 187, 937. What is a n ?

Kavita Math231 Example: Postpone the Arithmetic Calculate a few terms –a 1 = 1. –a 2 = 5  –a 3 = 5 2   –a 4 = 5 3    –a 5 = 5 4     It appears that, in general, –a n = 5 n – 1 + (5 n – n – 3 + … + 1)  2.

Kavita Math231 Arithmetic sequence A sequence in which each term is obtained by adding a fixed factor to the preceding term. Example if Geometric sequence A sequence in which each term is obtained by multiplying a fixed factor to the preceding term. Example if Sum of geometric sequence is given as Page 442, Page 477 example 8.2.2, 8.2.3, 8.2.4, Page 452, Page c, 2-b,c, 4, 17

Kavita Math231 Example: Postpone the Arithmetic a n = 5 n – 1 + (5 n – n – 3 + … + 1)  2 = 5 n – 1 + (5 n – 1 – 1)/(5 – 1)  2 = 5 n – 1 + (5 n – 1 – 1)/2 = (3  5 n – 1 – 1)/2.

Kavita Math231 Example: Future Value of an Annuity Define {a n } by –a 0 = d, –a n = (1 + r)a n – 1 + d, for all n  1. Find a formula for a n. –a 1 = (1 + r)d + d. –a 2 = (1 + r) 2 d + (1 + r)d + d. –a 3 = (1 + r) 3 d + (1 + r) 2 d + (1 + r)d + d.

Kavita Math231 Example: Future Value of an Annuity It appears that, in general, a n = (1 + r) n d + … + (1 + r)d + d = d((1 + r) n + 1 – 1)/((1 + r) – 1) = d((1 + r) n + 1 – 1)/r.

Kavita Math231 Verifying the Guess Use mathematical induction to verify the guess.

Kavita Math231 Verify the Guess Define {a n } by –a 1 = 1, –a n = 5a n – 1 + 2, for all n  2. Verify, by induction, the formula a n = (3  5 n – 1 – 1)/2.

Kavita Math231 Verify the Guess Basic Step (n = 1) –a 1 = 1 –(3  5 1 – 1 – 1)/2 = (3  5 0 – 1)/2 = (3 – 1)/2 = 1.

Kavita Math231 Verify the Guess Inductive Step –Suppose a k = (3  5 k – 1 – 1)/2 for some k  1. –Then a k + 1 = 5[(3  5 k – 1 – 1)/2] + 2 = (3  5 k – 5)/2 + 2 = (3  5 k – 1)/2. Therefore, it is true for all n  1.

Kavita Math231 Page 438, Page 472 #2, 4, 6 Examples of recursively defined sequences. To solve a problem recursively, find a way to break it down into smaller sub problems each having the same sub form as the original problem. Also put a terminating condition called the base condition. Have you seen this methodology anywhere before? Page 438, Page 472 #17, 22,(24), 28, (23), 29, (34),

Kavita Math231 ITERATION Suppose I want to print my name backwards. How will I do it recursively? Is there any other way I can achieve the same objective? For that I need to have a knowledge of another method called the iterative method. The iterative method converts the recurrence into a summation within some limits called bounds. Given a sequence a 0,a 1,a 2,… defined by a recursive relation and initial condition, you start from the initial conditions and calculate successive terms till you see a pattern developing. At that point you guess an explicit formula. Page 442, example You can check the correctness of the formula that you’ve derived by mathematical induction.

Kavita Math231 Recursion and iteration are different ways to cause the evaluation of an expression to re-occur as needed. Recursion often appears in functional-style definitions or divide-and-conqure explanations of an algorithm. Programming with recursion involves the idea of "vertical" self-reference, as in a data structure that has substructures similar to itself, or a function which invokes itself. In contrast, iteration simply involves the notion of "horizontal" repetition of some concept, such as an expression to be evaluated repeatedly or a data structure which contains a sequence of identically-structured components. In theory, recursion and iteration are equivalent; anything we can do with one, we can do with the other. That may be true, but each is the best tool for some purposes.