Recursion. Overview. Mathematical induction. Hello recursion. Recursion. Example applications. Goal: Compute factorial N! = 1 * 2 * 3...

Similar documents
Recursion CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2011

Recursion. Fundamentals of Computer Science

Michele Van Dyne Museum 204B CSCI 136: Fundamentals of Computer Science II, Spring

2.3 Recursion. Overview. Mathematical Induction. What is recursion? When one function calls itself directly or indirectly.

Overview. What is recursion? When one function calls itself directly or indirectly.

CSC 1052 Algorithms & Data Structures II: Recursion

CS 112 Introduction to Programming

Department of Computer Science Yale University Office: 314 Watson Closely related to mathematical induction.

Chapter 12 Supplement: Recursion with Java 1.5. Mr. Dave Clausen La Cañada High School

i.e.: n! = n (n 1)

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion

2.3 Recursion. Overview. Greatest Common Divisor. Greatest Common Divisor. What is recursion? When one function calls itself directly or indirectly.

COMP-202: Foundations of Programming. Lecture 13: Recursion Sandeep Manjanna, Summer 2015

! New mode of thinking. ! Powerful programming paradigm. ! Mergesort, FFT, gcd. ! Linked data structures.

2.3 Recursion. Overview. Greatest Common Divisor. Greatest Common Divisor. What is recursion? When one function calls itself directly or indirectly.

RECURSION. Data Structures & SWU Rachel Cardell- Oliver

Unit #3: Recursion, Induction, and Loop Invariants

CSCI 136 Data Structures & Advanced Programming. Lecture 9 Fall 2018 Instructors: Bills

Identify recursive algorithms Write simple recursive algorithms Understand recursive function calling

More Complicated Recursion CMPSC 122

26 Feb Recursion. Overview. Greatest Common Divisor. Greatest Common Divisor. Greatest Common Divisor. Greatest Common Divisor

Announcements. Recursion and why study it. Recursive programming. Recursion basic idea

Unit #2: Recursion, Induction, and Loop Invariants

CS 310 Advanced Data Structures and Algorithms

Admin. How's the project coming? After these slides, read chapter 13 in your book. Quizzes will return

Recursive Methods and Problem Solving. Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms

Algorithm Design and Recursion. Search and Sort Algorithms

Recursion. What is Recursion? Simple Example. Repeatedly Reduce the Problem Into Smaller Problems to Solve the Big Problem

Reduction & Recursion Overview

CMSC 132: Object-Oriented Programming II. Recursive Algorithms. Department of Computer Science University of Maryland, College Park

Recursion Chapter 3.5

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

Complexity of Algorithms

DM536 Introduction to Programming. Peter Schneider-Kamp.

Recursion. Let s start by looking at some problems that are nicely solved using recursion. First, let s look at generating The Fibonacci series.

DM536 / DM550 Part 1 Introduction to Programming. Peter Schneider-Kamp.

We cover recursion in 150. Why do it again in 151?

DM502 Programming A. Peter Schneider-Kamp.

Notes - Recursion. A geeky definition of recursion is as follows: Recursion see Recursion.

STUDENT LESSON A9 Recursion

Recursion CS GMU

2.3 Recursion 7/23/2015 3:06:35 PM

CSCI 136: Fundamentals of Computer Science II. Keith Vertanen Museum

Fun facts about recursion

Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved

CSC 1351: Quiz 6: Sort and Search

APCS-AB: Java. Recursion in Java December 12, week14 1

Recursion Chapter 17. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

DM550/DM857 Introduction to Programming. Peter Schneider-Kamp

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

CMSC 150 LECTURE 7 RECURSION

Lecture 10: Recursion vs Iteration

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

Recursion Chapter 8. What is recursion? How can a function call itself? How can a function call itself?

Recursion. Chapter 5

Recursion. Chapter 11. Chapter 11 1

Recursive Definitions

Wentworth Institute of Technology COMP1050 Computer Science II Spring 2017 Derbinsky. Recursion. Lecture 13. Recursion

Module 10. Recursion. Adapted from Absolute Java, Rose Williams, Binghamton University

Fundamentals of Computer Science II

UNIT 5B Binary Search

recursive algorithms 1

Recall from Last Time: Big-Oh Notation


Anatomy of a static method, S&W, 2.1, figure page 188. Class.name or just name Scope, S&W, 2.1, figure page 189. Overloading

Recursion. COMS W1007 Introduction to Computer Science. Christopher Conway 26 June 2003

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2012

More recursion, Divide and conquer

CS103L SPRING 2017 UNIT 8: RECURSION

Decision-Making and Repetition

2.3 Recursion 7/21/2014 5:12:34 PM

Algorithm Analysis CISC4080 CIS, Fordham Univ. Instructor: X. Zhang

Recursion. Recursion [Bono] 1

CSCI 136 Data Structures & Advanced Programming. Lecture 12 Fall 2018 Profs Bill & Jon

Conditionals, Loops, and Style

Lecture P6: Recursion

Lecture 6: Recursion RECURSION

Recursion Chapter 8. What is recursion? How can a function call itself? How can a function call itself? contains a reference to itself.

11. Recursion. n (n 1)!, otherwise. Mathematical Recursion. Recursion in Java: Infinite Recursion. 1, if n 1. n! =

12. Recursion. n (n 1)!, otherwise. Educational Objectives. Mathematical Recursion. Recursion in Java: 1, if n 1. n! =

Recursion: Factorial (1) Recursion. Recursion: Principle. Recursion: Factorial (2) Recall the formal definition of calculating the n factorial:

6/4/12. Recursive void Methods. Chapter 11. Vertical Numbers. Vertical Numbers. Vertical Numbers. Algorithm for Vertical Numbers

Analyzing Complexity of Lists

CMPSCI 250: Introduction to Computation. Lecture #14: Induction and Recursion (Still More Induction) David Mix Barrington 14 March 2013

Programming II (CS300)

Objectives. Recursion. One Possible Way. How do you look up a name in the phone book? Recursive Methods Must Eventually Terminate.

CS 211: Recursion. Chris Kauffman. Week 13-1

Recursion. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

Programming II (CS300)

Sorting & Growth of Functions

34. Recursion. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

Recursion. Tracing Method Calls via a Stack. Beyond this lecture...

Chapter 6 Recursion. The Concept of Recursion

q To develop recursive methods for recursive mathematical functions ( ).

Recursion. Comp Sci 1575 Data Structures. Introduction. Simple examples. The call stack. Types of recursion. Recursive programming

q To develop recursive methods for recursive mathematical functions ( ).

Transcription:

Recursion Recursion Overview A method calling itself A new way of thinking about a problem Divide and conquer A powerful programming paradigm Related to mathematical induction Example applications Factorial Pretty graphics Sorting things Fundamentals of Computer Science Keith Vertanen Copyright 2013 2 Mathematical induction Prove a statement involving an integer N Base case: Prove it for small N (usually 0 or 1) Induction step: Assume true for size N-1 Prove it is true for size N Example: Prove T(N) = 1 + 2 + 3 +... + N = N(N + 1) / 2 for all N Base case: T(1) = 1 = 1(1 + 1) / 2 Induction step: Assume true for size N - 1, T(N - 1) = (N - 1)(N) / 2 T(N) = 1 + 2 + 3 +... + N-1 + N = (N 1)(N) / 2 + N = (N 1)(N) / 2 + 2N / 2 = (N 1 + 2)(N) / 2 = (N + 1)(N) / 2 3 Hello recursion Goal: Compute factorial N! = 1 * 2 * 3... * N Base case: 0! = 1 Induction step: Assume we know (N 1)! Use (N 1)! to find N! if (N == 0) return fact(n - 1) * N; public static void main(string [] args) int N = Integer.parseInt(args[0]); System.out.println(N + "! = " + fact(n)); 4! = 4 * 3 * 2 * 1 = 24 4! = 4 * 3! 3! = 3 * 2! 2! = 2 * 1! 1! = 1 * 0! 0! = 1 base case induction step 4

Instrumented Factorial Recursion vs. Iterative System.out.println("start, fact " + N); if (N == 0) System.out.println("end base, fact " + N); long step = fact(n - 1); System.out.println("end, fact " + N ); return step * N; start, fact 4 start, fact 3 start, fact 2 start, fact 1 start, fact 0 end base, fact 0 end, fact 1 end, fact 2 end, fact 3 end, fact 4 4! = 24 5 levels of fact() 5 Recursive algorithms also have an iterative version if (N == 0) return fact(n - 1) * N; Recursive algorithm long result = 1; for (int i = 1; i <= N; i++) result *= i; return result; Iterative algorithm Reasons to use recursion: Code is more compact and easier to understand Easer to reason about correctness Reasons not to use recursion: If you end up recalculating things repeatedly (stay tuned) Processor with very little memory (e.g. 8051 = 128 bytes) 6 A useful example of recursion Given an array of N sorted numbers Find out if some number t is in the list Do it faster than going linearly through the list, i.e. O(N) Basic idea: Like playing higher/lower number guessing algorithm Me I'm thinking of a number between 1 and 100. 50 You Higher 75 Lower 63 Higher 69 Higher 72 You got it Wow I'm super smart! 7 target (value) = 5 low (index) = 0 high (index) = 10 midpoint (index) = (0 + 10) / 2 = 5 8

algorithm algorithm target (value) = 5 low (index) = 0 high (index) = 4 midpoint (index) = (0 + 4) / 2 = 2 9 target (value) = 5 low (index) = 3 high (index) = 4 midpoint (index) = (3 + 4) / 2 = 3 10, recursive algorithm algorithm YES. Element at new midpoint is target! public static boolean binarysearch(int target, int low, int high, int[] d) int mid = (low + high) / 2; System.out.printf("low %d, high %d, mid %d\n", low, high, mid); if (d[mid] == target) return true; if (high < low) return false; if (d[mid] < target) return binarysearch(target, mid + 1, high, d); else return binarysearch(target, low, mid - 1, d); public static void main(string [] args) int [] d = 1, 2, 2, 5, 8, 9, 14, 14, 50, 88, 89; int target = Integer.parseInt(args[0]); System.out.println("found " + target + "? " + binarysearch(target, 0, d.length - 1, d)); 11 12

Missing base case return fact(n - 1) * N; Things to avoid No convergence guarantee public static double badidea(int N) if (N == 1) return 1.0; return badidea(1 + N/2) + 1.0/N; % java Factorial 5 Exception in thread "main" java.lang.stackoverflowerror... Both result in infinite recursion = stack overflow http://xkcd.com/244/ 13 14 Sometimes we don't know... Collatz sequence If N = 1, stop If N is even, divide by 2 If N is odd, multiply by 3 and add 1 e.g. 24 12 6 3 10 5 16 8 4 2 1 No one knows if this terminates for all N! public static void collatz(long N) System.out.print(N + " "); if (N == 1) return; if (N % 2 == 0) collatz(n / 2); else collatz(3 * N + 1); 15 http://xkcd.com/710/ 16

size Recursive graphics 17 18 H-tree of order N H-tree Draw an H Recursively draw 4 H-trees One at each "tip" of the original H, half the size Stop once N = 0 Fibonacci Numbers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, F 0 = 0 F 1 = 1 F n = F n-1 + F n-2 Fibonacci numbers. A natural fit for recursion? size N N - 1 N - 2 public static long fib(long n) if (n == 0) return 0; if (n == 1) return fib(n - 1) + fib(n -2); Yellow Chamomile head showing the arrangement in 21 (blue) and 13 (aqua) spirals. 19 20

Trouble in recursion city N time, fib(n) fib(4) 10 0.000 20 0.002 fib(3) fib(2) 30 0.011 40 0.661 fib(2) fib(1) fib(1) fib(0) 41 1.080 42 1.748 fib(1) fib(0) 43 2.814 44 4.531 45 7.371 Bad news bears: Order of growth = Exponential! 46 11.860 47 19.295 48 31.319 49 50.668 50 81.542 21 22 23 24

public static long fibfast(int n) long n2 = 0; long n1 = 1; if (n == 0) return 0; for (int i = 1; i < n; i++) long n0 = n1 + n2; n2 = n1; n1 = n0; return n1; N time, fib(n) 50 0.001 100 0.001 200 0.001 400 0.001 10,000,000 0.010 20,000,000 0.016 40,000,000 0.028 80,000,000 0.051 160,000,000 0.096 25 Recursion Summary A method calling itself Sometimes just once in method e.g. binary search Sometimes multiple times in method e.g. H-tree All good recursion must come to an end Sooner or later method must NOT call itself recursively, must have base case A powerful tool in computer science Allows writing elegant and easy to understand algorithms (Once you get your head around it) http://www.flickr.com/photos/zeusandhera/2496831822/ 26