Recursion AP Computer Science A Mr. Langner By: Thomas Robbins.

Slides:



Advertisements
Similar presentations
Lecture 12 Recursion part 1
Advertisements

New Mexico Computer Science For All
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Recursion Introduction to Computing Science and Programming I.
16-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Imperative programming public int factorial (int N){ int F = 1; for(X=N; X>1; X-- ){ F= F*X; } return F; } Functional programming (defun factorial(N) (cond.
Topic 7 – Recursion (A Very Quick Look). CISC 105 – Topic 7 What is Recursion? A recursive function is a function that calls itself. Recursive functions.
28-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
29-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Prof. S.M. Lee Department of Computer Science. Answer:
Recursion In general there are two approaches to writing repetitive algorithms. One uses loops(while, do while and for): the other uses recursion. Recursion.
June 18, 2015IAT 2651 Recursion. June 18, 2015IAT 2652 Today’s Excitement  Recursion.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Recursion. Basic problem solving technique is to divide a problem into smaller sub problems These sub problems may also be divided into smaller sub problems.
M180: Data Structures & Algorithms in Java
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
CIS 068 Welcome to CIS 068 ! Stacks and Recursion.
Lecture 12 Recursion part 1 Richard Gesick. Recursion A recursive method is a method that calls itself. A recursive method is capable of solving only.
Recursion in C++. Recursion Recursive tasks: A task that is defined in terms of itself. A function that calls itself. With each invocation, the problem.
Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
Hopefully this lesson will give you an inception of what recursion is.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
IB Computer Science Unit 5 – Advanced Topics Recursion.
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
Recursion A function that calls itself. Recursion A function which calls itself is said to be recursive. Recursion is a technique which will allow us.
Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem.
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
Concepts of Algorithms CSC-244 Unit 5 and 6 Recursion Shahid Iqbal Lone Computer College Qassim University K.S.A.
C++ Programming Lecture 12 Functions – Part IV
CSE 143 Lecture 9 Recursion slides created by Alyssa Harding
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Welcome to Recursion! Say what?!? Recursion is… the process of solving large problems by simplifying them into smaller ones. similar to processing using.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
Recursion Function calling itself
Identify the Appropriate Method for Handling Repetition
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Introduction to Computing Science and Programming I
Computers as an Expressive Medium
Introduction to Recursion
Java 4/4/2017 Recursion.
IAT 800 Recursion Oct 28, 2009 IAT 800.
IAT 265 Recursion May 24, 2016 IAT 265.
Recursion
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Lecture 17 Recursion part 1 Richard Gesick.
Recursion.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Recursion based on slides by Alyssa Harding
Lecture 12 Recursion part 1 CSE /26/2018.
CSC 143 Recursion.
Review of Previous Lesson
MIPS function continued
Fundaments of Game Design
Main() { int fact; fact = Factorial(4); } main fact.
Self-Referencing Functions
Recursion.
Presentation transcript:

Recursion AP Computer Science A Mr. Langner By: Thomas Robbins

 Recursion happens whenever a method has a statement inside it that causes the method to call itself.  Example: Public int recursion(int a, int b) { //other body statements return 1 + recursion(int a, int b-1); } Overview

Two Important Things  There are two things to know when coding with recursion;  1)If the problem is simple and easy, solve it right away.  2)If the problem isn’t so simple and appears very difficult, you need to break it down into smaller parts.  Then solve these.

Example  Say you want to divide a piece of wood into x amount of parts.

Example  You will use recursion until all the pieces of wood are small enough.  Remember the two important things:  1)If the wood is small enough, stop dividing  2)Else, divide the wood in two pieces and do the same for the next pieces.

Example Etc, etc, until the pieces are small enough

Coding Examples  Now that we understand the principle behind recursion, we can take a look at some specific examples.  Probably the easiest way to describe recursion to someone would be to relate factorials. Ex: 5! = 5 * 4 * 3 * 2 * 1 = 120 Ex: 3! = 3 * 2 * 1 = 6

Psuedocode 1. If the number is equal to one, then the factorial equals one. 2. Else, the factorial of the number equals itself times itself minus one.  factorial(0) = 1, factorial(1) = 1  factorial(N) = N * factorial( N-1 )

Simple Code The Recursion that progresses towards the base case. Code similar to this is nearly mandatory on all recursive methods. It’s called a “Base case” and prevents the method from entering an infinite loop. static int fact(int n) { if (n <= 1) { return 1; return 1;} else { return n * fact(n-1); return n * fact(n-1);}

Break it down!  Terrible pun aside, here’s a good break down of the factorial method’s process:  factorial(5) = 5 * factorial(4) = 5 * ( 4 * factorial(3)) = 5 * ( 4 * factorial(3)) = 5 * ( 4 * (3 * factorial(2))) = 5 * ( 4 * (3 * factorial(2))) = 5 * ( 4 * (3 * (2 * factorial(1)))) = 5 * ( 4 * (3 * (2 * factorial(1)))) = 5 * ( 4 * (3 * (2 * (1 * factorial(0))))) = 5 * ( 4 * (3 * (2 * (1 * factorial(0))))) = 5 * ( 4 * (3 * (2 * (1 * 1)))) = 5 * ( 4 * (3 * (2 * (1 * 1)))) = 5 * 4 * 3 * 2 * 1 * 1 = 120 = 5 * 4 * 3 * 2 * 1 * 1 = 120

Another example of Recursion!  Counting the number of bowling pins in a triangle: trianglePins(int N) //N is the number of rows { if ( N == 1 ) if ( N == 1 ) return 1; return 1; else else return N + Triangle( N-1 ); return N + Triangle( N-1 ); }

The breakdown trianglePins(5 ) = 5 + trianglePins (4) = 5 + (4 + trianglePins(3)) = 5 + (4 + trianglePins(3)) = 5 + (4 + (3 + trianglePins(2))) = 5 + (4 + (3 + trianglePins(2))) = 5 + (4 + (3 + (2 + trianglePins(1)))) = 5 + (4 + (3 + (2 + trianglePins(1)))) = 5 + (4 + (3 + (2 + 1))) = 5 + (4 + (3 + (2 + 1))) = 5 + (4 + (3 + 3)) = 5 + (4 + (3 + 3)) = 5 + (4 + 6) = 5 + (4 + 6) = = = 15 = 15