Chapter 16 Recursion: Another Control Mechanism. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. a.

Slides:



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

Recursion - see Recursion. RHS – SOC 2 Recursion We know that: –We can define classes –We can define methods on classes –Mehtods can call other methods.
Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: Recursion.
Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate.
Chapter 10 Recursion Instructor: alkar/demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: Recursion.
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.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
Data Structures Using C++ 2E Chapter 6 Recursion.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Data Structures Using C++ 2E Chapter 6 Recursion.
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
1 Decrease-and-Conquer Approach Lecture 06 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms.
Data Structures and Abstractions with Java, 4e Frank Carrano
Recursion.
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
Recursion Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zWhat is a recursive function?
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.
CMPS 1371 Introduction to Computing for Engineers RECURSION.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
CSIS 123A Lecture 9 Recursion Glenn Stevenson CSIS 113A MSJC.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 6 Recursion.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
IB Computer Science Unit 5 – Advanced Topics Recursion.
Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 15 Recursion.
1 Programming for Engineers in Python Autumn Lecture 8: Recursion.
Functions. functions: a collection of lines of code with a name that one can call. Functions can have inputs and outputs.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
Recursively Defined Sequences Lecture 40 Section 8.1 Wed, Apr 11, 2007.
Program Development and Design Using C++, Third Edition
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Recursion Chapter 10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Chapter 9 Recursion. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe recursive function is –a.
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
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
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Recursion Lakshmish Ramaswamy.
Introduction to Recursion
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
CMSC201 Computer Science I for Majors Lecture 16 – Recursion
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
RECURSION Annie Calpe
Recursion: The Mirrors
Recursion Taken from notes by Dr. Neil Moore
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.
CMPT 120 Lecture 18 – Unit 3 – Graphics and Animation
Presentation transcript:

chapter 16 Recursion: Another Control Mechanism

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. a function that calls itself The very basic meaning of a recursive function is a function that calls itself Leads to some funny definitions: –Def: recursion. see recursion However, when you first see it, it looks odd. Look at the recursive function that calculates factorial (code listing 16-2)

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. It doesn’t do anything! This is a common complaint when one first sees a recursive function. What exactly is it doing? It doesn’t seem to do anything! Our goal is to understand what it means to write a recursive function from a programmers and computers view.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Defining a recursive function

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. 1) divide and conquer Recursion is a natural outcome of a divide and conquer approach to problem solving A recursive function defines how to break a problem down (divide) and how to reassemble (conquer) the sub-solutions into an overall solution

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. 2) base case recursion is a process not unlike loop iteration. –You must define how long (how many iterations) recursion will proceed through until it stops The base case defines this limit Without the base case, recursion will continue infinitely (just like an infinite loop)

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Simple Example Lao-Tzu: “A journey of 1000 miles begins with a single step” def journey (steps): –the first step is easy (base case) –the n th step is easy having complete the previous n-1 steps (divide and conquer)

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Code Listing 16-1

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Factorial You remember factorial? factorial(4) = 4! = 4 * 3 * 2 * 1 The result of the last step, multiply by 1, is defined based on all the previous results that have been calculated

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Code Listing 16-2

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Trace the calls 4! = 4 * 3! start first function invocation 3! = 3! * 2 start second function invocation 2! = 2 * 1! start third function invocation 1! = 1 fourth invocation, base case 2! = 2 * 1 = 2 third invocation finishes 3! = 3 * 2 = 6 second invocation finishes 4! = 4 * 6 = 24 first invocation finishes

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Another: Fibonacci Depends on the Fibonacci 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)

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Code Listing 16-3

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. 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) = = 5

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Reverse string We know Python has a very simple way to reverse a string, but let’s see if we can write a recursive function that reverses a string without using slicing.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Code Listing 16-4 Template for reversal

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Base Case What string do we know how to trivially reverse? Yes, a string with one character, when reversed, gives back exactly the same string We use this as our base case

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Code Listing 16-5

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. def reverser (aStr): if len(aStr) == 1: # base case return aStr # recursive step # divide into parts # conquer/reassemble theStr = raw_input("Reverse what string: ") result = reverser(theStr) print ”Reverse of %s is %s\n" % (theStr,result)

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Recursive step We must base the recursive step on what came before, plus the extra step we are presently in. Thus the reverse of a string is the reverse of all but the first character of the string, which is placed at the end. We assume the rev function will reverse the rest rev(s) = rev(s[1:]) + s[0]

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Code Listing 16-6

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. How does Python keep track?

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. 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

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. 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

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. 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

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Code Listing 16-7

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. >>> factorial(4) Enter factorial n = 4 Before recursive call f(3) Enter factorial n = 3 Before recursive call f(2) Enter factorial n = 2 Before recursive call f(1) Enter factorial n = 1 Base case. After recursive call f(1) = 1 After recursive call f(2) = 2 After recursive call f(3) = 6 24

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Recursive Figures

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Fractal Objects You can use recursion to draw many real world figures Fractals are a class of drawing that has a couple of interesting properties: –upon magnification, the “shape” of the figure remains the same –the resulting figure has a floating point dimensionality value (2.35 D for example)

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Algorithm to draw a tree 1.Draw an edge 2.turn left 3.draw edge and left branch # recurse 4.turn right 5.draw edge and right branch #recurse 6.turn left 7.backup

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Code Listing 16-8

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Couple things note that length is reduced as we recurse down (making for shorter branches) the numbers on the right of the following picture show the order in which the branches are drawn

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Sierpinski Triangles Little simpler than the tree: 1.draw edge 2.recurse 3.backward 4.turn 120 degrees

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Code Listing 16-9 Sierpinski Triangles

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Some recusive details Recursive functions are easy to write and lend themselves to divide and conquer They can be slow (all the pushing and popping on the stack) Can be converted from recursive to iterative but that can be hard depending on the problem.