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

Similar documents
UNIT 5A Recursion: Basics. Recursion

Solving problems by recursion

UNIT 5A Recursion: Basics. Recursion

Fruitful Recursion (recursion that returns a value)

CS/CE 2336 Computer Science II

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

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

Recursion continued. Programming using server Covers material done in Recitation. Part 2 Friday 8am to 4pm in CS110 lab

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

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

Bisection method. we can implement the bisection method using: a loop to iterate until f(c) is close to zero a function handle to the function f

Identify recursive algorithms Write simple recursive algorithms Understand recursive function calling

Recursive Definitions

Recursion. Dr. Jürgen Eckerle FS Recursive Functions

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

Discovering. Algebra. An Investigative Approach. Condensed Lessons for Make-up Work

recursive algorithms 1

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

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 ) )

CS1 Lecture 15 Feb. 19, 2018

STREAMS AND REVIEW 12

Recursive definition: A definition that is defined in terms of itself. Recursive method: a method that calls itself (directly or indirectly).

1 Recursion. 2 Recursive Algorithms. 2.1 Example: The Dictionary Search Problem. CSci 235 Software Design and Analysis II Introduction to Recursion

Recursion Solution. Counting Things. Searching an Array. Organizing Data. Backtracking. Defining Languages

BBM 101. Introduction to Programming I. Lecture #06 Recursion

Chapter 13 Recursion. Chapter Objectives

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

CSE 214 Computer Science II Recursion

CS200: Recursion and induction (recap from cs161)

Recursive Definitions and Structural Induction

Test Bank Ver. 5.0: Data Abstraction and Problem Solving with C++: Walls and Mirrors, 5 th edition, Frank M. Carrano

In this lesson, students build fractals and track the growth of fractal measurements using tables and equations. Enduring Understanding

ENGG1811 Computing for Engineers Week 10 Recursion, External Application Interfacing

CSC 8301 Design and Analysis of Algorithms: Recursive Analysis

CSE 2123 Recursion. Jeremy Morris

Recursion vs Induction

Data Structures And Algorithms

CS1 Lecture 15 Feb. 18, 2019

CS115 INTRODUCTION TO COMPUTER SCIENCE 1. Additional Notes Module 5

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

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

Recursion vs Induction. CS3330: Algorithms The University of Iowa

Summary. csci 210: Data Structures Recursion. Recursive algorithms. Recursion. Topics READING:

csci 210: Data Structures Recursion

Resources matter. Orders of Growth of Processes. R(n)= (n 2 ) Orders of growth of processes. Partial trace for (ifact 4) Partial trace for (fact 4)

Perry Bible Fellowship [pbfcomics.com/94/]

Recursion. notes Chapter 8

Algorithmic Methods Tricks of the Trade

Introduction to Recursion

Chapter Summary. Mathematical Induction Recursive Definitions Structural Induction Recursive Algorithms

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

1.7 Recursion. Department of CSE

Data Abstraction & Problem Solving with C++: Walls and Mirrors 6th Edition Carrano, Henry Test Bank

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

CS 112 Introduction to Programming

Chapter 18 Recursion. Motivations

Divide and Conquer Strategy. (Page#27)

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

Unit-5 Dynamic Programming 2016

Recursion. Example 1: Fibonacci Numbers 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,

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

Mathematics 350 Section 6.3 Introduction to Fractals

Recursive Algorithms. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Recursion. Recursion is: Recursion splits a problem:

CS 380 ALGORITHM DESIGN AND ANALYSIS

CISC 3115 TY3. C21a: Recursion. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/6/2018 CUNY Brooklyn College

Recursion. Chapter 5

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

Do you remember what is the most powerful tool for managing complexity?

5.1 The String reconstruction problem

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

Dynamic Programming Homework Problems

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

Fractals. Materials. Pencil Paper Grid made of triangles

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

Applications. 44 Stretching and Shrinking

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

C22a: Problem Solving using Recursion

Fun with Fractals and Functions. CHAMP at University of Houston March 2, 2015 Houston, Texas

Recursion. Chapter 7

Unit 8: Recursion. Dave Abel. April 6th, 2016


Programming with Recursion. What Is Recursion?

ENVIRONMENT DIAGRAMS AND RECURSION 2

Environment Diagrams and Recursion Fall 2017 Discussion 2: September 6, 2017 Solutions. 1 More Environment Diagrams

CS 201 Advanced Object-Oriented Programming Lab 10 - Recursion Due: April 21/22, 11:30 PM

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

Data Structures. Chapter 06. 3/10/2016 Md. Golam Moazzam, Dept. of CSE, JU

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

Recursion Chapter 3.5

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

9/16/14. Overview references to sections in text RECURSION. What does generic mean? A little about generics used in A3

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

Assignment 7: Due Wednesday May 11 at 6pm UPDATES on Monday May 9

Programming & Data Structure

Algorithm Design Techniques. Hwansoo Han

Backtracking. Module 6. The Eight Queens Problem. CS 147 Sam Houston State University Dr. McGuire. Backtracking. Recursion Revisited

An algorithm is a sequence of instructions that one must perform in order to solve a wellformulated

Due Thursday, July 18 at 11:00AM

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

Transcription:

Blowing a gasket Classic recursive examples CS112 Scientific Computation Department of Computer Science Wellesley College Recursion Recursion solves a problem by solving a smaller instance of the same problem Think divide, conquer, and glue, where all of the subproblems have the same shape as the original problem 2 1

Factorial 4! = 4 x (3 x 2 x 1) = 4 x 3! function result = fact (n) if (n <= 1) % base case result = 1; else % recursive step result = n * fact(n-1); end 3! = 3 x (2 x 1) = 3 x 2! 2! = 2 x (1) = 2 x 1! 1! = 1 base case recursive cases 3 The recursive leap of faith To solve a problem recursively: If it is simple enough then solve it immediately Otherwise express solution in terms of smaller, similar problem(s) Initially this will take some faith on your part 4 2

Fibonacci numbers, or multiplying rabbits? Fibonacci numbers first appear around 500 B.C. in writings of a Sanscrit grammarian named Pingala who studied rhythm in language Leonardo Fibonacci studied these numbers around 1200 in the context of multiplying rabbits 1 1 2 3 5 8 13 21 34 In the first month, there s one newborn pair Newborn pairs become fertile in their second month Each month, every fertile pair begets a new pair Rabbits never die 5 Fibonacci function function result = fibonacci (n) if (n <= 2) % base case result = 1; else % recursive step result = fibonacci(n-1) + fibonacci(n-2); end fib(5) fib(4) fib(3) 6 3

Towers of Hanoi 7 A natural recursive solution function towersofhanoi (n, source, destination, spare) if (n == 1) % base case disp(['move disk 1 from ' source ' to ' destination]); else % recursive step towersofhanoi(n-1, source, spare, destination); disp(['move disk ' num2str(n) ' from ' source ' to ' destination]); towersofhanoi(n-1, spare, destination, source); end 8 4

Sierpinski Gasket The Sierpinski Gasket was devised by the polish mathematician, Waclaw Sierpinski, in the early 1900 s It is a self-similar figure composed of triangles whose construction can be described recursively 9 Sierpinski Gasket To construct a Sierpinski Gasket of level n and side length len: if level n = 1 then draw an equilateral triangle of size len otherwise draw a Sierpinski Gasket of level (n-1) and side length len/2 in lower left corner draw a Sierpinski Gasket of level (n-1) and side length len/2 in lower right corner draw a Sierpinski Gasket of level (n-1) and side length len/2 in the top corner 10 5

Sierpinski Gasket: The Code function sierpinski (levels, x, y, length) % draws Sierpinski's Gasket with input number of levels and % input side length, and lower left corner located at (x,y) if (levels == 1) % base case: draw a triangle triangle(x, y, length); else % recursive step % draw smaller Sierpinski Gasket in lower left corner sierpinski(levels-1, x, y, length/2); % draw smaller Sierpinski Gasket in lower right corner sierpinski(levels-1, x+length/2, y, length/2); % draw smaller Sierpinski Gasket in upper corner sierpinski(levels-1, x+length/4, y+sqrt(3)*length/4, length/2); end function triangle (x, y, size) % draws equilateral triangle of input size with lower left corner at (x,y) plot([x x+size x+size/2 x], [y y y+sqrt(3)*size/2 y]); 11 Madam I m Adam Suppose we want to test whether a string is a palindrome How can we express the solution to this problem using recursion? 12 6

Trees are everywhere 13 Print the contents of a tree phylo = {'trees' {'deciduous' {'elm' {} {}}... {'maple' {'japanese' {} {}}... {'sugar' {} {}}}}... {'evergreen' {'spruce' {} {}}... {'pine' {'white' {} {}}... {'southern yellow' {} {}}}}}; trees deciduous evergreen elm maple spruce pine japanese sugar white southern yellow 14 7

Think recursively! If the tree is empty do nothing Otherwise print the string in the root node print the contents of the left subtree print the contents of the right subtree trees deciduous evergreen elm maple spruce pine japanese sugar white southern yellow 15 Let s write the code If the tree is not empty print the string in the root node print the contents of the left subtree print the contents of the right subtree function printtree (tree) % prints all of the strings contained in the input tree if ~isempty(tree) disp(tree{1}); printtree(tree{2}); printtree(tree{3}); end % if tree is not empty % print string in root node % print left subtree % print right subtree 16 8

So is it empty? function answer = isempty (tree) % returns true if the input tree is an empty cell array answer = (length(tree) == 0); 17 Decisions, decisions attributes of a potential loan customer credit assessment 18 9

Data Mining with decision trees Medicine: What health factors result in increased risk for certain diseases or other health problems? Economics: What social, political, or financial factors contribute most directly to certain economic indicators? Geology: What physical factors best predict catastrophic events such as earthquakes and volcanoes? 19 10