Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Slides:



Advertisements
Similar presentations
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Advertisements

Chapter 16 Recursion: Another Control Mechanism. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. a.
Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.
Chapter 10 Recursion Instructor: alkar/demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe.
Monday, 12/9/02, Slide #1 CS 106 Intro to CS 1 Monday, 12/9/02  QUESTIONS??  On HW #5 (Due 5 pm today)  Today:  Recursive functions  Reading: Chapter.
Fall 2008ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself.
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.
CS 206 Introduction to Computer Science II 10 / 15 / 2008 Instructor: Michael Eckmann.
Fall 2009ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
Stacks and Queues Introduction to Computing Science and Programming I.
Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)
Recursion.
4.4 Recursive Algorithms A recursive algorithm is one which calls itself to solve “smaller” versions of an input problem. How it works: – The current status.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Functions-Recall 1. 2 Parameter passing & return void main() { int x=10, y=5; printf (“M1: x = %d, y = %d\n”, x, y); interchange (x, y); printf (“M2:
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.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
CSE205 Review Session SAMUEL & JESSA. Recursion WHAT IS RECURSION?  Recursion is a tool a programmer can use to invoke a function call on itself. 
1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Recursion Chapter 2 Objectives Upon completion you will be able to:
Chapter 9 Recursion. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe recursive function is –a.
General Computer Science for Engineers CISC 106 Lecture 09 Dr. John Cavazos Computer and Information Sciences 03/04/2009.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
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
Programming and Data Structures
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
Data Structures with Java © Rick Mercer
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Recursion Lakshmish Ramaswamy.
Chapter 17 Recursion.
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
Introduction to Recursion
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
Topic: Recursion – Part 1
Programming Fundamentals Lecture #7 Functions
Introduction to Computer Science - Alice
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
Intro to Recursion.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Lesson #6 Modular Programming and Functions.
CMSC201 Computer Science I for Majors Lecture 16 – Recursion
MSIS 655 Advanced Business Applications Programming
STACK By:- Rajendra ShakyawalP.G.T. Computer Science KV-No.1, AFS, Tambaram, Chennai.
More Nested Loops and Lab 5
Lesson Objectives Aims
CS201: Data Structures and Discrete Mathematics I
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Types, Truth, and Expressions (Part 2)
CMSC201 Computer Science I for Majors Lecture 17 – Recursion (cont)
Chapter 17 Recursion.
Recursion: The Mirrors
Lesson #6 Modular Programming and Functions.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Last Class We Covered Recursion Stacks Parts of a recursive function:
Chapter 13 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
ITEC324 Principle of CS III
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Presentation transcript:

Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg More Recursion Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Recursion – one way A class of methods exhibit recursive behavior when they can be defined by two properties: A simple base case (or cases), and A set of rules which reduce all other cases toward the base case. (recursive step)

Recursion – another way A recursive function is: One that calls itself With “smaller” inputs (a recursive step) Until those inputs reach a base case

A language based example… The following is a recursive definition of a person's ancestors: Your parents are your ancestors (base case). The parents of your ancestors are also your ancestors (recursion step).

Recursion Humor Recursion See "Recursion".

Better Recursion Humor If you still don't get it, see "Recursion".

Recursion Humor

Some Examples of things that can be done recursively Summation of numbers – sum(lower,upper) Exponents - power(base,exp) Reverse a string – reverse(string) Merge Sort – mergeSort(lyst)

How Does the Computer Keep Track?

The Stack A Stack is a data structure, like a List or a Dictionary, but with a few different characteristics. A Stack is a sequence. A Stack only allows access to one end of its data, the top of the stack.

Operations pop: remove top of stack. Stack is one element smaller. push (val): add val to the stack. Val is now the top. Stack is one element larger. top: Reveals the top of the stack. No modification to stack.

Stack of Function Calls Python maintains a stack of function calls. If a function calls another function recursively, the new function is pushed onto the calling stack and the previous function waits. The top is always the active function. When a pop occurs, the function below becomes active.

Example: Fibonacci Sequence Start with two numbers in the sequence of 1 1 To get the next number in the sequence, add the previous two numbers together 1+1=2 1 1 2 1+2=3 1 1 2 3

How to make Fibonacci a Recursive Function Depends on the Fibo results for the two previous values in the sequence. The base values are Fibo(0) == 1 and Fibo(1) == 1. fibo (x) = fibo(x-1) + fibo(x-2)

Code Listing 16-3 def fibo(n): """Recursive Fibonacci sequence.""" if n == 0 or n == 1: # base case return 1 else: # divide and conquer return fibonacci(n-1) + fibonacci(n-2)

Trace fibo(4) = fibo(3) + fibo(2) fibo(3) = fibo(2) + fibo(1) fibo(2) = fibo(1) + fibo(0) = 2 # base case fibo(3) = 2 + fibo(1) = 3 # base case fibo(4) = 3 + 2 = 5