   # Recursion

Good Essays
Topics: Infinite loop
Algorithm
Finite description of steps for solving problem Problem types
Satisfying ⇒ find any legal solution Optimization ⇒ find best solution (vs. cost metric)

Approaches
Iterative Recursive ⇒ execute action in loop ⇒ reapply action to subproblem(s)

Recursive Algorithm
Definition
An algorithm that calls itself

Approach
1. Solve small problem directly 2. Simplify large problem into 1 or more smaller

subproblem(s) & solve recursively 3. Calculate solution from solution(s) for subproblem

Algorithm Format
1. Base case Solve small problem directly 2. Recursive step Simplify problem into smaller subproblem(s) Recursively apply algorithm to subproblem(s) Calculate overall solution

Example – Find
To find an element in an array
Base case If array is empty, return false Recursive step If 1st element of array is given value, return true Skip 1st element and recur on remainder of array Example (See ArrayExamples.java)

Example – Count
To count # of elements in an array
Base case If array is empty, return 0 Recursive step Skip 1st element and recur on remainder of array Add 1 to result

Example – Factorial
Factorial definition n! = n × n-1 × n-2 × n-3 × … × 3 × 2 × 1 0! = 1

To calculate factorial of n
Base case If n = 0, return 1 Recursive step Calculate the factorial of n-1 Return n × (the factorial of n-1)

Example – Factorial
Code
int fact ( int n ) { if ( n == 0 ) return 1; return n * fact(n-1); }

// base case // recursive step

Requirements
Must have
Small version of problem solvable without recursion Strategy to simplify problem into 1 or more smaller subproblems Ability to calculate overall solution from solution(s) to subproblem(s)

Making Recursion Work
Designing a correct recursive algorithm Verify
1. Base case is

Recognized correctly Solved correctly 2. Recursive case Solves 1 or more simpler subproblems Can calculate solution from solution(s) to subproblems

Uses principle of proof by induction

## You May Also Find These Documents Helpful

• Powerful Essays

The Property of Recursion in Birdsong and Human Language Introduction: Human speech and birdsong have numerous parallels. Both humans and songbirds learn their complex vocalizations early in life, exhibiting a strong dependence on hearing the adults they will imitate, as well as themselves as they practice, and a waning of this dependence as they mature. Innate predispositions for perceiving and learning the correct sounds exist in both groups, although more evidence of innate descriptions of…

• 4946 Words
• 16 Pages
Powerful Essays
• Satisfactory Essays

and primitive recursion Composition replaces the arguments of a function by another. For example, one can define a function f by F(x,y)=g(h1(x,y), h2(x,y)) Where one supplies the functions g1, g2 and h. Primitive recursion A typical use of primitive recursion has the following form Where one supplies the function g1, g2 and h. For example, in the case of addition, the h is the successor function of the projection of the 2nd argument. A special case of primitive recursion is for some…

• 698 Words
• 3 Pages
Satisfactory Essays
• Better Essays

Pramuka Ehagama CSCi 372 Assignment 01 1. What are the different Language Evaluation Criteria? A language evaluation criterion is broadly used to measure how usable a programming language is. This includes four main criteria and many characteristics alongside. Readability: defines the ease of understandability of a programming language. Include many factors such as below. * Simplicity – strongly affects readability. A language with few basic constructs is much easier to learn. Feature…

• 1672 Words
• 7 Pages
Better Essays
• Good Essays

Faith Integration Paper (Relationship between God, Family, Society) In the field and study of computer science in relation to one’s relationship to others, I believe they can co-exist and that computer science can complement the Christian faith, if kept in perspective. For example, both computer science and Christianity affect the lives of other’s worldwide, especially loved ones at large. As computer science makes technological advances and progresses in discovering and applying new technology…

• 1100 Words
• 5 Pages
Good Essays
• Powerful Essays

(http://cslibrary.stanford.edu/105/) -- a large collection of linked list problems using various pointer techniques (while this binary tree article concentrates on recursion) Pointer and Memory (http://cslibrary.stanford.edu/102/) -- basic concepts of pointers and memory The Great Tree-List Problem (http://cslibrary.stanford.edu/109/) -- a great pointer recursion problem that uses both trees and lists Section 1 -- Introduction To Binary Trees A binary tree is made of nodes, where each node contains a "left"…

• 4816 Words
• 20 Pages
Powerful Essays
• Satisfactory Essays

Scheme of work Year 2012 Term I Computer Department Class: Upper six option A Number of Periods: 3 (Paper 2) Textbook: A- Level computing Educator: Mrs Boodhoo |Week |Themes |Pedagogical strategies |Tasks for students (in class |Assessment | | | | |or as h/w) | | |2 |2…

• 504 Words
• 3 Pages
Satisfactory Essays
• Better Essays

170 CHAPTER 5. RECURSION AND RECURRENCES 5.2 The Master Theorem Master Theorem In the last section, we saw three diﬀerent kinds of behavior for recurrences of the form aT (n/2) + n if n > 1 d if n = 1. T (n) = These behaviors depended upon whether a < 2, a = 2, and a > 2. Remember that a was the number of subproblems into which our problem was divided. Dividing by 2 cut our problem size in half each time, and the n term said that after we completed our recursive work, we had n…

• 5125 Words
• 21 Pages
Better Essays
• Better Essays

Assignment Front Cover Sheet PART 1 – To be completed by the student Student Name Angel Nikolov Student ID Number LON29101212 Module Name (e.g.: Business Environment) Data Structures and Algorithms Course (e.g. HND Business ) BSC in IT Assignment Title Implementing Data Structures and Algorithms Module Lecturer Rafiqul Islam Number of Words 2966 Assignment Due Date 06/05/2014 Submission Date 05/05/2014 First submission  Resubmission (as per lecturer’s instruction) …

• 2965 Words
• 13 Pages
Better Essays
• Better Essays

Executive Summary "Clouds are not spheres, mountains are not cones, coastlines are not circles and bark is not smooth, nor does the lightning travel in a straight line." - Benoit Mandelbrot Beautiful patterns surround us. You can see them on trees, clouds, on bodies of water. You can even see them on plants, on animals and on our very skin. The very tips of our fingers prove just that. There is also no doubt that patterns are just as mysterious as they are beautiful. In fact, there are some patterns…

• 5711 Words
• 23 Pages
Better Essays
• Good Essays

atmosphere can easily be avioded by keeping CO2 levels as low as possible. To begin the research I was given the original DDS model that illustrated the CO2 concentration levels in PPM. The DDS consisted of an initial condition of P1959= 315.97 and a recursion equation of Pn=(.97)*Pn-1+D where Pn equals the amount of CO2 in PPM within the Earth’s atmosphere at year n. I was also givein a DDS model and an excel sheet that illustrated the average annual concentration collected from Mauna Loa, Hawaii.…

• 467 Words
• 2 Pages
Good Essays