CMSC201 Computer Science I for Majors Lecture 19 – Recursion

Slides:



Advertisements
Similar presentations
LEVEL II, TERM II CSE – 243 MD. MONJUR-UL-HASAN LECTURER DEPT OF CSE, CUET Recursive & Dynamic Programming.
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.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Computer Science II Recursion Professor: Evan Korth New York University.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
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.
analysis, plug ‘n’ chug, & induction
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion.
Stacks. An alternative storage structure for collections of entities is a stack. A stack is a simplified form of a linked list in which all insertions.
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.
CIS 068 Welcome to CIS 068 ! Stacks and Recursion.
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++
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
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.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
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.
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.
CMSC201 Computer Science I for Majors Lecture 18 – Classes and Modules (Continued, Part 3) Prof. Katherine Gibson Based on slides from the.
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
Programming with 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.
Topic: Recursion – Part 2
Introduction to Recursion
Introduction to Recursion
Introduction to Computing Science and Programming I
Computers as an Expressive Medium
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
CS 1321.
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 24 – Sorting
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Programming with Recursion
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
CMSC201 Computer Science I for Majors Lecture 16 – Recursion
Ch 12 Recursion Mr. Dave Clausen La Cañada High School
Last Class We Covered Data representation Binary numbers ASCII values
Recursion Chapter 11.
Lesson Objectives Aims
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
Fundamentals of Programming
Log onto a computer first then ….
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
CMSC201 Computer Science I for Majors Lecture 17 – Recursion (cont)
Recursion: The Mirrors
Recursion Taken from notes by Dr. Neil Moore
Yan Shi CS/SE 2630 Lecture Notes
CMSC201 Computer Science I for Majors Lecture 24 – Sorting
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Chapter 18 Recursion.
Last Class We Covered Recursion Stacks Parts of a recursive function:
Chapter 13 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Ch 12 Recursion Mr. Dave Clausen La Cañada High School
Lecture 6 - Recursion.
Presentation transcript:

CMSC201 Computer Science I for Majors Lecture 19 – Recursion Prof. Katherine Gibson Based on slides from the book author, and previous iterations of the course

Last Class We Covered Project 1 Details Classes Inheritance

Any Questions from Last Time?

Today’s Objectives To introduce recursion To begin to learn how to “think” recursively To better understand the concept of stacks

Introduction to Recursion

M.C. Escher: "Drawing Hands" (1948)

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 Solving a problem using recursion means the solution depends on solutions to smaller instances of the same problem

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

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

Simple Recursion Example 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. This program simply computes from 50 down to 2.

Visualizing Recursion To understand how recursion works, it helps to visualize what’s going on. To help visualize, we will use a common concept called the Stack. A stack basically operates like a container of trays in a cafeteria. It has only two operations: Push: you can push something onto the stack. Pop: you can pop something off the top of the stack. Let’s see an example stack in action.

Stacks

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

Stacks In computer science, a stack is a last in, first out(LIFO) abstract data type and data structure. A stack can have any abstract data type as an element, but is characterized by only two fundamental operations, the push and the pop. The push operation adds to the top of the list, hiding any items already on the stack, or initializing the stack if it is empty.

Stacks 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: therefore, the lower elements are typically those that have been in the list the longest.

Stacks and Functions When you run a program, the computer creates a stack for you. Each time you invoke a function, the function is placed on top of the stack. When the function returns or exits, the function is popped off the stack.

Stacks and Functions square() main() main() main() Time 3: Pop: square() returns a value. method exits. Time 4: Pop: main() returns a value. method exits. Time: 0 Empty Stack Time 1: Push: main() Time 2: Push: square() This is called an activation record or stack frame. Usually, this actually grows downward.

Stacks and Recursion Each time a function is called, you push the function on the stack. Each time the function returns or exits, you pop the function off the stack. If a function calls itself recursively, you just push another copy of the function onto the stack. We therefore have a simple way to visualize how recursion really works.

Back to the Simple Recursion Program 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(9) main() compute(9) compute(8) main() compute(9) compute(8) compute(7) … Time: 0 Empty Stack Time 1: Push: main() Time 2: Push: compute(9) Time 3: Push: compute(8) Time 4: Push: compute(7) After returning from compute(2) pop everything Inside compute(9): print (intInput);  9 if (intInput > 2) compute(intInput-1); Inside compute(8): print (intInput);  8 if (intInput > 2) compute(intInput-1); Inside compute(7): print (intInput);  7 if (intInput > 2) compute(intInput-1);

Defining Recursion

Terminology def f(n): if n == 1: return 1 else: return f(n - 1) "Useful" recursive functions have: at least one recursive case at least one base case so that the computation terminates base case recursive case

Recursion def f(n): if n == 1: return 1 else: return f(n + 1) Find f(5) We have a base case and a recursive case. What's wrong?

Recursion The recursive case should call the function on a simpler input, bringing us closer and closer to the base case.

Recursion def f(n): if n == 0: return 0 else: return 1 + f(n - 1) Find f(0) Find f(1) Find f(2) Find f(100)

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

Factorial 4! = 4 × 3 × 2 × 1 = 24

Factorial Does anyone know the value of 9! ? 362,880 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!

Factorial def fact(n): return n * fact(n - 1) fact(3) 3 × fact(2) ...

Factorial What did we do wrong? What is the base case for factorial?

Any Other Questions?

Announcements Lab has been cancelled this week! Project 1 is out Work on your project instead Project 1 is out Due by Tuesday, November 17th at 8:59:59 PM Do NOT procrastinate! Next Class: Recursion