UNIT 5A Recursion: Basics. Recursion

Similar documents
UNIT 5A Recursion: Basics. Recursion

Algorithmic Methods Tricks of the Trade

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

def F a c t o r i a l ( n ) : i f n == 1 : return 1 else : return n F a c t o r i a l ( n 1) def main ( ) : print ( F a c t o r i a l ( 4 ) )

Recursion (Rosen, 6 th edition, Section 4.3, 4.4)

Recursive Definitions

Recursion. Dr. Jürgen Eckerle FS Recursive Functions

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

mith College Computer Science Week 13 CSC111 Spring 2018 Dominique Thiébaut

Recursion. Jordi Cortadella Department of Computer Science

1.7 Recursion. Department of CSE

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

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

EE 368. Weeks 4 (Notes)

recursive algorithms 1

CS1 Lecture 15 Feb. 19, 2018

Clicker Question What will this print? def blah(x): return x+1 def main(): z = blah(3) print(z) main() A) 3 B) 4 C) 5 D) It causes an error

Outline. Simple Recursive Examples Analyzing Recursion Sorting The Tower of Hanoi Divide-and-conquer Approach In-Class Work. 1 Chapter 6: Recursion


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

Recursion Chapter 4 Self-Reference. Recursive Definitions Inductive Proofs Implementing Recursion

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

CS/CE 2336 Computer Science II

RECURSION, RECURSION, (TREE) RECURSION! 2

Chapter 15: Recursion

Goals: Define computational problem instance of a computational problem algorithm pseudocode Briefly discuss recursive procedure big-o notation

RECURSION 7. 1 Recursion COMPUTER SCIENCE 61A. October 15, 2012

CSC-140 Assignment 4

11/2/2017 RECURSION. Chapter 5. Recursive Thinking. Section 5.1

CSE 214 Computer Science II Recursion

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

Last week. Another example. More recursive examples. How about these functions? Recursive programs. CSC148 Intro. to Computer Science

Data Structures And Algorithms

Reading 8 : Recursion

RECURSION 3. 1 Recursion COMPUTER SCIENCE 61A. June 30, 2016

Chapter 18 Recursion. Motivations

UNIT 5B Binary Search

RECURSION, RECURSION, (TREE) RECURSION! 3

Algorithm. Building blocks of algorithm

Fundamentals of Programming. Recursion - Part 2. October 30th, 2014

Recursion. Chapter 2. Objectives. Upon completion you will be able to:

Recursion. Thinking Recursively. Tracing the Recursive Definition of List. CMPT 126: Lecture 10. Recursion

Solving problems by recursion

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

Programming with Recursion. What Is Recursion?

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

UNIT B3 Number Sequences: Activities

CS1 Lecture 15 Feb. 18, 2019

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

Recursive Thinking. Chapter 8: Recursion. Recursive Definitions. Recursion. Java Software Solutions for AP* Computer Science A 2nd Edition

Chapter 10: Recursive Problem Solving

1 Short Answer (8 Points Each)

Lecture 10: Recursion vs Iteration

Blowing a gasket. Recursion. Classic recursive examples. CS112 Scientific Computation Department of Computer Science Wellesley College

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

Below is the simple C program with recursive approach for Towers of Hanoi problem

Chapter 13 Recursion. Chapter Objectives

a function that calls itself It doesn t do anything! Defining a recursive function 1) divide and conquer 2) base case 6/21/2018 chapter 15

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

Chapter 19 Recursion and Method Redefinition

Lecture Notes 4 More C++ and recursion CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Identify recursive algorithms Write simple recursive algorithms Understand recursive function calling

CSC 148 Lecture 3. Dynamic Typing, Scoping, and Namespaces. Recursion

RECURSION: n. SEE RECURSION 3

Recursion. ! When the initial copy finishes executing, it returns to the part of the program that made the initial call to the function.

SOFTWARE DEVELOPMENT 1. Recursion 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

Chapter 6 Recursion. The Concept of Recursion

ENVIRONMENT DIAGRAMS AND RECURSION 2

CSE 230 Intermediate Programming in C and C++ Recursion

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

Recursion. Chapter 11. Chapter 11 1

ITERATION AND RECURSION 3

Decision-Making and Repetition

Introduction to Functional Programming

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

RECURSION, RECURSION, (TREE) RECURSION! 3

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

Lecture 24 Tao Wang 1

CmpSci 187: Programming with Data Structures Spring 2015

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS

CSC 8301 Design and Analysis of Algorithms: Recursive Analysis

CSC1016. Welcome (again!) Get course notes handout (and read them)

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

Recursion Introduction OBJECTIVES

Instructors: Daniel Deutch, Amir Rubinstein, Teaching Assistants: Amir Gilad, Michal Kleinbort

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

Unit #2: Recursion, Induction, and Loop Invariants

Lecture 6: Combinatorics Steven Skiena. skiena

Koch snowflake. Fractal Fern

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

Outline Purpose How to analyze algorithms Examples. Algorithm Analysis. Seth Long. January 15, 2010

DM536 Introduction to Programming. Peter Schneider-Kamp.

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

CS103L SPRING 2017 UNIT 8: RECURSION

Induction and Recursion. CMPS/MATH 2170: Discrete Mathematics

A Function that Calls Itself. It Doesn t Do Anything! Defining a Recursive Function. 2) Base Case. 1) Divide and Conquer 11/22/2010

Nonvisual Arrays and Recursion. by Chris Brown under Prof. Susan Rodger Duke University June 2012

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

CSCI 121: Recursive Functions & Procedures

Transcription:

UNIT 5A Recursion: Basics 1 Recursion A recursive function is one that calls itself. Infinite loop? Not necessarily. 2 1

Recursive Definitions Every recursive definition includes two parts: Base case (non recursive) A simple case that can be done without solving the same problem again. Recursive case(s) One or more cases that are simpler versions of the original problem. By simpler, we sometimes mean smaller or shorter or closer to the base case. 3 Example: Factorial n! = n (n 1) (n 2) 1 2! = 2 1 3! = 3 2 1 4! = 4 3 2 1 So 4! = 4 3! And 3! = 3 2! What is the base case? 0! = 1 4 2

How Recursion Works 4! = 4(3!) = 4(6) = 24 3! = 3(2!) = 3(2) = 6 2! = 2(1!) = 2(1) = 2 1! = 1 (0!) = 1(1) = 1 make smaller instances of the same problem Base case build up the result 5 Factorial in Ruby (Recursive) def factorial (n) if n == 0 % base case return 1 else % recursive case return n * factorial(n-1) 6 3

Fibonacci Numbers A sequence of numbers such that each number is the sum of the previous two numbers in the sequence, starting the sequence with 0 and 1. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, etc. 7 Fibonacci Numbers in Nature 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, etc. Number of branches on a tree. Number of petals on a flower. Number of spirals on a pineapple. 8 4

Recursive Definition Let fib(n) = the nth Fibonacci number, n 0 fib(0) = 0 (base case) fib(1) = 1 (base case) fib(n) = fib(n 1) + fib(n 2), n > 1 9 Recursive Fibonacci in Ruby def fib(n) if n == 0 or n == 1 return n else return fib(n-1) + fib(n-2) 10 5

Recursive Definition 5 fib(5) 3 fib(4) 2 fib(3) 2 1 fib(3) fib(2) fib(2) fib(1) 1 1 1 1 1 0 fib(2) fib(1) fib(1) fib(0) 1 0 fib(1) fib(0) 1 0 fib(1) fib(0) fib(0) = 0 fib(1) = 1 fib(n) = fib(n-1) + fib(n-2), n > 1 11 Recursive vs. Iterative Solutions For every recursive function, there is an equivalent iterative solution. For every iterative function, there is an equivalent recursive solution. But some problems are easier to solve one way than the other way. 12 6

Factorial Function (Iterative) def factorial (n) result = 1 for i in 1..n do result = result * i return result 13 Iterative Fibonacci def fib(n) x = 0 next_x = 1 for i in 1..n do x, next_x = next_x, x+next_x return x Much faster than the recursive version. Why? 14 7

Recursive sum of a list def sumlist(list) n = list.length if n == 0 then return 0 else Base case: The sum of an empty list is 0. return list[0] + sumlist(list[1..n-1]) tail of list Recursive case: The sum of a list is the first element + the sum of the rest of the list. 15 Geometric Recursion (Fractals) A recursive operation performed on successively smaller regions. Sierpinski's Triangle http://fusionanomaly.net/recursion.jpg 16 8

Sierpinski s Triangle 17 Sierpinski s Carpet 18 9

Simple Fractal To draw a fractal with top left corner (x,y) and a side length of size: Draw a white square with top left corner (x,y) and a side length of size/2. Draw another fractal with top left corner (x+size/2, y+size/2) and a side length of size/2. [recursive step] increasing x increasing y (x,y) size Draw a fractal with top left corner at (x+size/2, y+size/2) and a side length of size/2. 19 Simple Fractal To draw a fractal with top left corner (x,y) and a side length of size: Draw a white square with top left corner (x,y) and a side length of size/2. Draw another fractal with top left corner (x+size/2, y+size/2) and a side length of size/2. [recursive step] (x,y) size Draw a fractal with top left corner at (x+size/2, y+size/2) and a side length of size/2. 20 10

Simple Fractal To draw a fractal with top left corner (x,y) and a side length of size: Draw a white square with top left corner (x,y) and a side length of size/2. Draw another fractal with top left corner (x+size/2, y+size/2) and a side length of size/2. [recursive step] (x,y) size Draw a fractal with top left corner at (x+size/2, y+size/2) and a side length of size/2. 21 Simple Fractal in Ruby (not all code shown) def fractal(x, y, size) return if size < 2 # base case draw_square(x, y, size/2) fractal(x+size/2, y+size/2, size/2) def draw_fractal() # initial top-left (x,y) and size fractal(0, 0, 512) 22 11

23 Next Lecture Binary search: Apply the technique of recursion in doing search Analyze its time complexity 24 12

Famous Puzzle of Towers of Hanoi EXTRA RECURSION EXAMPLE 25 Towers of Hanoi A puzzle invented by French mathematician Edouard Lucas in 1883. At a temple far away, priests were led to a courtyard with three pegs and 64 discs stacked on one peg in size order. Priests are only allowed to move one disc at a time from one peg to another. Priests may not put a larger disc on top of a smaller disc at any time. The goal of the priests was to move all 64 discs from the leftmost peg to the rightmost peg. According to the story, the world would when the priests finished their work. Towers of Hanoi with 8 discs. 26 13

Towers of Hanoi Problem: Move n discs from peg A to peg C using peg B. 1. Move n 1 discs from peg A to peg B using peg C. (recursive step) A B C A B C 2. Move 1 disc from peg A to peg C. 3. Move n 1 discs from peg B to C using peg A. (recursive step) A B C A B C 27 Towers of Hanoi in Ruby def towers(n, from_peg, to_peg, using_peg) if n >= 1 then towers(n-1, from_peg, using_peg, to_peg) puts "Move disc from " + from_peg + " to " + to_peg towers(n-1, using_peg, to_peg, from_peg) In irb: towers(4, "A", "C", "B") How many moves do the priests need to move 64 discs? 28 14