Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School.

Slides:



Advertisements
Similar presentations
Basics of Recursion Programming with Recursion
Advertisements

1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
Factorial Recursion stack Binary Search Towers of Hanoi
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Recursion CSC 220: Data Structure Winter Introduction A programming technique in which a function calls itself. One of the most effective techniques.
Computer Science II Recursion Professor: Evan Korth New York University.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Recursion. Binary search example postponed to end of lecture.
Recursion Chapter 11 Chapter 11.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Recursion. 2 CMPS 12B, UC Santa Cruz Solving problems by recursion How can you solve a complex problem? Devise a complex solution Break the complex problem.
Recursion Road Map Introduction to Recursion Recursion Example #1: World’s Simplest Recursion Program Visualizing Recursion –Using Stacks Recursion Example.
Recursion.
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.
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.
Fundamental in Computer Science Recursive algorithms 1.
Recursion.  Identify recursive algorithms  Write simple recursive algorithms  Understand recursive function calling  With reference to the call stack.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Recursion Review.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 13 - Recursion.
M180: Data Structures & Algorithms in Java
Recursion.
Fall Week 3 CSCI-141 Scott C. Johnson.  Say we want to draw the following figure ◦ How would we go about doing this?
Functions and an Introduction to Recursion.  Recursive function ◦ A function that calls itself, either directly, or indirectly (through another function)
Chapter 12 Recursion, Complexity, and Searching and Sorting
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
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.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
1 Chapter 3. Recursion Lecture 6. In functions and data structures.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
IB Computer Science Unit 5 – Advanced Topics Recursion.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
Sum of Arithmetic Sequences. Definitions Sequence Series.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Program Development and Design Using C++, Third Edition
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:
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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.
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.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Topic: Recursion – Part 2
Recursion DRILL: Please take out your notes on Recursion
RECURSION.
Computer Science 4 Mr. Gerb
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
Java 4/4/2017 Recursion.
Introduction to Computer Science - Alice
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Ch 12 Recursion Mr. Dave Clausen La Cañada High School
Recursion Chapter 11.
Data Structures and Algorithms
Chapter 12 Supplement: Recursion with Java 1.5
Basics of Recursion Programming with Recursion
Intro to Computer Science CS1510 Dr. Sarah Diesburg
ITEC324 Principle of CS III
Ch 12 Recursion Mr. Dave Clausen La Cañada High School
Presentation transcript:

Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

2 Mr. Dave Clausen Recursion: Definitions ßRecursion ßThe process of a subprogram (function) calling itself. ßA clearly defined stopping state must exist. ßThe well defined termination of the recursive process. ßAny recursive subprogram could be rewritten using iteration (for, while) ßRecursive step ßA step in the recursive process that solves a similar problem of a smaller size and will eventually lead to a termination of the process.

3 Mr. Dave Clausen Recursive Problems in Mathematics ßFactorials ßThe definition of factorials is as follows: ß0! = 1; 1! = 1; for n>1, n! = n*(n-1)*(n-2)*…2*1 ßTo find n! you can calculate n*(n-1)! ßEach step solves a similar problem of smaller size. ßFibonacci sequence ßThe first term is 1, the second term is also 1, and every successive term is the sum of the previous two terms. ßAn Arithmetic Series using sigma notation

4 Mr. Dave Clausen Recursive Example 1 ßFirst recursion example ß(Do not compile or run, there is no stopping state) recursion1.py def Example (value): print(“Hello, value = ” + str(value)) Example (value + 1)

5 Mr. Dave Clausen Recursive Example 2 ßSecond Example recursion2.py def Example2 (value): print(“Hello, value = ” + str(value)) if (value < 5): Example2 (value + 1)

6 Mr. Dave Clausen Example 2 Animation: 1 1 Value TRUE Value < 5 Hello, value = 1 Monitor print("Hello value = ", str(value)) if(value < 5): Example2(value+1)

7 Mr. Dave Clausen Example 2 Animation: 2 2 Value TRUE Value < 5 Hello, value = 1 Hello, value = 2 Monitor print("Hello value = ", str(value)) if(value < 5): Example2(value+1) print("Hello value = ", str(value)) if(value < 5): Example2(value+1)

8 Mr. Dave Clausen Example 2 Animation: 3 3 Value TRUE Value < 5 Hello, value = 1 Hello, value = 2 Hello, value = 3 Monitor print("Hello value = ", str(value)) if(value < 5): Example2(value+1) print("Hello value = ", str(value)) if(value < 5): Example2(value+1) print("Hello value = ", str(value)) if(value < 5): Example2(value+1)

9 Mr. Dave Clausen Example 2 Animation: 4 4 Value TRUE Value < 5 Hello, value = 1 Hello, value = 2 Hello, value = 3 Hello, value = 4 Monitor print("Hello value = ", str(value)) if(value < 5): Example2(value+1) print("Hello value = ", str(value)) if(value < 5): Example2(value+1) print("Hello value = ", str(value)) if(value < 5): Example2(value+1) print("Hello value = ", str(value)) if(value < 5): Example2(value+1)

10 Mr. Dave Clausen Example 2 Animation: 5 5 Value FALSE Value < 5 Hello, value = 1 Hello, value = 2 Hello, value = 3 Hello, value = 4 Hello, value = 5 Monitor print("Hello value = ", str(value)) if(value < 5): Example2(value+1) print("Hello value = ", str(value)) if(value < 5): Example2(value+1) print("Hello value = ", str(value)) if(value < 5): Example2(value+1) print("Hello value = ", str(value)) if(value < 5): Example2(value+1) print("Hello value = ", str(value)) if(value < 5): Example2(value+1)

11 Mr. Dave Clausen Example 2 Animation: 6 5 Value FALSE Value < 5 Hello, value = 1 Hello, value = 2 Hello, value = 3 Hello, value = 4 Hello, value = 5 Monitor

12 Mr. Dave Clausen Tail-recursive Algorithms ßTail-recursive algorithms perform no work after the recursive call. ßExamples 1 and 2 are tail recursive.

13 Mr. Dave Clausen Recursive Example 3 ßThird Example (not tail recursive) recursion3.py def Example3 (value): print(“Hello, value = ” + str(value)) if (value < 5): Example3 (value + 1) print(“Goodbye, value = ” + str(value))

14 Mr. Dave Clausen Example 3 1 Value Hello, value = 1 Monitor print("Hello value = ", str(value)) if(value < 5): Example3(value+1) print("Goodbye value = ", str(value))

15 Mr. Dave Clausen Example 3 2 Value Hello, value = 1 Hello, value = 2 Monitor print("Hello value = ", str(value)) if(value < 5): Example3(value+1) print("Goodbye value = ", str(value)) print("Hello value = ", str(value)) if(value < 5): Example3(value+1) print("Goodbye value = ", str(value))

16 Mr. Dave Clausen Example 3 3 Value Hello, value = 1 Hello, value = 2 Hello, value = 3 Monitor print("Hello value = ", str(value)) if(value < 5): Example3(value+1) print("Goodbye value = ", str(value)) print("Hello value = ", str(value)) if(value < 5): Example3(value+1) print("Goodbye value = ", str(value)) print("Hello value = ", str(value)) if(value < 5): Example3(value+1) print("Goodbye value = ", str(value))

17 Mr. Dave Clausen Example 3 4 Value Hello, value = 1 Hello, value = 2 Hello, value = 3 Hello, value = 4 Monitor print("Hello value = ", str(value)) if(value < 5): Example3(value+1) print("Goodbye value = ", str(value)) print("Hello value = ", str(value)) if(value < 5): Example3(value+1) print("Goodbye value = ", str(value)) print("Hello value = ", str(value)) if(value < 5): Example3(value+1) print("Goodbye value = ", str(value)) print("Hello value = ", str(value)) if(value < 5): Example3(value+1) print("Goodbye value = ", str(value))

18 Mr. Dave Clausen Example 3 5 Value Hello, value = 1 Hello, value = 2 Hello, value = 3 Hello, value = 4 Hello, value = 5 Monitor print("Hello value = ", str(value)) if(value < 5): Example3(value+1) print("Goodbye value = ", str(value)) print("Hello value = ", str(value)) if(value < 5): Example3(value+1) print("Goodbye value = ", str(value)) print("Hello value = ", str(value)) if(value < 5): Example3(value+1) print("Goodbye value = ", str(value)) print("Hello value = ", str(value)) if(value < 5): Example3(value+1) print("Goodbye value = ", str(value)) print("Hello value = ", str(value)) if(value < 5): Example3(value+1) print("Goodbye value = ", str(value))

19 Mr. Dave Clausen Example 3 5 Value Hello, value = 1 Hello, value = 2 Hello, value = 3 Hello, value = 4 Hello, value = 5 Goodbye, value = 5 Monitor print("Hello value = ", str(value)) if(value < 5): Example3(value+1) print("Goodbye value = ", str(value)) print("Hello value = ", str(value)) if(value < 5): Example3(value+1) print("Goodbye value = ", str(value)) print("Hello value = ", str(value)) if(value < 5): Example3(value+1) print("Goodbye value = ", str(value)) print("Hello value = ", str(value)) if(value < 5): Example3(value+1) print("Goodbye value = ", str(value))

20 Mr. Dave Clausen Example 3 4 Value Hello, value = 1 Hello, value = 2 Hello, value = 3 Hello, value = 4 Hello, value = 5 Goodbye, value = 5 Goodbye, value = 4 Monitor print("Hello value = ", str(value)) if(value < 5): Example3(value+1) print("Goodbye value = ", str(value)) print("Hello value = ", str(value)) if(value < 5): Example3(value+1) print("Goodbye value = ", str(value)) print("Hello value = ", str(value)) if(value < 5): Example3(value+1) print("Goodbye value = ", str(value))

21 Mr. Dave Clausen Example 3 3 Value Hello, value = 1 Hello, value = 2 Hello, value = 3 Hello, value = 4 Hello, value = 5 Goodbye, value = 5 Goodbye, value = 4 Goodbye, value = 3 Monitor print("Hello value = ", str(value)) if(value < 5): Example3(value+1) print("Goodbye value = ", str(value)) print("Hello value = ", str(value)) if(value < 5): Example3(value+1) print("Goodbye value = ", str(value))

22 Mr. Dave Clausen Example 3 2 Value Hello, value = 1 Hello, value = 2 Hello, value = 3 Hello, value = 4 Hello, value = 5 Goodbye, value = 5 Goodbye, value = 4 Goodbye, value = 3 Goodbye, value = 2 Monitor print("Goodbye value = ", str(value)) print("Hello value = ", str(value)) if(value < 5): Example3(value+1) print("Goodbye value = ", str(value))

23 Mr. Dave Clausen Example 3 1 Value Hello, value = 1 Hello, value = 2 Hello, value = 3 Hello, value = 4 Hello, value = 5 Goodbye, value = 5 Goodbye, value = 4 Goodbye, value = 3 Goodbye, value = 2 Goodbye, value = 1 Monitor print("Goodbye value = ", str(value))

24 Mr. Dave Clausen Example 3 1 Value Hello, value = 1 Hello, value = 2 Hello, value = 3 Hello, value = 4 Hello, value = 5 Goodbye, value = 5 Goodbye, value = 4 Goodbye, value = 3 Goodbye, value = 2 Goodbye, value = 1 Monitor

25 Mr. Dave Clausen Recursive Example 4 ßArithmetic Series (sigma) Example RecursiveSum.py int sigma(int n) { if(n <= 1) return n; else return n + sigma(n-1); }

26 Mr. Dave Clausen Sigma Example Function Calls ßIf we call the sigma function with the statement, sum=sigma(5) ßthe else portion of the first function calls the function again, return 5 + sigma(4); ßat this point the value of sigma (4) must be computed ßthis calls return 4 + sigma (3) ßwhich calls return 3 + sigma(2) ßthen return 2 + sigma(1) ßfinally this returns the value of 1.

27 Mr. Dave Clausen Visualizing the sigma Function Calls ßreturn 5 + sigma(4) ßreturn 4 + sigma(3) ßreturn 3 + sigma(2) ßreturn 2 + sigma(1) ßreturn 1 ßat the end of the recursive calls the steps are reversed for assigning the values. ßreturn 1 ßreturn(2+1)(=3) ßreturn(3+3)(=6) ßreturn(4+6)(=10) ßreturn(5+10)(=15)

28 Mr. Dave Clausen Stacks ßImagine a stack as a pile of trays in a cafeteria. The last tray put on the stack is the first one taken off of the stack. ßThis is what occurs in memory when a subprogram calls itself. ßA stack is a dynamic data structure where access can be made only from one end. This is called a Last In First Out (LIFO) structure.

29 Mr. Dave Clausen Stack Animation ßLevel 5n: 1 Sigma: ßLevel 4:n: 2 Sigma: ßLevel 3:n: 3 Sigma: ßLevel 2:n: 4 Sigma: ßLevel 1:n: 5 Sigma:

30 Mr. Dave Clausen The Costs and Benefits of Recursion ßCosts ßA recursive process that requires N recursive calls uses N+1 units of stack memory and processor time to manage the process. ßAn equivalent iterative process always uses one stack of memory and processor time to manage the method call, therefore, recursion requires more memory and processor power than the non-recursive process. ßBenefits ßShort, simple & elegant solutions using divide & conquer algorithms (solve the problem by repeatedly dividing it into simpler problems.)

31 Mr. Dave Clausen Some Recursive Algorithms ßThe Binary Search ßThe Quick Sort ßThe Merge Sort

32 Mr. Dave Clausen M. C. Escher & Recursion ßM.C. Escher “Drawing Hands” lithograph ßIllustrates the concept of Recursion