Recursion –a programming strategy for solving large problems –Think “divide and conquer” –Solve large problem by splitting into smaller problems of same.

Slides:



Advertisements
Similar presentations
Introduction to Recursion and Recursive Algorithms
Advertisements

Recursion yet another look To recurse is divine, to iterate is human.
Recursion, Divide and Conquer Lam Chi Kit (George) HKOI2007.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming 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.
ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can: Design recursive algorithms to solve.
Circular Arrays Neat trick: use a circular array to insert and remove items from a queue in constant time The idea of a circular array is that the end.
RECURSION Lecture 6 CS2110 – Fall Recursion Overview 4  Recursion is a powerful technique for specifying functions, sets, and programs 
Recursion Chapter 8. 2 Chapter Contents What Is Recursion? Tracing a Recursive Method Recursive Methods That Return a Value Recursively Processing an.
Recursion. Binary search example postponed to end of lecture.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Discrete Mathematics Recursion and Sequences
Recursion!. Can a method call another method? YES.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion.  Identify recursive algorithms  Write simple recursive algorithms  Understand recursive function calling  With reference to the call stack.
RECURSION Lecture 7 CS2110 – Spring Overview references to sections in text 2  Note: We’ve covered everything in JavaSummary.pptx!  What is recursion?
A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
1 Recursion Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Recursion Recursion is a math and programming tool –Technically, not necessary Advantages of recursion –Some things are very easy to do with it, but difficult.
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.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation.
CIS 068 Welcome to CIS 068 ! Stacks and Recursion.
Sudeshna Sarkar, IIT Kharagpur 1 Functions : Recursion Lecture
ICS220 – Data Structures and Algorithms Dr. Ken Cosh Week 5.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
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.
CMPT 225 Recursion. Objectives Understand how the Fibonacci series is generated Recursive Algorithms  Write simple recursive algorithms  Analyze simple.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
1 Recursion Recursive definitions Recursive methods Run-time stack & activation records => Read section 2.3.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
7. RECURSIONS Rocky K. C. Chang October 12, 2015.
Ramamurthy Recursion: The Mirrors B. Ramamurthy CS114A,B.
Pei Zheng, Michigan State University 1 Chapter 8 Recursion.
1 Recursion. 2 A process by which a function calls itself repeatedly  Either directly. X calls X  Or cyclically in a chain. X calls Y, and Y calls X.
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.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
1 Recursion and induction We teach these early, instead of new object- oriented ideas, so that those who are new to Java can have a chance to catch up.
递归算法的效率分析. When a function is called... A transfer of control occurs from the calling block to the code of the function --It is necessary that there be.
Recursion Powerful Tool
Recursion.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Applied Algorithms (Lecture 17) Recursion Fall-23
Recursion Recursion is a math and programming tool
CS201: Data Structures and Discrete Mathematics I
Recursion.
Module 1-10: Recursion.
Recursion: The Mirrors
Java Programming: Chapter 9: Recursion Second Edition
Recursion yet another look To recurse is divine, to iterate is human
Dr. Sampath Jayarathna Cal Poly Pomona
Recurrences.
ICS103 Programming in C Lecture 11: Recursive Functions
Presentation transcript:

Recursion –a programming strategy for solving large problems –Think “divide and conquer” –Solve large problem by splitting into smaller problems of same kind Induction –A mathematical strategy for proving statements about large sets of things Now we learn recursion…

Recursion Recursive definition 1 : See recursive definition. Weiss: ch Free On-line Dictionary of Computing (FOLDOC)

Recursive Definitions (recap) S(n) = S(n-1) + nS(0) = … + n Q(n) = Q(n-1) + n 2 Q(1) = … + n 2 Tile(2 N by 2 N ) : Place “L” in center; Then tile each quadrant Tile(2 by 2) : easy Tile entire grid minus one square C(n) = C(n – 1) + 1C(0) = 5

Recursion in Detail: Permutations How many ways to put 4 ducks in a row? (4 very unique ducks)

Factorial How many ways to arrange n distinct objects? –Call it fact(n) –Write it n! –Say it “n factorial” One definition (closed form): –fact(n) = 1*2*3*…*n = n! Another (recursive form): –fact(1) = 1 –fact(n) = fact(n-1) * n, for n > 1 By convention: –fact(0) = 1

Code static int fact(int n) { if (n = = 0) return 1; else return fact(n –1) * n; }

Executing fact(4) n = 4 time n = 4 n = 3 n = 4 n = 3 n = 2 n = 1 n = 4 n = 3 n = 2 n = 4 n = 3 n = 2 n = 1 n = 0 n = 1 n = 4 n = 3 n = 2 n = 4 n = 3 n = 2 n = 4 n = 3 n = fact(4) fact(3) fact(2) fact(1) fact(0)

Executing fact(4) Alternative view: call tree fact(4) fact(3) fact(2) fact(1) fact(0)

Writing Recursive Functions 1.Find a parameter n such that solution for large n can be obtained from solution(s) for smaller n. 2.Find base cases: small values of n which you can solve directly, easily. 3.Verify that for any n, it will eventually reduce to a base case (or cases). 4.Write code.

Fibonacci A problem in the third section of Liber abaci led to the introduction of the Fibonacci numbers and the Fibonacci sequence for which Fibonacci is best remembered today 1 : Fibonacci sequence –A certain man put a pair of rabbits in a place surrounded on all sides by a wall. How many pairs of rabbits can be produced from that pair in a year if it is supposed that every month each pair begets a new pair which from the second month on becomes productive? 1 Taken from

Fibonacci Definition (recursive form): fib(0) = 1 fib(1) = 1 fib(n) = fib(n-1) + fib(n-2) Closed form? See HW. Example: 1, 1, 2, 3, 5, 8, 13, 21, … Code:static int fib(int n) { if (n == 0 || n == 1) return 1; else return fib(n-1) + fib(n-2); } Base cases }

Call Tree for fib(4) fib(4) fib(3) fib(2)fib(1) fib(0) fib(2) fib(1)fib(0) Note: this is called a tree because it has a root, some leaves, and it branches

Inefficient! fib(4) fib(3) fib(2)fib(1) fib(0) fib(2) fib(1)fib(0) fib(2) calculated twice

A More Complex Example: Combinations How many ways can you pick r items from a set of n distinct elements? –Call it n C r Ex. Pick two letters from set S = {A, B, C, D, E}

u A More Complex Example: Combinations How many ways can you pick r items from a set of n distinct elements? –Call it n C r Ex. Pick two letters from set S = {A, B, C, D, E} Answer: {A, B}, {B, C}, {B, D}, {B, E} {A, C}, {A, D}, {A, E}, {C, D}, {C, E}, {D, E} So 10 ways to choose. Recurrence relation?

Combinations Theorem: n C r = n-1 C r + n-1 C r-1 for n > r > 0(recursive case) n C n = 1(base case) n C 0 = 1(base case) Proof: (reason it out in three cases) Theorem: n C r = n! / r!(n-r)! Proof: (induction, or reason it out)

Sanity Check Do all cases reduce to base cases? Example: 6 C 3 = 5 C C 2 = … = ? n r … …012345… 0C00C0 1C01C0 2C02C0 3C03C0 4C04C0 5C05C0 1C11C1 2C12C1 3C13C1 4C14C1 5C15C1 2C22C2 3C23C2 4C24C2 5C25C2 3C33C3 4C34C3 5C35C3 4C44C4 5C45C4 6C36C3 note: we call this a graph

Code static int comb(int n, int r) { if (r == 0 || n == r) return 1; else return comb(n-1, r) + comb(n-1, r-1); }

y Somewhat inefficient code… Call tree: comb(5, 3) comb(4, 2) comb(4, 3) comb(3, 2) comb(3, 3) comb(3, 1) comb(3, 2) comb(3, 1) comb(6, 3) comb(5, 2) comb(4, 1) comb(3, 1)comb(4, 1) comb(4, 2)comb(3, 2) comb(3, 1) !

Efficiency Rule #1: Avoid duplicate work in recursive calls –e.g. iterative version of fib(n) is faster than recursive Other goals: –choose sub-problems carefully e.g. divide and conquer works best when we “divide” roughly evenly: 16  8  4  2  1 versus 16  15  14  13  12  11  10…  3  2  1 –choose base cases carefully e.g., can stop recursion early for special cases

Case Study Computing powers –pow(x, n) = x * x * … * x = x n

First Attempt Recursive form: x 0 = 1 x n = x n-1 * x, for n > 0 Code static int pow(int x, int n) { if (n == 0) return 1; else return pow(x, n-1) * x; }

Efficiency Duplicate work in recursive calls? How much “work”? –Number of calls to pow()? –Number of multiplications?

Second Attempt Recursive form: x 0 = 1 x n = (x n/2 ) 2, for n > 0, n even x n = x*(x n/2 ) 2, for n > 0, n odd Code static int pow(int x, int n) { if (n == 0) return 1; int halfPower = pow(x, n/2); if (n/2 == n) return halfPower * halfPower; else return x * halfPower * halfPower; }

Efficiency Duplicate work in recursive calls? How much “work”? –Number of calls to pow()? –Number of multiplications?

Recursion: Implementation Details Methods in Java (or C, or Pascal, or …): –method-local variables stored in stack area –each invocation gets a frame on the stack –frame contains: parameters and locals –… and return value (eventually) The call stack: –frames pushed on top of stack, then popped off –“current” method computation is at top of stack

Managing the Call Stack Non-recursives tatic void main(String args[]) { example: int a = 5; int b = 3; int c = Math.max(a, b); }

s tatic void main(String args[]) { int a = 5; int b = 3; int c = Math.max(a, b); } a = 5 b = 3 c = undef. args = … As main() is called In main() a = 5 b = 3 c = undef. args = … As max() is called x = 5 s tatic int max(int x, int y) { return x > y ? x : y; } y = 3 a = 5 b = 3 c = undef. args = … In max() x = 5 y = 3 a = 5 b = 3 c = undef. args = … As max() returns 5 a = 5 b = 3 c = 5 args = … After max()

Recursive Calls Call stack details for pow(2, 5) static int pow(int x, int n) { if (n == 0) return 1; else return pow(x, n-1)*x; }

x=5 static int pow(int x, int n) { if (n == 0) return 1; else return pow(x, n-1)*x; } pow(5, 3); n=3 x=5 n=3 x=5 n=2 x=5 n=3 x=5 n=2 x=5 n=1 x=5 n=3 x=5 n=2 x=5 n=1 x=5 n=0 x=5 n=3 x=5 n=2 x=5 n=1 1 x=5 n=3 x=5 n=2 5 x=5 n=

Questions What location does x refer to in pow()? –Is it “statically” determined, or “dynamically”? How to keep track of which frame is “active”? –Always the top-most one –So keep a pointer to top, move up and down as needed. Call it the Stack Pointer (SP). –Also keep a pointer to “frame base”, for debugging and efficiency. Call this the Frame Base Register (FBR) –All low-level machine-code relative to FBR or SP. How to know how big frame should be? –Static: compiler counts number of locals + params in the method Java is type-safe –Static: compiler tracks which slots have ints, doubles, Strings, etc. –e.g., it can catch all type errors at compile time