1 Survey of Computer Science CSCI 110, Spring 2011 Lecture 14 Recursion.

Slides:



Advertisements
Similar presentations
Introduction to Recursion and Recursive Algorithms
Advertisements

1 Data Structures CSCI 132, Spring 2014 Lecture 15 Recursion.
New Mexico Computer Science For All
Recursion –a programming strategy for solving large problems –Think “divide and conquer” –Solve large problem by splitting into smaller problems of same.
Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate.
Recursion Introduction to Computing Science and Programming I.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
Recursion.
ICS103 Programming in C Lecture 11: Recursive Functions
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.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Guide to Programming with Python
1 Chapter 18-1 Recursion Dale/Weems. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
1 Recursion Dr. Bernard Chen Ph.D. University of Central Arkansas.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Recursion.
Fall Week 3 CSCI-141 Scott C. Johnson.  Say we want to draw the following figure ◦ How would we go about doing this?
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
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.
Sudeshna Sarkar, IIT Kharagpur 1 Functions : Recursion Lecture
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
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.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 18 What are programs for? Classes and Objects.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 4 Conditionals Turtles.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursion.
A Different Solution  alternatively we can use the following algorithm: 1. if n == 0 done, otherwise I. print the string once II. print the string (n.
Pei Zheng, Michigan State University 1 Chapter 8 Recursion.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
1 Chapter 8 Recursion. 2 Recursive Function Call a recursion function is a function that either directly or indirectly makes a call to itself. but we.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Recursion Powerful Tool
Recursion.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Recursion.
Recursion The programs discussed so far have been structured as functions that invoke one another in a disciplined manner For some problems it is useful.
Lesson #6 Modular Programming and Functions.
Introduction to Computing Science and Programming I
Lesson #6 Modular Programming and Functions.
General Form for a Conditional
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
Java 4/4/2017 Recursion.
Topic: Functions – Part 2
Recursion
Lesson #6 Modular Programming and Functions.
Applied Algorithms (Lecture 17) Recursion Fall-23
Recursion Chapter 11.
Programming application CC213
Lesson #6 Modular Programming and Functions.
15-110: Principles of Computing
Recursion Taken from notes by Dr. Neil Moore
Algorithms CSCI 235, Spring 2019 Lecture 7 Recurrences II
Algorithms CSCI 235, Spring 2019 Lecture 6 Recurrences
CMPT 120 Lecture 16 – Unit 3 – Graphics and Animation
CMPT 120 Lecture 19 – Unit 3 – Graphics and Animation
Lecture 6 - Recursion.
Presentation transcript:

1 Survey of Computer Science CSCI 110, Spring 2011 Lecture 14 Recursion

2 Recall Divide and Conquer Draw City Draw Building Draw Streets Draw Park Draw Outline Draw Doors Draw Windows Repeat Buildings

3 Recursion Count votes Count 5000 votes Count 2500 votes When the subproblems are smaller versions of the original problem, we can use recursion to solve the problem.

4 Example Draw stars How to draw n stars using recursion: 1) Draw 1 star 2) Draw (n - 1) stars The first step is trivial, and we can simply write a line of code to do it. The second step is a smaller version of the original problem. A function that can draw n stars can also draw n-1 stars.

5 Diagramming drawStars Draw n stars Draw 1 starDraw n-1 stars Draw 1 starDraw n-2 stars... Draw 0 stars

6 DrawStars in Python #program drawing.py import sys def drawStars( n ): if n < = 0: sys.stdout.write("") #do nothing else: sys.stdout.write("*") drawStars( n - 1) #Begin main program here drawStars(2) Base Case General Case

7 Execution of drawStars dS(2) n if n <=0:... else: sys.stdout.write("*") drawStars(n-1) n if n <=0:... else: sys.stdout.write("*") drawStars(n-1) n if n <=0: sys.stdout.write("") #do nothing else:...

8 Recursive function calls A recursive call is a function call in which the called function is the same as the one making the call. In other words, recursion occurs when a function calls itself! But we need to avoid making an infinite sequence of function calls (infinite recursion).

9 Finding a recursive solution A recursive solution to a problem must be written carefully. The idea is for each successive recursive call to bring you one step closer to a situation in which the problem can easily be solved. This easily solved situation is called the base case. Each recursive algorithm must have at least one base case, as well as the general case.

10 General form for recursion if some easily-solved condition: # base case solution statement(s) else: # general case recursive function call

11 Example: Factorial Definition of Factorial n! = 1 * 2 * 3 * 4 *... * (n-1) * n; 0! = 1 In other words: fact(n) = 1, n = 0 n * fact( n-1), n > 0 fact( n - 1) {

12 Writing Factorial in Python def factorial (n): #we will write this code in class.

13 Execution of factorial fact(2) n if n <= 0: return 1 else: return n * fact(n - 1) n if n <= 0: return 1 else: return n * fact(n - 1) n if n <= 0: return 1 else: return n * fact(n - 1)

14 Invocation tree for factorial Fact( 3) : Fact( 2) : Fact( 1) : Fact( 0) : Trace of Factorial (we will do in class) nFact(n - 1)Fact( n )

15 Rabbit populations: Fibonacci

16 Fibonacci in Python def fib (n): #we will write this code in class.

17 Fibonacci Execution tree

18 How long will this take? For large numbers, each time you increment by 1, it takes about twice as long to compute the Fibonacci result: Fib(32) Fib(31)Fib(30) Suppose our computer can process 10 9 operations per second. How long will it take to compute Fib(100)? (Approximate answer will be given in class).

19 Classes and Objects Python is an object oriented programming language. All items of data are objects. Objects have properties: Things that describe the objects Objects have methods: Things an object can do. A class describes a group of objects that have the same methods and set of properties. Example: A car class Properties: color, number of doors, body type Methods: accelerate, brake, steer My car is an object that is a member of the car class.

20 Using Pre-defined classes Python has many libraries with pre-defined classes that we can use in our programs. Example: The Turtle( ) class. A turtle in python is an object from the Turtle class. Properties: x position, y position, direction, pen position, etc. Methods: forward(distance), backward(distance), left(angle), etc. yertle = Turtle( )#Create a turtle object, identified as yertle yertle.forward(150)#Call the forward( ) method for yertle.

21 Writing a Class definition class Critter: color = "red"#color is a property mood = "happy"#mood is another property def talk(self):#talk is a method print "I am a critter!" def sing(self):#sing is another method print "Tra la la" #main program starts here crit = Critter( ) print crit.color crit.talk( ) crit.sing( )

22 Using self in a class method class Critter: color = "red" mood = "happy" def talk(self): print "I am a " + self.mood + " critter!" crit = Critter( ) crit.talk( ) We can use self to refer to a given object in a class method. Example:

23 The docstring The docstring is used to describe the objects in a class. Example: class Critter: """A virtual pet"""... crit = Critter( ): print crit.__doc__

24 The constructor function We can use a constructor function to initialize properties. Example: class Critter: """A virtual pet""" def __init__(self, clr, md): self.color = clr self.mood = md def talk(self): print "I am a " + self.mood + " critter!" crit = Critter("green", "sad") print crit.color crit.talk( )