David Stotts Computer Science Department UNC Chapel Hill.

Slides:



Advertisements
Similar presentations
C++ Programming:. Program Design Including
Advertisements

New Mexico Computer Science For All
David Stotts Computer Science Department UNC Chapel Hill.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Recursion.
1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Recursion. Binary search example postponed to end of lecture.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
1 Recursion  Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems  Chapter 11 of the book.
Imperative programming public int factorial (int N){ int F = 1; for(X=N; X>1; X-- ){ F= F*X; } return F; } Functional programming (defun factorial(N) (cond.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Recursion.
Topic 7 – Recursion (A Very Quick Look). CISC 105 – Topic 7 What is Recursion? A recursive function is a function that calls itself. Recursive functions.
Fall 2009ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself.
Unit 181 Recursion Definition Recursive Methods Constructing Recursion Benefits and Usage Infinite Recursion Recursion Removal Examples Exercises.
CS 211 RECURSION TREES. Trees versus Linked Lists A tree is like a linked list, except instead of a single next node, it can have multiple next nodes.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Data Structures Using C++ 2E Chapter 6 Recursion.
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Data Structures Using C++ 2E Chapter 6 Recursion.
Recursion Fall 2008 Dr. David A. Gaitros
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
M180: Data Structures & Algorithms in Java
Fall Week 3 CSCI-141 Scott C. Johnson.  Say we want to draw the following figure ◦ How would we go about doing this?
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
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.
Recursion AP Computer Science A Mr. Langner By: Thomas Robbins.
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.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
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.
Chapter 8 Recursion Modified.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
1 Wright State University, College of Engineering Dr. T. Doom, Computer Science & Engineering CS 241 Computer Programming II CS 241 – Computer Programming.
David Stotts Computer Science Department UNC Chapel Hill.
David Stotts Computer Science Department UNC Chapel Hill.
David Stotts Computer Science Department UNC Chapel Hill.
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 6 Recursion.
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.
David Stotts Computer Science Department UNC Chapel Hill.
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
Lecture 7. Solution by Substitution Method T(n) = 2 T(n/2) + n Substitute n/2 into the main equation 2T(n/2) = 2(2(T(n/4)) + n/2) = 4T(n/4) + n And T(n)
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Nonvisual Arrays by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout
R ECURRSION Prepared by Miss Simab Shahid Lecturer computer Science and Software Engineering department, University of Hail Chapter.
David Stotts Computer Science Department UNC Chapel Hill.
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
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.
Welcome to Recursion! Say what?!? Recursion is… the process of solving large problems by simplifying them into smaller ones. similar to processing using.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Recursion DRILL: Please take out your notes on Recursion
Data Structures and Analysis (COMP 410)
Introduction to Computer Science - Alice
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Functions Recursion CSCI 230
CS148 Introduction to Programming II
Lecture 6 - Recursion.
Presentation transcript:

David Stotts Computer Science Department UNC Chapel Hill

0. data (types, simple information) 1. data storage (variables, assignment) 2. data retrieval (expressions, evaluation) 3. repetition (loops) 4. decision making (conditionals) 5. procedure abstraction (functions) 6. data abstraction (arrays) 7. objects: all-the-above, wrapped up

 We are by now familiar with calling a function to get some work done distance = calcMiles ( speed, time );  One function can call another function to help get its own job done  We are already doing this… myMain function calls your other functions

function myMain() { var result = cube(4) ; alert(result); return result; } function cube (n) { return n*n*n ; } myMain function is called when button clicked cube function is called by myMain cube returns alert function is called by myMain alert returns

function calls create a “tree” of memory maps (“root” at top, “leaves” at the bottom) myMain ( ) alert() cube() call (from button) return (to HTML page) call return call Cube memory map goes away alert memory map goes away

function myMain() { var result = mash(3) ; alert(result); return result; } function mash (n) { return cube(2*n) + 5; } function cube (n) { return n*n*n ; } button calls myMain myMain calls mash mash calls cube cube returns then mash returns 3 active function calls, 3 active memory maps

myMain ( ) mash() alert() cube() call (from button) return (to HTML page) call return call return call Cube memory map goes away mash memory map goes away alert memory map goes away

Ok, it’s brain bending time Can a function call itself ?  Yes it can, and it is very useful  A function that calls itself is “recursive”  Computing with a self-calling function is called “recursion”

 Root of “recursive” is “occur again”  A recursive function has a call to its own name in its code body function doTask ( n ) { … x = doTask (n-1); // parameter is smaller … return result; }

Computer scientists love recursion. We are twisted people. Easy to entertain. It has been known for programming books to include a joke entry in their index like this: Recursion, see Recursion. Page 269, index of C language manual says recursion 86, 139, 141, 182, 202, 269

In our favored version, an Eastern guru affirms that the earth is supported on the back of a tiger. When asked what supports the tiger, he says it stands upon an elephant; and when asked what supports the elephant he says it is a giant turtle. When asked, finally, what supports the giant turtle, he is briefly taken aback, but quickly replies "Ah, after that it is turtles all the way down." Antonin Scalia, footnote in Rapanos vs. United States, 2006 A well known “myth” recursive tall turtle tale

"What is reflected in a mirror which is reflected in a mirror?"

This is infinite recursion… Not quite what we want in programming

myMain calls doTask calls doTask …. its turtles all the way down the calling has to stop somehow… or the program will run forever

We like finite We need the recursion to end at some point We need an armadillo in the pile

We write a recursive function by following a very specific pattern base case, then recursive case Base case: the problem is so small the solution is easy with no recursive call. Base case ends the recursion Base case is our armadillo Recursive case: we call the function again but the parameter makes a “smaller” problem. Recursion stops when the smaller problem becomes the base case

Classic recursive definition… has base case and recursive case… “recurrence equation” in math fact(1) = 1 (the base case) fact(2) = 2 * 1 = 2 * fact(1) fact(3) = 3 * ( 2 * 1 ) = 3 * fact(2) fact(4) = 4 * 3 * ( 2 * 1 ) = 4 * fact(3) etc. In general fact(n) = 1, if n is 1 base case n * fact ( n-1 ), otherwise recurrence

Coding pattern reflects the parts of the definition function factorial ( n ) { if (n==1) return 1 ; // base case // the armadillo at the bottom else { // the recursive call // the parameter must be smaller // we solve a smaller problem, then use // that result to solve the current problem return n* factorial ( n - 1 ) ; } }

Coding pattern reflects the parts of the definition function sum ( n ) { if (n==1) return 1 ; // base case, the armadillo else { return n + sum ( n - 1 ) ; } } function sum ( n ) { if (n==1) return 1; if (n==2) return 3; if (n==3) return 6; return n+sum(n-1); }

Coding pattern reflects the parts of the definition function sum ( n ) { switch (n) { case 1: return 1; // armadillo case 2: return 3; // another armadillo case 3: return 6; // dillo default: return n + sum(n-1); // recursion } }

Recursion is a notational convenience It does not create new computational power Any loop can be expressed as a recursion and… Any recursion can be expressed as a loop

function sum ( n ) { // non recursive var acc = 0; for (var i=1; i<=n; i++) { acc = acc + i ; } return acc; } function sum ( n ) { // recursive if (n==1) return 1 ; return n + sum(n-1) ; }

function factorial ( n ) { // non recursive var acc = 1; for (var i=1; i<=n; i++) { acc = acc * i ; } return acc; } function factorial ( n ) { // recursive if (n==1) return 1 ; return n * factorial(n-1) ; }