CMSC201 Computer Science I for Majors Lecture 19 – Recursion (Continued) Prof. Katherine Gibson Prof. Jeremy Dixon Based on slides from UPenn’s.

Slides:



Advertisements
Similar presentations
8 Algorithms Foundations of Computer Science ã Cengage Learning.
Advertisements

Chapter 16 Recursion: Another Control Mechanism. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. a.
Factorial Recursion stack Binary Search Towers of Hanoi
Search and Recursion pt. 2 CS221 – 2/25/09. How to Implement Binary Search Take a sorted data-set to search and a key to search for Start at the mid-point.
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.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
COMPSCI 105 S Principles of Computer Science Recursion 3.
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.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
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.
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
Sudeshna Sarkar, IIT Kharagpur 1 Functions : Recursion Lecture
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors Data Abstraction & Problem Solving.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
CSIS 123A Lecture 9 Recursion Glenn Stevenson CSIS 113A MSJC.
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.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
IB Computer Science Unit 5 – Advanced Topics Recursion.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
© 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.
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 2 Objectives Upon completion you will be able to:
CMSC201 Computer Science I for Majors Lecture 20 – Classes and Modules Prof. Katherine Gibson Prof. Jeremy Dixon Based on slides from the.
CMSC201 Computer Science I for Majors Lecture 23 – Algorithms and Analysis Prof. Katherine Gibson Based on slides from previous iterations.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Recursion.
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.
Chapter 15 Recursion.
Introduction to Recursion
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Recursion: The Mirrors
CMSC201 Computer Science I for Majors Lecture 23 – Sorting
Introduction to Computing Science and Programming I
Decrease-and-Conquer Approach
Chapter 15 Recursion.
Introduction to Recursion
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
MA/CSSE 473 Day 02 Some Numeric Algorithms and their Analysis
CMSC201 Computer Science I for Majors Lecture 24 – Sorting
Announcements Final Exam on August 17th Wednesday at 16:00.
CMSC201 Computer Science I for Majors Lecture 16 – Recursion
Applied Algorithms (Lecture 17) Recursion Fall-23
Last Class We Covered Data representation Binary numbers ASCII values
Algorithm design and Analysis
Recursion Chapter 11.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Last Class We Covered Dictionaries Hashing Dictionaries vs Lists
CMSC201 Computer Science I for Majors Lecture 17 – Recursion (cont)
Search,Sort,Recursion.
Basics of Recursion Programming with Recursion
Announcements Last … First … … quiz section … office hour
Search,Sort,Recursion.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Last Class We Covered Recursion Stacks Parts of a recursive function:
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 13 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
ITEC324 Principle of CS III
Presentation transcript:

CMSC201 Computer Science I for Majors Lecture 19 – Recursion (Continued) Prof. Katherine Gibson Prof. Jeremy Dixon Based on slides from UPenn’s CIS 110, and from previous iterations of the course

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

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 – Binary Search – Hailstone problem (Collatz) – Fibonacci Sequence To better understand when it is best to use recursion, and when it is best to use iteration 4

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 6

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 7

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

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 10

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: – Summation of 6: What would a recursive implementation look like? What’s the base case? Recursive case? 11

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

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 main() def main(): summ(4)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 Must pay attention to what happens at the “end” of a function. 30

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

Hailstone Example

The Hailstone Problem Simulating the up and down movement of a hailstone in a storm The problem is actually known as the “Collatz Conjecture” 33 comic courtesy of xkcd.com

Rules of the Collatz Conjecture Three rules to govern how it behaves – If the current height is 1, quit the program – If the current height is even, cut it in half (divide by 2) – If the current height is odd, multiply it by 3, then add 1 This process has also been called HOTPO – Half Or Triple Plus One 34

Implementation It is possible to implement this process using a while loop Can you think of another way to implement it? Recursively! 35

Designing our Recursive Function What is our base case? – When the Height is 1 What is our recursive case? – We have two! What are they? – Height is even:divide by 2 – Height is odd:multiply by 3 and add 1 36

Exercise Create a function hail() that takes in a number and prints out the height of the hailstone at each point in time Important considerations: – What do we check first? Base or recursive case? – Is this function returning anything? Why or why not? 37

Exercise Details Rules for function behavior – If the current height is 1, quit the program – If the current height is even, cut it in half (divide by 2) – If the current height is odd, multiply it by 3, then add 1 Create a function hail() that – Takes in a number – Prints out the height of the hailstone each time 38

Binary Search

Searching Given a list of sorted elements (e.g., words), find a specific word as quickly as possible We could start from the beginning and iterate through the list until we find it – But that could take a long time! 40

Binary Search Uses a “divide and conquer” approach Go to the middle, and compare the element there to the one we’re looking for – If it’s larger, we know it’s not in the last half – If it’s smaller, we know it’s not in the first half – If it’s the same, we found it! 41

Binary Search Example Find "J" ABCDEFGHIJKLMNOPQRSTUVWX

Binary Search Example Find “V" ABCDEFGHIJKLMNOPQRSTUVWX

Binary Search Can be implemented using a while loop – But much more common to use recursion What is the base case? What is the recursive case? 44

Recursion vs Iteration

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

Use Iteration When… Speed and efficiency is an issue – Iteration doesn’t push things onto the stack The problem is an obvious fit for iteration – Processing every element of a list (or 2D list) 47

Use Recursion When… Speed is not an issue The data being processed is recursive – A hierarchical data structure A recursive algorithm is obvious Clarity and simplicity of code is important 48

Fibonacci Sequences

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

Fibonacci Sequence Starts with 0, 1, 1 Next number is …? …987

Recursively Implement Fibonacci The formula for a number in the sequence: F(n) = F(n-1) + F(n-2) What is our base case? What is our recursive case? How would we code this up? 52

Any Other Questions?

Announcements Project 1 is out – Due by Monday, April 18th at 8:59:59 PM – Do NOT procrastinate! Next Class: Modules 54