Recursion https://sites.google.com/a/cabrillo.edu/cs-11m/schedule/recursion.

Slides:



Advertisements
Similar presentations
Introduction to Recursion and Recursive Algorithms
Advertisements

Recursion (define tell-story (lambda () (print ‘’Once upon a time there was a mountain. ‘’) (print ‘’On the mountain, there was a temple. ‘’) (print ‘’In.
Recursion.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: Recursion.
Computer Science II Recursion Professor: Evan Korth New York University.
Recursion. Binary search example postponed to end of lecture.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: Recursion.
Recursion Road Map Introduction to Recursion Recursion Example #1: World’s Simplest Recursion Program Visualizing Recursion –Using Stacks Recursion Example.
16/23/2015 9:48 AM6/23/2015 9:48 AM6/23/2015 9:48 AMRecursion Recursion Recursion is when a function calls itself to implement an algorithm. Really a paradigm.
Recursion.
Topic 9 Introduction to Recursion
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.
Unit 181 Recursion Definition Recursive Methods Constructing Recursion Benefits and Usage Infinite Recursion Recursion Removal Examples Exercises.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Recursion AP Computer Science A Mr. Langner By: Thomas Robbins.
1Recursion. 2 Outline thinking recursively recursive algorithms iteration vs. recursion recursive functions integer exponentiation (pow) infinite recursion.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
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.
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
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.
Building Java Programs Chapter 12 Recursion Copyright (c) Pearson All rights reserved.
CSE 143 Lecture 10 Recursion reading: slides created by Marty Stepp and Hélène Martin
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Analysis of Algorithms & Recurrence Relations. Recursive Algorithms Definition –An algorithm that calls itself Components of a recursive algorithm 1.Base.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
CSE 143 Lecture 9: introduction to recursion reading: 12.1.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Programming with Recursion
Recursion.
CS314 – Section 5 Recitation 9
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Recursion.
Recursion Data Structure Submitted By:- Dheeraj Kataria.
Recursion CENG 707.
Recursion CSE 2320 – Algorithms and Data Structures
Recursion Salim Arfaoui.
Recursion CENG 707.
Lecture 24: Recursion Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Lecture 9: introduction to recursion reading: 12.1
Introduction to Recursion
Introduction to Recursion
Building Java Programs
Recursion CSE 2320 – Algorithms and Data Structures
Topic 12 Introduction to Recursion
Programming with Recursion
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Recursion Copyright (c) Pearson All rights reserved.
Applied Algorithms (Lecture 17) Recursion Fall-23
slides adapted from Marty Stepp and Hélène Martin
Adapted from slides by Marty Stepp, Stuart Reges & Allison Obourn.
Recursion Data Structures.
7.Recursion Recursion is the name given for expression anything in terms of itself. Recursive function is a function which calls itself until a particular.
slides created by Marty Stepp and Alyssa Harding
Lecture 14: Strings and Recursion AP Computer Science Principles
Lecture 19: Recursion Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Recursion.
Building Java Programs
Yan Shi CS/SE 2630 Lecture Notes
slides adapted from Marty Stepp and Hélène Martin
Building Java Programs
slides created by Marty Stepp
Building Java Programs
Recursion.
Recursive Function Prepared by Harprith ICT2102 Introduction to Data Structure.
Lecture 6 - Recursion.
Presentation transcript:

Recursion https://sites.google.com/a/cabrillo.edu/cs-11m/schedule/recursion

Recursion Recursion: Definition of operation in terms of itself Solve a problem in terms of the solution to smaller occurrences of same problem Recursive functions: functions that call themselves Substitute for iteration Suited to certain types of problems

Why Recursion? A different way of thinking about problems A better way of solving some problems than iteration Can produce elegant, short code Some functional programming languages (Scheme, Haskell) use recursion exclusively – no loops

How many people are in this column? Exercise For student in front row: How many students total are directly behind you in the classroom (i.e., in your column in the room)? Assumptions: Your vision is poor. You cannot look back and count. But you can ask questions of people sitting next to you (in any direction.) How do we solve this problem? How many people are in this column?

Exercise Recursion involves breaking a big problem into smaller occurrences of the same problem. Each person can solve a small part of the problem. What's a small version of the problem that's easy to answer? What information might the person behind me provide? Hey, behind me, can you help me? Hey, behind me, can you help me? Hey, behind me, can you help me?

Exercise To find the number of people behind me: If there's a person behind me: ask how many people are behind him/her. If that person answers N, then my answer is N+1 If there's nobody behind me, I answer 0.

Recursive Cases A recursive algorithm has at least two cases base case: a simple occurrence that can be answered directly recursive case: an occurrence of the problem that is answered in terms of smaller occurrences of the problem There may be more than one base or recursive case.

Writing Recursive Functions Base case: Always have at least one case that is solved directly without using recursion Know when to stop Make progress: Any recursive call must progress towards base case. Call function on smaller occurrence of the problem You "gotta have faith." Always assume that the recursive call works. (And of course, design and test to ensure that it does!) Note: A recursive solution solves a small occurrence of the problem directly, and leaves the rest of the problem in the same form as the original.

N! The classic first recursion example Example: 5! = 5*4*3*2*1 = 120 Recursive definition: 0! = 1 base case N! = N * (N-1)! recursive case Iterative function: int fact(int n) { int res = 1; for(int i = 2; i <= n; i++) { res *= i; } return res;

Recursive Factorial Recursive definition of factorial: 0! = 1 N! = N * (N-1)! // pre: n >= 0 int fact(int n) { if(n == 0) return 1; else return n * fact(n-1); }

Program Stack For every recursive call, an activation record is pushed onto the runtime stack Every recursive call must simplify the computation in some way To compute fact(4) fact(4) calls fact(3) fact(3) calls fact(2) fact(2) calls fact(1) fact(1) calls fact(0) fact(0) returns 1 fact(1) returns 1 * 1 = 1 fact(2) returns 1 * 2 = 2 fact(3) returns 2 * 3 = 6 fact(4) returns 6 * 4 = 24 n = 0 return 1 to caller n = 1 return 1 * fact(0) = 1 * 1 to caller n = 2 return 2 * fact(1) = 2 * 1 to caller n = 3 return 3 * fact(2) = 3 * 2 to caller n = 4 return 4 * fact(3) = 4 * 3 to caller

Evaluate Recursive Functions int mystery(int n) { if (n == 0) return 1; else return 3 * mystery(n-1); } mystery(5) returns ? mystery(5) = 3 * mystery(4) = 3 * 3 * mystery(3) = 3^3 * mystery(2) = 3^4 * mystery(1) = 3^5 * mystery(0) = 3^5 * 1 = 3^5

Evaluate Recursive Functions Draw the program stack: m(5) = 3 * m(4) m(4) = 3 * m(3) m(3) = 3 * m(2) m(2) = 3 * m(1) m(1) = 3 * m(0) m(0) = 1  3^5 = 243

Multiple Recursive Calls int bar(int n) { if (n <= 0) return 2; else return 3 + bar(n-1) + bar(n-2); } What does bar(5) return? A. 2 B. 5 C. 13 D. 62 E. 127

Evaluating Recursive Functions What's returned by bar(5)? b(5) = 3 + b(4) + b(3) b(4) = 3 + b(3) + b(2) b(3) = 3 + b(2) + b(1) b(2) = 3 + b(1) + b(0) b(1) = 3 + b(0) + b(-1) b(0) = 2 b(-1) = 2 int bar(int n) { if (n <= 0) return 2; else return 3 + bar(n-1) + bar(n-2); }

Evaluating Recursive Functions What's returned by bar(5)? b(5) = 3 + b(4) + b(3) b(4) = 3 + b(3) + b(2) b(3) = 3 + b(2) + b(1) b(2) = 3 + b(1) + b(0) // substitute in results b(1) = 3 + 2 + 2 = 7 b(0) = 2 b(-1) = 2 int bar(int n) { if (n <= 0) return 2; else return 3 + bar(n-1) + bar(n-2); }

Evaluating Recursive Functions What's returned by bar(5)? b(5) = 3 + b(4) + b(3) b(4) = 3 + b(3) + b(2) b(3) = 3 + b(2) + b(1) b(2) = 3 + 7 + 2 = 12 b(1) = 7 b(0) = 2 b(-1) = 2 int bar(int n) { if (n <= 0) return 2; else return 3 + bar(n-1) + bar(n-2); }

Evaluating Recursive Functions What's returned by bar(5)? b(5) = 3 + b(4) + b(3) b(4) = 3 + b(3) + b(2) b(3) = 3 + 12 + 7 = 22 b(2) = 12 b(1) = 7 b(0) = 2 b(-1) = 2 int bar(int n) { if (n <= 0) return 2; else return 3 + bar(n-1) + bar(n-2); }

Evaluating Recursive Functions What's returned by bar(5)? b(5) = 3 + b(4) + b(3) b(4) = 3 + 22 + 12 = 37 b(3) = 22 b(2) = 12 b(1) = 7 b(0) = 2 b(-1) = 2 int bar(int n) { if (n <= 0) return 2; else return 3 + bar(n-1) + bar(n-2); }

Evaluating Recursive Functions What's returned by bar(5)? b(5) = 3 + 37 + 22 = 62 b(4) = 37 b(3) = 22 b(2) = 12 b(1) = 7 b(0) = 2 b(-1) = 2 int bar(int n) { if (n <= 0) return 2; else return 3 + bar(n-1) + bar(n-2); }

Recursion Practice Write a function raiseToPower(int base, int power) // pre: power >= 0 Simple or tail recursion: recursive function call occurs at end of function int raiseToPower(int base, int power) { if(n == 0) return 1; else return base * raiseToPower(base, power-1); }

Recursion Practice Here's an iterative function that prints a line of asterisks: // Print a line of n stars // pre: n >= 1 void printStars(int n) { for(int i = 0; i < n; i++) { putchar("*"); } printf("\n"); Write a recursive version of this function. Your function should print only one star at a time. What's the base case? What's the recursive case? base case: print one star (n == 1) recursive case: (n > 1) print one star, call printStars(n-1) void printStars(int n) { if(n ==1) {putchar("*"); putchar("\n");} else { putchar("*"); printStars(n-1); }

Recursion Practice int mystery(int n) { if(n < 10) return n; else { int a = n / 10; int b = n % 10; return mystery(a + b); } What is returned by mystery(648)? mystery(648): a = 64, b = 8 return mystery(72): a = 7, b = 2 return mystery(9): return 9

Recursion Recursion is a tool. It is not a good tool for all problems. We'll implement several algorithms and functions where an iterative solution would work fine. After learning how recursion works, the real skill is knowing when to use it

Big-O and Recursion Determining big-O of recursive functions: tricky T(N), the actual run time, is a recurrence relation. For fact() function: T(N) = T(N-1) + O(1) There are N steps T(N) = O(N) int fact(int n) { if(n == 0) return 1; else return n * fact(n-1); }

Common Recurrence Relations T(N) = T(N/2) + O(1)  O(logN) binary search T(N) = T(N-1) + O(1)  O(N) sequential search, factorial T(N) = T(N-1) + T(N-1) + O(1)  O(2^N) Fibonacci