CMSC201 Computer Science I for Majors Lecture 17 – Recursion (cont)

Slides:



Advertisements
Similar presentations
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Advertisements

Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Fall 2009ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
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.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
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.
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.
M180: Data Structures & Algorithms in Java
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Recursion Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zWhat is a recursive function?
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Computer Science and Software Engineering University of Wisconsin - Platteville 9. Recursion Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
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.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
IB Computer Science Unit 5 – Advanced Topics Recursion.
Lecture 7. Solution by Substitution Method T(n) = 2 T(n/2) + n Substitute n/2 into the main equation 2T(n/2) = 2(2(T(n/4)) + n/2) = 4T(n/4) + n And T(n)
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
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.
Recursion Chapter 10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Recursion Chapter 10 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion (Continued) Prof. Katherine Gibson Prof. Jeremy Dixon Based on slides from UPenn’s.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Recursion.
Programming and Data Structures
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
Recursion Topic 5.
CMSC201 Computer Science I for Majors Lecture 20 – Recursion (Continued) Prof. Katherine Gibson Based on slides from UPenn’s CIS 110, and from previous.
Introduction to Recursion
Recursion DRILL: Please take out your notes on Recursion
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
Decrease-and-Conquer Approach
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
GC211Data Structure Lecture2 Sara Alhajjam.
Introduction to Recursion
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
Java 4/4/2017 Recursion.
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Last Class We Covered File I/O How to open a file
CMSC201 Computer Science I for Majors Lecture 14 – For Loops
Chapter 8: Recursion Java Software Solutions
CMSC201 Computer Science I for Majors Lecture 16 – Recursion
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Applied Algorithms (Lecture 17) Recursion Fall-23
Recursion Chapter 11.
Chapter 8: Recursion Java Software Solutions
Recursion Data Structures.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Last Class We Covered Dictionaries Hashing Dictionaries vs Lists
Unit 3 Test: Friday.
Announcements Last … First … … quiz section … office hour
Chapter 8: Recursion Java Software Solutions
Last Class We Covered File I/O How to open a file
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Chapter 18 Recursion.
Last Class We Covered Recursion Stacks Parts of a recursive function:
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
CS210- Lecture 3 Jun 6, 2005 Announcements
Recursive Function Prepared by Harprith ICT2102 Introduction to Data Structure.
Lecture 6 - Recursion.
Presentation transcript:

CMSC201 Computer Science I for Majors Lecture 17 – Recursion (cont)

Last Class We Covered Recursion Stacks Parts of a recursive function: Base case: when to stop Recursive case: when to go (again)

Any Questions from Last Time?

Today’s Objectives To gain a more solid understanding of recursion To explore what goes on “behind the scenes” To examine individual examples of recursion Fibonacci Sequence To better understand when it is best to use recursion, and when it is best to use iteration

Review of Recursion

What is Recursion? Solving a problem using recursion means the solution depends on solutions to smaller instances of the same problem In other words, to define a function or calculate a number by the repeated application of an algorithm

Recursive Procedures When creating a recursive procedure, there are a few things we want to keep in mind: We need to break the problem into smaller pieces of itself We need to define a “base case” to stop at The smaller problems we break down into need to eventually reach the base case

“Cases” in Recursion A recursive function must have two things: At least one base case When a result is returned (or the function ends) “When to stop” At least one recursive case When the function is called again with new inputs “When to go (again)”

Code Tracing: Recursion

Stacks and Tracing Stacks will help us track what we are doing when tracing through recursive code Remember, stacks are LIFO data structures Last In, First Out We’ll be doing a recursive trace of the summation function

Summation Funcion The addition of a sequence of numbers The summation of a number is that number plus all of the numbers less than it (down to 0) Summation of 5: 5 + 4 + 3 + 2 + 1 Summation of 6: 6 + 5 + 4 + 3 + 2 + 1 What does a recursive implementation look like? What’s the base case? Recursive case?

Summation Function def summ(num): if num == 0: return 0 else: return num + summ(num-1) Base case: Don’t want to go below 0 Summation of 0 is 0 Recursive case: Otherwise, summation is num + summation(num-1)

STACK main() def main(): fact(0) summ(4) fact(1) fact(2) def summ(num): if num == 0: return 0 else: return num + summ(num-1) fact(0) fact(1) fact(2) fact(3) fact(4) main() STACK

STACK main() def main(): summ(4) def summ(num): if num == 0: return 0 else: return num + summ(num-1) STACK main()

STACK main() def main(): summ(4) def summ(num): if num == 0: return 0 else: return num + summ(num-1) num: 4 STACK summ(4) main()

main() def main(): summ(4) This is a local variable. Each time the summ() function is called, the new instance gets its own unique local variables. num = 4 def summ(num): if num == 0: return 0 else: return num + summ(num-1) num: 4 STACK summ(4) main()

STACK main() def main(): summ(4) def summ(num): if num == 0: return 0 else: return num + summ(num-1) num: 4 STACK num = 3 def summ(num): if num == 0: return 0 else: return num + summ(num-1) summ(3) num: 3 summ(4) main()

STACK def summ(num): main() if num == 0: return 0 else: return num + summ(num-1) num: 2 def main(): summ(4) num = 2 num = 4 def summ(num): if num == 0: return 0 else: return num + summ(num-1) num: 4 STACK num = 3 summ(2) def summ(num): if num == 0: return 0 else: return num + summ(num-1) summ(3) num: 3 summ(4) main()

STACK def summ(num): main() if num == 0: return 0 else: return num + summ(num-1) num: 2 def main(): summ(4) num = 2 num = 4 num = 1 def summ(num): if num == 0: return 0 else: return num + summ(num-1) def summ(num): if num == 0: return 0 else: return num + summ(num-1) num: 4 STACK num: 1 summ(1) num = 3 summ(2) def summ(num): if num == 0: return 0 else: return num + summ(num-1) summ(3) num: 3 summ(4) main()

STACK def summ(num): main() if num == 0: return 0 else: return num + summ(num-1) num: 2 def main(): summ(4) num = 2 num = 4 num = 1 def summ(num): if num == 0: return 0 else: return num + summ(num-1) def summ(num): if num == 0: return 0 else: return num + summ(num-1) num: 4 STACK summ(0) num: 1 summ(1) num = 3 summ(2) def summ(num): if num == 0: return 0 else: return num + summ(num-1) num = 0 summ(3) num: 3 def summ(num): if num == 0: return 0 else: return num + summ(num-1) summ(4) main() num: 0

STACK def summ(num): main() if num == 0: return 0 else: return num + summ(num-1) num: 2 def main(): summ(4) num = 2 num = 4 num = 1 def summ(num): if num == 0: return 0 else: return num + summ(num-1) def summ(num): if num == 0: return 0 else: return num + summ(num-1) num: 4 STACK summ(0) num: 1 summ(1) num = 3 summ(2) def summ(num): if num == 0: return 0 else: return num + summ(num-1) num = 0 summ(3) num: 3 def summ(num): if num == 0: return 0 else: return num + summ(num-1) summ(4) main() num: 0

STACK return 0 main() def summ(num): if num == 0: return 0 else: return num + summ(num-1) num: 2 def main(): summ(4) num = 2 num = 4 num = 1 def summ(num): if num == 0: return 0 else: return num + summ(num-1) def summ(num): if num == 0: return 0 else: return num + summ(num-1) num: 4 STACK summ(0) POP! num: 1 summ(1) num = 3 summ(2) def summ(num): if num == 0: return 0 else: return num + summ(num-1) return 0 num = 0 summ(3) num: 3 def summ(num): if num == 0: return 0 else: return num + summ(num-1) summ(4) main() num: 0 return 0

STACK return 1 + 0 (= 1) main() def summ(num): if num == 0: return 0 else: return num + summ(num-1) num: 2 def main(): summ(4) num = 2 num = 4 num = 1 def summ(num): if num == 0: return 0 else: return num + summ(num-1) return 1 def summ(num): if num == 0: return 0 else: return num + summ(num-1) num: 4 STACK POP! num: 1 summ(1) POP! num = 3 summ(2) def summ(num): if num == 0: return 0 else: return num + summ(num-1) summ(3) num: 3 summ(4) main() return 1 + 0 (= 1)

STACK return 2 + 1 (= 3) main() def summ(num): if num == 0: return 0 else: return num + summ(num-1) num: 2 def main(): summ(4) num = 2 num = 4 def summ(num): if num == 0: return 0 else: return num + summ(num-1) num: 4 STACK POP! POP! return 3 num = 3 summ(2) POP! def summ(num): if num == 0: return 0 else: return num + summ(num-1) summ(3) num: 3 summ(4) main() return 2 + 1 (= 3)

STACK return 3 + 3 (= 6) main() def main(): summ(4) def summ(num): if num == 0: return 0 else: return num + summ(num-1) num: 4 STACK POP! POP! num = 3 POP! def summ(num): if num == 0: return 0 else: return num + summ(num-1) return 6 summ(3) POP! num: 3 summ(4) main() return 3 + 3 (= 6)

STACK return 4 + 6 (=10) main() def main(): summ(4) def summ(num): if num == 0: return 0 else: return num + summ(num-1) num: 4 STACK POP! POP! POP! POP! summ(4) POP! main() return 4 + 6 (=10)

STACK return None main() def main(): summ(4) POP! POP! POP! POP! POP!

STACK POP! POP! The stack is empty! POP! POP! POP! POP! return control

Returning and Recursion

Returning Values If your goal is to return a final value Every recursive call must return a value You must be able to pass it “back up” to main() In most cases, the base case should return as well Remember to pay attention to what happens at the “end” of a function

Does this work? What’s wrong? main() def summ(num): if num == 0: return 0 else: num + summ(num-1) def main(): summ(4) num = 2 num: 2 num = 4 num = 1 def summ(num): if num == 0: return 0 else: num + summ(num-1) def summ(num): if num == 0: return 0 else: num + summ(num-1) num: 4 STACK summ(0) num: 1 summ(1) num = 3 summ(2) def summ(num): if num == 0: return 0 else: num + summ(num-1) num = 0 summ(3) num: 3 def summ(num): if num == 0: return 0 else: num + summ(num-1) summ(4) main() num: 0 Does this work? What’s wrong?

Recursion vs Iteration

Recursion and Iteration Both are important All modern programming languages support them Some problems are easy to solve when using one and difficult to solve if using the other How do you decide which to use?

Use Iteration When… Speed and efficiency is an issue Iteration doesn’t push things onto the stack Can’t “run out” of room like recursion does The problem is an obvious fit for iteration Processing every element of a list (or a 2D list)

Use Recursion When… Speed is not an issue The data being processed is recursive A hierarchical data structure A recursive algorithm is obvious (Will happen with time, as you gain experience) Clarity and simplicity of code is important

Recursion Practice

Fibonacci Sequences

Fibonacci Sequence Number series Starts with 0 and 1 Next number is found by adding the previous two numbers together Pattern is repeated over and over (and over…)

Fibonacci Sequence Starts with 0, 1, 1 Next number is …? 1 1 2 3 5 8 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 …

Time for… LIVECODING!!!

Recursively Implement Fibonacci The formula for a position in the sequence: fib(p) = fib(p-1) + fib(p-2) What is our base case? What is our recursive case?

Transform this into a recursive function Non-Recursive exp() How do we create an exponentiation without using the exponentiation operator? Given number and power, we can use code like this: ans = 1 count = 1 while count <= power: ans *= number count += 1 Transform this into a recursive function

Recursive Answer There are other correct answers; this is just one def recExp(number, power): # BASE CASE if power == 0: return 1 # RECURSIVE CASE else: return number * \ recExp(number, power - 1) There are other correct answers; this is just one

Daily CS History Margaret Hamilton Created software for space flight! Apollo 8 First to leave orbit Apollo 11 Moon landing Invented the term “software engineering” Her daughter got to play with space flight simulators!

Announcements Project 2 is out on Blackboard now Project is due by Friday (Nov 10th) at 8:59:59 PM Homework 6 will come out Saturday The topic is recursion – recommended that you do the assignment parts in order Final exam is when? Friday, December 15th from 6 to 8 PM

Image Sources Margaret Hamilton https://en.wikipedia.org/wiki/File:Margaret_Hamilton_in_action.jpg