CMSC201 Computer Science I for Majors Lecture 19 – Recursion

Slides:



Advertisements
Similar presentations
Recursion Genome 559: Introduction to Statistical and Computational Genomics Elhanan Borenstein.
Advertisements

Recursion vs. Iteration The original Lisp language was truly a functional language: –Everything was expressed as functions –No local variables –No iteration.
8 Algorithms Foundations of Computer Science ã Cengage Learning.
Chapter 16 Recursion: Another Control Mechanism. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. a.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Computer Science II Recursion Professor: Evan Korth New York University.
Recursion. Binary search example postponed to end of lecture.
Recursion Introduction to Computing Science and Programming I.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
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 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.
analysis, plug ‘n’ chug, & induction
Stacks and Queues Introduction to Computing Science and Programming I.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
M180: Data Structures & Algorithms in Java
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
1 CS 177 Week 16 Recitation Recursion. 2 Objective To understand and be able to program recursively by breaking down a problem into sub problems and joining.
1 Lecture 14 Chapter 18 - Recursion. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
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 Department Data Structure & Algorithms Lecture 8 Recursion.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
General Computer Science for Engineers CISC 106 Lecture 06 James Atlas Computer and Information Sciences 06/24/2009.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion (Continued) Prof. Katherine Gibson Prof. Jeremy Dixon Based on slides from UPenn’s.
Recursion Powerful Tool
Recursion.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Recursion.
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
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
Introduction to Recursion
Revised based on textbook author’s notes.
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
Introduction to Computing Science and Programming I
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
CMSC201 Computer Science I for Majors Lecture 22 – Searching
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
To understand recursion, you have to understand recursion!
CMSC201 Computer Science I for Majors Lecture 10 – Functions (cont)
Programming with Recursion
CMSC201 Computer Science I for Majors Lecture 16 – Recursion
Last Class We Covered Data representation Binary numbers ASCII values
Fundamentals of Programming
Log onto a computer first then ….
Last Class We Covered Dictionaries Hashing Dictionaries vs Lists
Coding Concepts (Basics)
CMSC201 Computer Science I for Majors Lecture 17 – Recursion (cont)
Programming with Recursion
Recursion Taken from notes by Dr. Neil Moore
Last Class We Covered File I/O How to open a file
Yan Shi CS/SE 2630 Lecture Notes
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Chapter 19: Recursion.
Programming with Recursion
Chapter 18 Recursion.
Last Class We Covered Recursion Stacks Parts of a recursive function:
CMSC201 Computer Science I for Majors Lecture 12 – Program Design
Lecture 20 – Practice Exercises 4
Lecture 6 - Recursion.
Presentation transcript:

CMSC201 Computer Science I for Majors Lecture 19 – Recursion

Last Class We Covered What makes “good code” good Readability Adaptability Commenting guidelines Incremental development #TBT Function Returns

Any Questions from Last Time?

Today’s Objectives To introduce recursion To better understand the concept of “stacks” To begin to learn how to “think recursively” To look at examples of recursive code Summation, factorial, etc.

Introduction to Recursion

What is Recursion? In computer science, recursion is a way of thinking about and solving problems It’s actually one of the central ideas of CS In recursion, the solution depends on solutions to smaller instances of the same problem

Recursive Solutions When creating a recursive solution, 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

Normal vs Recursive Functions So far, we’ve had functions call other functions For example, main() calls the square() function A recursive function, however, calls itself main() square() compute()

Why Would We Use Recursion? In computer science, some problems are more easily solved by using recursive methods For example: Traversing through a directory or file system Traversing through a tree of search results Some sorting algorithms recursively sort data For today, we will focus on the basic structure of using recursive methods

Recursion Examples Traversing a Binary Search Tree Sorting data https://commons.wikimedia.org/wiki/File:Binary_Search_Tree_-_Remove_Step_3.svg https://commons.wikimedia.org/wiki/File:Selection-Sort-Animation.gif Image from wikimedia.org

Toy Example of Recursion def compute(intInput): print(intInput) if (intInput > 2): compute(intInput-1) def main(): compute(50) main() This is where the recursion occurs. You can see that the compute() function calls itself. What does this program do? This program prints the numbers from 50 down to 2.

Visualizing Recursion To understand how recursion works, it helps to visualize what’s going on Python uses a stack to keep track of function calls A stack is an important computer science concept To help visualize, we will

Stacks http://www.topwithcinnamon.com/2013/01/2-ingredient-healthy-pancakes-gluten-free-dairy-free.html http://i1.wp.com/www.topwithcinnamon.com/wp-content/uploads/2013/01/OMFGTHISISTHEBESTGIFIVEEVEREVEREVERMADEEE.gif?resize=654%2C800 Image from www.topwithcinnamon.com

Stacks A stack is like a bunch of lunch trays in a cafeteria It has only two operations: Push You can push something onto the top of the stack Pop You can pop something off the top of the stack Let’s see an example stack in action

Stack Example The diagram below shows a stack over time We perform two pushes and two pops Time: 0 Empty Stack 2 2 8 2 Time 1: Push “2” Time 2: Push “8” Time 3: Pop Time 4: Pop

Stack Details In computer science, a stack is a last in, first out (LIFO) data structure It can store any type of data, but has only two operations: push and pop Push adds to the top of the stack, hiding anything else on the stack Pop removes the top element from the stack

Stack Details The nature of the pop and push operations also means that stack elements have a natural order Elements are removed from the stack in the reverse order to the order of their addition The lower elements are those that have been in the stack the longest

Stacks and Functions When you run your program, the computer creates a stack for you Each time you call a function, the function is pushed onto the top of the stack When the function returns or exits, the function is popped off the stack

Stacks and Functions Example Time: 0 Empty Stack main() main() square() main() Time 3: Pop: square() returns a value. method exits. Time 4: Pop: main() returns a value. method exits. Time 1: Push: main() Time 2: Push: square()

Stacks and Recursion If a function calls itself recursively, you push another call to the function onto the stack We now have a simple way to visualize how recursion really works

Toy Example of Recursion def compute(intInput): print(intInput) if (intInput > 2): compute(intInput-1) def main(): compute(50) main() Here’s the code again. Now, that we understand stacks, we can visualize the recursion.

Stack and Recursion in Action main() main() compute (4) main() compute (4) compute (3) main() compute(4) compute(3) compute(2) main() compute(4) compute(3) … Time: 0 Empty Stack Time 1: Push: main() Time 2: Push: compute(4) Time 3: Push: compute(3) Time 4: Push: compute(2) Time 4: Pop After every function returns (pops)

Defining Recursion

“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)”

Terminology def fxn(n): if n == 1: return 1 else: return fxn(n - 1) Notice that the recursive call is passing in simpler input, approaching the base case base case recursive case recursive call

Recursion Example What is sum (1) ? What is sum (2) ? def sum(n): if n == 1: return 1 else: return n + sum (n - 1) What is sum (1) ? What is sum (2) ? What is sum (100) ? We at least know that it’s 100 + sum(99)

Recursion Example def sum(n): if n == 1: return 1 else: return n + sum (n - 1) sum(3) 3 + sum(2) 2 + sum(1) 1 3 + 2 + 1 = 6

Factorials 4! = 4 × 3 × 2 × 1 = 24 Does anyone know the value of 9! ? 362,880 Does anyone know the value of 10! ? How did you know?

Factorial 9! = 9×8×7×6×5×4×3×2×1 10! = 10 × 9×8×7×6×5×4×3×2×1 9! = 9×8×7×6×5×4×3×2×1 10! = 10 × 9×8×7×6×5×4×3×2×1 10! = 10 × 9! n! = n × (n - 1)! That's a recursive definition! The answer to a problem can be defined as a smaller piece of the original problem

What happened? What went wrong? Factorial def fact(n): return n * fact(n - 1) fact(3) 3 * fact(2) 3 * 2 * fact(1) 3 * 2 * 1 * fact(0) 3 * 2 * 1 * 0 * fact(-1) ... What happened? What went wrong?

Factorial (Fixed) def fact(n): if n == 0: return 1 else: return n * fact(n - 1) fact(3) 3 * fact(2) 3 * 2 * fact(1) 3 * 2 * 1 * fact(0) 3 * 2 * 1 * 1

Recursion Practice

Thinking Recursively Anything we can do with a while loop can also be accomplished through recursion Let’s get some practice by transforming basic loops into a recursive function To keep in mind: What is the base case? The recursive case? Are we returning values, and if so, how?

Non-Recursive getGrade() Gets a grade between 0 and 100, inclusive def getGrade(): grade = int(input("Grade? ")) while grade < 0 or grade > 100: print("That's not valid.") return grade Transform this into a recursive function

Non-Recursive sumList() Sum the contents of a list together def sumList(numList): total = 0 for num in numList: total = total + num return total Transform this into a recursive function

Recursive Thinking Sometimes, creating a recursive function requires us to think about the problem differently What kind of base case do we need for summing a list together? How do we know we’re “done”? Instead of approaching the problem as before, think of it instead as adding the first element to the sum of the rest of the list

Recursive Summing What is the base case here? myList = [3, 5, 8, 7, 2, 6, 1] 3 + [5, 8, 7, 2, 6, 1] 5 + [8, 7, 2, 6, 1] 8 + [7, 2, 6, 1] etc... What is the base case here? How does the recursive case work?

Announcements Project 2 out on Blackboard Final exam is when? Project due Friday, April 21st @ 8:59:59 PM Uses 3D lists and file I/O Final exam is when? Friday, May 19th from 6 to 8 PM Survey #2 out now, due Sunday @ 11:59 PM