Recursion, pt. 1 The Foundations. What is Recursion? Recursion is the idea of solving a problem in terms of itself. – For some problems, it may not possible.

Slides:



Advertisements
Similar presentations
§3 Dynamic Programming Use a table instead of recursion 1. Fibonacci Numbers: F(N) = F(N – 1) + F(N – 2) int Fib( int N ) { if ( N
Advertisements

Recursion (define tell-story (lambda () (print ‘’Once upon a time there was a mountain. ‘’) (print ‘’On the mountain, there was a temple. ‘’) (print ‘’In.
Recursion, pt. 2: Thinking it Through. What is Recursion? Recursion is the idea of solving a problem in terms of solving a smaller instance of the same.
Recursion in Python. Recursion Problems in every area of life can be defined recursively, that is, they can be described in terms of themselves. An English.
Recursion.
Recursion Chapter 7. Spring 2010CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn how.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
1 Introduction to Recursion  Introduction to Recursion  Example 1: Factorial  Example 2: Reversing Strings  Example 3: Fibonacci  Infinite Recursion.
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Fall 2007CS 2251 Recursion Chapter 7. Fall 2007CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
ICS103 Programming in C Lecture 11: Recursive Functions
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Unit 7 1 Unit 7: Recursion H Recursion is a fundamental programming technique that can provide an elegant solution to a certain kinds of problems H In.
analysis, plug ‘n’ chug, & induction
Fibonacci numbers Fibonacci numbers:Fibonacci numbers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,... where each number is the sum of the preceding two. Recursive.
1 Recursion Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
CMSC 2021 Recursion Recursive Definition – one that defines something in terms of itself Recursion – A technique that allows us to break down a problem.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
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 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)
Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation.
M180: Data Structures & Algorithms in Java
Lecture 10 Recursion CSE225: Data Structures. 2 A Look Back at Functions #include double distance(double x1, double y1, double x2, double y2) { double.
Week 5 - Monday.  What did we talk about last time?  Linked list implementations  Stacks  Queues.
1 Joe Meehean.  call themselves directly  or indirectly void f(){... f();... } void g(){... h();... } void h(){... g();... }
Data Structures Recursion Phil Tayco Slide version 1.0 Mar. 8, 2015.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
ASET RECURSION. ASET RECURSIVE FUNCTIONS A recursive function is a function that calls itself to solve a smaller version of its task until a final call.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
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.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive 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.
W1-1 University of Washington Computer Programming I Recursion © 2000 UW CSE.
A Different Solution  alternatively we can use the following algorithm: 1. if n == 0 done, otherwise I. print the string once II. print the string (n.
CS162 - Topic #10 Lecture: Recursion –The Nature of Recursion –Tracing a Recursive Function –Work through Examples of Recursion Programming Project –Discuss.
1 Chapter 8 Recursion. 2 Recursive Function Call a recursion function is a function that either directly or indirectly makes a call to itself. but we.
Recursion Powerful Tool
CS212: Data Structures and Algorithms
Recursion.
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Recursion The programs discussed so far have been structured as functions that invoke one another in a disciplined manner For some problems it is useful.
Recursion Topic 5.
Chapter 15 Recursion.
CS 211 Object Oriented Programming
Chapter 15 Recursion.
Java 4/4/2017 Recursion.
Use mathematical induction to prove that the formula is true for all natural numbers m. {image} Choose the first step of the proof from the following:
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Applied Algorithms (Lecture 17) Recursion Fall-23
CS201: Data Structures and Discrete Mathematics I
Functions Recursion CSCI 230
Applied Discrete Mathematics Week 9: Integer Properties
Module 1-10: Recursion.
Recursion Taken from notes by Dr. Neil Moore
Main() { int fact; fact = Factorial(4); } main fact.
Presentation transcript:

Recursion, pt. 1 The Foundations

What is Recursion? Recursion is the idea of solving a problem in terms of itself. – For some problems, it may not possible to find a direct solution. – Instead, the problem is typically broken down, progressively, into simpler and simpler versions of itself for evaluation.

What is Recursion? One famous problem which is solved in a recursive manner: the factorial. – n! = 1 for n = 0, n =1… – n! = n * (n-1)!, n > 1. Note that aside from the n=0, n=1 cases, the factorial’s solution is stated in terms of a reduced form of itself.

What is Recursion? As long as n is a non-negative integer, n! will eventually reach a reduced form for which there is an exact solution. – 5! = 5 * 4! = 5 * 4 * 3! = … = 5 * 4 * 3 * 2 * 1

What is Recursion? From this point, the solution for the reduced problem will be used to determine the exact solution. 5 * 4 * 3 * 2 * 1= 5 * 4 * 3 * 2 = 5 * 4 * 6 = 5 * 24 = 120.

Recursion Thus, the main idea of recursion is to reduce a complex problem to a combination of operations upon its simplest form. – This “simplest form” has a well- established, exact solution.

What is Recursion? As a result of how recursion works, it ends up being subject to a number of jokes: – “In order to understand recursion, you must understand recursion.” – Or, “recursion (n): See recursion.”

A Mathematical Look Recursion is actually quite similar to a certain fairly well-known mathematical proof technique: induction.

A Mathematical Look

The main idea behind how induction works is the same as that for recursion. – The process is merely inverted: the way that induction proves something is how recursion will actually produce its solution. While this may be inefficient for problems with a closed-form solution, there are other problems which have no closed form.

The Basic Process There are two main elements to a recursive solution: – The base case: the form (or forms) of the problem for which an exact solution is provided. – The recursive step: the reduction of one version the problem to a simpler form.

The Basic Process Note that if we progressively reduce the problem, one step at a time, we’ll eventually hit the base case. – From there, we take that solution and modify it as necessary on the way back up to yield the true solution.

The Basic Process There are thus these two main elements to a recursive solution of the factorial method: – The base case: 0! and 1! – The recursive step: n! = n * (n-1)! Note that the “n *” will be applied after the base case is reached. (n-1)! is the reduced form of the problem.

Questions?

Coding Recursion As we’ve already seen, programming languages incorporate the idea of function calls. – This allows us to reuse code in multiple locations within a program. – Is there any reason that a function shouldn’t be able to reuse itself?

Coding Recursion int factorial(int n) { if(n == 0 || n == 1) return 1; else return n * factorial(n-1); }

Coding Recursion Potential problem: how can the program keep track of its state? – There will multiple versions of “n” over the different calls of the factorial function. – The answer: stacks! The stack is a data structure we haven’t yet seen, but may examine in brief later in the course.

Coding Recursion int factorial(int n) { if(n == 0 || n == 1) return 1; else return n * factorial(n-1); } Each individual method call within a recursive process can be called a frame.

Coding Recursion int factorial(int n) { if(n == 0 || n == 1) return 1; else return n * factorial(n-1); } We’ll use this to denote each frame of this method’s execution. – Let’s try n = 5. n:

Coding Recursion int factorial(int n) { if(n == 0 || n == 1) return 1; else return n * factorial(n-1); } n: Result: 1

1 2 Coding Recursion int factorial(int n) { if(n == 0 || n == 1) return 1; else return n * factorial(n-1); } n:

Result: 2 6 Coding Recursion int factorial(int n) { if(n == 0 || n == 1) return 1; else return n * factorial(n-1); } n: 5 4 3

Result: 6 24 Coding Recursion int factorial(int n) { if(n == 0 || n == 1) return 1; else return n * factorial(n-1); } n: 5 4

Result: 24 Result: 120 Coding Recursion int factorial(int n) { if(n == 0 || n == 1) return 1; else return n * factorial(n-1); } n: 5

Coding Recursion Notice how recursion looks in code – we have a function that calls itself. – In essence, we assume that we already have a function that already does most of the work needed, aside from one small manipulation. – The trick is that this “already there” function is actually the function we’re writing.

Coding Recursion

Questions?

Recursion - Fibonacci Let’s examine how this would work for another classic recursive problem. – The Fibonacci sequence: Fib(0) = 1 Fib(1) = 1 Fib(n) = Fib(n-2) + Fib(n-1) – How can we code this? What parts are the base case? What parts are the recursive step?

Recursion - Fibonacci int fibonacci(int n) { if(n == 0 || n == 1) return 1; else return fibonacci(n-2) + fibonacci(n-1); }

Recursion - Fibonacci int fibonacci(int n) { if(n == 0 || n == 1) return 1; else A:return fibonacci(n-2) + B: fibonacci(n-1); } We’ll use the below graphics to aid our analysis of this method. n: pos: part:res:

Recursion - Fibonacci if(n == 0 || n == 1) return 1; else A:return fibonacci(n-2) + B: fibonacci(n-1); n: pos: part: 5A--- n: pos: part: 3A--- res: 1

Recursion - Fibonacci if(n == 0 || n == 1) return 1; else A:return fibonacci(n-2) + B: fibonacci(n-1); n: pos: part: 5A--- n: pos: part: 3B1 res: 1 n: pos: part: 2A---

Recursion - Fibonacci if(n == 0 || n == 1) return 1; else A:return fibonacci(n-2) + B: fibonacci(n-1); n: pos: part: 5A--- n: pos: part: 3B1 res: 1 n: pos: part: 2B1

Recursion - Fibonacci if(n == 0 || n == 1) return 1; else A:return fibonacci(n-2) + B: fibonacci(n-1); n: pos: part: 5A--- n: pos: part: 3B1 res: 2

Recursion - Fibonacci if(n == 0 || n == 1) return 1; else A:return fibonacci(n-2) + B: fibonacci(n-1); n: pos: part: 5A--- res: 3

Recursion - Fibonacci if(n == 0 || n == 1) return 1; else A:return fibonacci(n-2) + B: fibonacci(n-1); n: pos: part: 5B3 4A--- n: pos: part: 2A--- res: …

Recursion - Fibonacci if(n == 0 || n == 1) return 1; else A:return fibonacci(n-2) + B: fibonacci(n-1); n: pos: part: 5B3 4A--- n: pos: part: 2A--- res: … Didn’t we already get an answer for n = 2? Yep. So I’ll save us some time.

Recursion - Fibonacci if(n == 0 || n == 1) return 1; else A:return fibonacci(n-2) + B: fibonacci(n-1); n: pos: part: 5B3 4A--- res: 2

Recursion - Fibonacci if(n == 0 || n == 1) return 1; else A:return fibonacci(n-2) + B: fibonacci(n-1); n: pos: part: 5B3 4B2 3A--- Didn’t we already get an answer for n = 3? Yep. So I’ll save us some time.

Recursion - Fibonacci if(n == 0 || n == 1) return 1; else A:return fibonacci(n-2) + B: fibonacci(n-1); n: pos: part: 5B3 4B2 Didn’t we already get an answer for n = 3? Yep. So I’ll save us some time. res: 3

Recursion - Fibonacci if(n == 0 || n == 1) return 1; else A:return fibonacci(n-2) + B: fibonacci(n-1); n: pos: part: 5B3 res: 5

Recursion - Fibonacci if(n == 0 || n == 1) return 1; else A:return fibonacci(n-2) + B: fibonacci(n-1); res: 8

Recursion - Fibonacci Can this be done more efficiently? – You betcha! First off, note that we had had to recalculate some of the intermediate answers. What if we could have saved those answers? It’s possible, and the corresponding technique is called dynamic programming. We’ll not worry about that for now.

Fibonacci

Questions?