Recursion. John Perry. Spring 2016

Similar documents
John Perry. Fall 2009

Section 2.3 Fibonacci Numbers and the Golden Mean

Fibonacci. books that contributed to the study of mathematics. These books introduced Europe to Indian and

Algorithms. How data is processed. Popescu

Fibonacci s Mathematical Contributions

Mini Project 3: Fermentation, Due Monday, October 29. For this Mini Project, please make sure you hand in the following, and only the following:

Name: Adapted from Mathalicious.com DOMINO EFFECT

The Golden ratio (or Divine Proportion)

The Fibonacci Sequence

MAMA SID'S PIZZA by Faith Goddard-Allen

MUMmer 2.0. Original implementation required large amounts of memory

Fibonacci Numbers: How To Use Fibonacci Numbers To Predict Price Movements [Kindle Edition] By Glenn Wilson

Introduction to the Practical Exam Stage 1. Presented by Amy Christine MW, DC Flynt MW, Adam Lapierre MW, Peter Marks MW

A Note on H-Cordial Graphs

Lecture 9: Tuesday, February 10, 2015

The Golden Ratio And Fibonacci Numbers By R. A. Dunlap READ ONLINE

The Fibonacci Numbers and the Golden Ratio

The Fibonacci Numbers and the Golden Ratio

Price & Time Symmetry DENNIS W. WILBORN, SR.

Week 5 Objectives. Subproblem structure Greedy algorithm Mathematical induction application Greedy correctness

The Fibonacci Numbers Geometry Minimal design. All cocktails 155 SEK

Greenhouse Effect Investigating Global Warming

6.2.2 Coffee machine example in Uppaal

Introduction to the Practical Exam Stage 1

Compiler. --- Lexical Analysis: Principle&Implementation. Zhang Zhizheng.

GLOBALIZATION UNIT 1 ACTIVATE YOUR KNOWLEDGE LEARNING OBJECTIVES

Roux Bot Home Cooker. UC Santa Cruz, Baskin Engineering Senior Design Project 2015

Guided Study Program in System Dynamics System Dynamics in Education Project System Dynamics Group MIT Sloan School of Management 1

VIII. Claim Drafting Methodologies. Becky White

Going Strong. Comparing Ratios. to Solve Problems

Greenhouse Effect. Investigating Global Warming

Injection, Modularity, and Testing

Rice Paddy in a Bucket

Instruction (Manual) Document

0 + 1 = = = 2 + = = 3 + = = 5 + = = 8 + = = 13 + =

Activity 10. Coffee Break. Introduction. Equipment Required. Collecting the Data

Predicting Fruitset Model Philip Schwallier, Amy Irish- Brown, Michigan State University

Dum Ka Biryani, Make for each other

7.RP Cooking with the Whole Cup

STA Module 6 The Normal Distribution

STA Module 6 The Normal Distribution. Learning Objectives. Examples of Normal Curves

Fibonacci Pattern ZHU ZHI YUAN. July 17th, Math of the universe Duke summer college 2017

3-Total Sum Cordial Labeling on Some New Graphs

Bourbon Barrel Notes. So enjoy reading the notes below, and we will keep this updated with each barrel we release! CURRENT RELEASE

Duration of resource: 17 Minutes. Year of Production: Stock code: VEA12062

CS 322: (Social and Information) Network Analysis Jure Leskovec Stanford University

ENGI E1006 Percolation Handout

TACO SAUCE PENNY CLEANER

Algorithmic Thinking. Alison Pamment. On behalf of the course team (STFC/NERC:CEDA, NERC:NCAS, NERC:NCEO)

Chapter 1 The Beginnings of Human Society

Content. Learning Outcomes. In this lesson you will learn adjectives and vocabulary for talking about fast food.

Comparison of 256-bit stream ciphers

Mixers Innovation. José Cheio De Oliveira

Lesson 23: Newton s Law of Cooling

The jar of salad cream

All About Food 1 UNIT

Which of your fingernails comes closest to 1 cm in width? What is the length between your thumb tip and extended index finger tip? If no, why not?

Applying Brewing Better Beer

Warren Peterson Schooner Exact Brewing Company

Biologist at Work! Experiment: Width across knuckles of: left hand. cm... right hand. cm. Analysis: Decision: /13 cm. Name

Quotient Cordial Labeling of Graphs

ARM4 Advances: Genetic Algorithm Improvements. Ed Downs & Gianluca Paganoni

Honeyed Spelt and Oat

World Fair Trade Day. New Building Bridges. Introduction. Warm-up activity

Reading Essentials and Study Guide

Concepts/Skills. Materials

Lesson 11: Comparing Ratios Using Ratio Tables

Enzymes in Industry Time: Grade Level Objectives: Achievement Standards: Materials:

Maximising Sensitivity with Percolator

Illinois Geometry Lab. Percolation Theory. Authors: Michelle Delcourt Kaiyue Hou Yang Song Zi Wang. Faculty Mentor: Kay Kirkpatrick

Hispanic Retail Pilot Test Summary

A WORLD FIRST FOR HIBISCUS (WE THINK)

DEVELOPING PROBLEM-SOLVING ABILITIES FOR MIDDLE SCHOOL STUDENTS

Pevzner P., Tesler G. PNAS 2003;100: Copyright 2003, The National Academy of Sciences

TURNING A MEAL INTO AN EXPERIENCE 2018 RESTAURANT INDUSTRY REPORT

Dining Philosophers Theory and Concept in Operating System Scheduling

Special Interchange Design National Perception vs. Urban Reality. Traffic Engineering and Safety Conference October 14, 2015

Measuring household food waste The Spain experience

Proposal for Instruction Manual/Feasibility Study /Rewrite of Manual on Starbucks Barista Espresso Machine

The First People 5 million-5,000 years ago. Picture source: humanorigins.si.edu

Predicting Wine Quality

1. Wine Seminar May 27 th 2012

In the eye of the beer holder: thoughts on color, bubbles and the meaning of life. Charlie Bamforth

Flexible Working Arrangements, Collaboration, ICT and Innovation

After your yearly checkup, the doctor has bad news and good news.

Square Divisor Cordial, Cube Divisor Cordial and Vertex Odd Divisor Cordial Labeling of Graphs

EATING OUTSIDE THE HOME

End of Chapter Exercises 1. What values do the following assignment statements place in the variable result?

2012 Estimated Acres Producers Estimated Production Units Estimated Farm Value Farm Crawfish 182,167 1,251 90,973,725 Lbs.

Frequently Asked Questions Nutrition Resolution

Title: NEED BETTER Trash bags?

Name: Hour: Review: 1. What are the three elements that you need to measure to guarantee a successful recipe?

Scientific Research and Experimental Development (SR&ED) Tax Credit

HomePizzaChef s. The Secrets of Making Great Home Pizza

Paper: PHYLLOTAXIS OF THE VATICAN PIGNA

T R A I N I N G M O D U L E 4 TECHNIQUES T H A T S H A P E D T H E B A R W O R L D

Adapted By Kennda Lynch, Elizabeth Adsit and Kathy Zook July 26, Moooooogic!

Sugar Cane in Costa Rica THE PROCESS

Novice Guide for Cuts (pot still)

Chapter 1: The Ricardo Model

Transcription:

MAT 305: Recursion University of Southern Mississippi Spring 2016

Outline 1 2 3

Outline 1 2 3

re + cursum: return, travel the path again (Latin) Two (similar) views: mathematical: a function defined using itself; computational: an algorithm that invokes itself.

When? At least one base case w/closed form ( closed = no ) All recursive chains terminate at base case

Example: Proof by induction Prove P (n) for all n : Inductive Base: Show P (1) Inductive Hypothesis: Assume P (i) for 1 i n Inductive Step: Show P (n + 1) using P (i) for 1 i n

Example: Fibonacci s Bunnies Fibonacci (Leonardo da Pisa) describes in Liber Abaci a population of bunnies: first month: one pair of bunnies;

Example: Fibonacci s Bunnies Fibonacci (Leonardo da Pisa) describes in Liber Abaci a population of bunnies: first month: one pair of bunnies; second month: pair matures; third month: mature pair produces new pair;

Example: Fibonacci s Bunnies Fibonacci (Leonardo da Pisa) describes in Liber Abaci a population of bunnies: first month: one pair of bunnies; second month: pair matures; third month: mature pair produces new pair; fourth month: second pair matures, first pair produces new pair;

Example: Fibonacci s Bunnies Fibonacci (Leonardo da Pisa) describes in Liber Abaci a population of bunnies: first month: one pair of bunnies; second month: pair matures; third month: mature pair produces new pair; fourth month: second pair matures, first pair produces new pair; fifth month: third pair matures, two mature pairs produce new pairs;...

How many pairs? month 1 2 3 4 5 6 7... mature pairs immature pairs new pairs 1 total pairs 1

How many pairs? month 1 2 3 4 5 6 7... mature pairs immature pairs 1 new pairs 1 total pairs 1 1

How many pairs? month 1 2 3 4 5 6 7... mature pairs 1 immature pairs 1 new pairs 1 1 total pairs 1 1 2

How many pairs? month 1 2 3 4 5 6 7... mature pairs 1 1 immature pairs 1 1 new pairs 1 1 1 total pairs 1 1 2 3

How many pairs? month 1 2 3 4 5 6 7... mature pairs 1 1 2 immature pairs 1 1 1 new pairs 1 1 1 2 total pairs 1 1 2 3 5

How many pairs? month 1 2 3 4 5 6 7... mature pairs 1 1 2 3 immature pairs 1 1 1 2 new pairs 1 1 1 2 3 total pairs 1 1 2 3 5 8

How many pairs? month 1 2 3 4 5 6 7... mature pairs 1 1 2 3 5 immature pairs 1 1 1 2 3 new pairs 1 1 1 2 3 5 total pairs 1 1 2 3 5 8 13...

Describing it month 1 2 3 4 5 6 7... mature pairs 1 1 2 3 5 immature pairs 1 1 1 2 3 new pairs 1 1 1 2 3 5 total pairs 1 1 2 3 5 8 13... total = (# mature + # immature) + # new

Describing it month 1 2 3 4 5 6 7... mature pairs 1 1 2 3 5 immature pairs 1 1 1 2 3 new pairs 1 1 1 2 3 5 total pairs 1 1 2 3 5 8 13... total = (# mature + # immature) + # new total = # one month ago + # new

Describing it month 1 2 3 4 5 6 7... mature pairs 1 1 2 3 5 immature pairs 1 1 1 2 3 new pairs 1 1 1 2 3 5 total pairs 1 1 2 3 5 8 13... total = (# mature + # immature) + # new total = # one month ago + # new total = # one month ago + # mature now

Describing it month 1 2 3 4 5 6 7... mature pairs 1 1 2 3 5 immature pairs 1 1 1 2 3 new pairs 1 1 1 2 3 5 total pairs 1 1 2 3 5 8 13... total = (# mature + # immature) + # new total = # one month ago + # new total = # one month ago + # mature now total = # one month ago + # two months ago

Describing it month 1 2 3 4 5 6 7... mature pairs 1 1 2 3 5 immature pairs 1 1 1 2 3 new pairs 1 1 1 2 3 5 total pairs 1 1 2 3 5 8 13... total = (# mature + # immature) + # new total = # one month ago + # new total = # one month ago + # mature now total = # one month ago + # two months ago F now = F one month ago + F two months ago, or

Describing it month 1 2 3 4 5 6 7... mature pairs 1 1 2 3 5 immature pairs 1 1 1 2 3 new pairs 1 1 1 2 3 5 total pairs 1 1 2 3 5 8 13... total = (# mature + # immature) + # new total = # one month ago + # new total = # one month ago + # mature now total = # one month ago + # two months ago F now = F one month ago + F two months ago, or F i = F i 1 + F i 2

Fibonacci Sequence 1, i = 1,2; F i = F i 1 + F i 2, i 3.

Example F i = Fibonacci Sequence 1, i = 1,2; F i 1 + F i 2, i 3. F 5 = F 4 + F 3 = (F 3 + F 2 ) + (F 2 + F 1 ) = [(F 2 + F 1 ) + F 2 ] + (F 2 + F 1 ) = 3F 2 + 2F 1 = 5.

Example F i = Fibonacci Sequence 1, i = 1,2; F i 1 + F i 2, i 3. F 5 = F 4 + F 3 = (F 3 + F 2 ) + (F 2 + F 1 ) = [(F 2 + F 1 ) + F 2 ] + (F 2 + F 1 ) = 3F 2 + 2F 1 = 5. F 100 = F 99 + F 98 =... = 218922995834555169026 F 2 + 135301852344706746049 F 1 = 354224848179261915075

Pseudocode Easy to implement w/: algorithm Fibonacci inputs n outputs the nth Fibonacci number do if n = 1 or n = 2 return 1 else return Fibonacci(n 2) + Fibonacci(n 1)

Implementation sage: def fibonacci(n): if n == 1 or n == 2: return 1 else: return fibonacci(n-2) + fibonacci(n-1)

Implementation sage: sage: 5 sage: 6765 def fibonacci(n): if n == 1 or n == 2: return 1 else: return fibonacci(n-2) + fibonacci(n-1) fibonacci(5) fibonacci(20) sage: fibonacci(30) 832040

Outline 1 2 3

Infinite loops must stop eventually must ensure reach base case

Infinite loops must stop eventually must ensure reach base case Wasted computation fibonacci(20) requires fibonacci(19) and fibonacci(18) fibonacci(19) also requires fibonacci(18) fibonacci(18) computed twice!

Example Modify program: sage: def fibonacci_details(n): print computing fibonacci #, n, if n == 1 or n == 2: return 1 else: return fibonacci_details(n-2) + fibonacci_details(n-1)

Example Modify program: sage: def fibonacci_details(n): print computing fibonacci #, n, if n == 1 or n == 2: return 1 else: return fibonacci_details(n-2) + fibonacci_details(n-1) sage: fibonacci_details(5) computing fibonacci # 5 computing fibonacci # 3 computing fibonacci # 1 computing fibonacci # 2 computing fibonacci # 4 computing fibonacci # 2 computing fibonacci # 3 computing fibonacci # 1 computing fibonacci # 2 5

Example Modify program: sage: def fibonacci_details(n): print computing fibonacci #, n, if n == 1 or n == 2: return 1 else: return fibonacci_details(n-2) + fibonacci_details(n-1) sage: fibonacci_details(5) computing fibonacci # 5 computing fibonacci # 3 computing fibonacci # 1 computing fibonacci # 2 computing fibonacci # 4 computing fibonacci # 2 computing fibonacci # 3 computing fibonacci # 1 computing fibonacci # 2 5...F 3 computed 2 times; F 2, 3 times; F 1, 2 times

Workaround Can we tell Sage to remember pre-computed values? Need a list Compute F i? add value to list Apply formula only if F i not in list! Remember computation after function ends: global list (called a cache)

Workaround Can we tell Sage to remember pre-computed values? Need a list Compute F i? add value to list Apply formula only if F i not in list! Remember computation after function ends: global list (called a cache) Definition global variables available to all functions in system cache makes information quickly accessible

Pseudocode algorithm Fibonacci_with_table globals F, a list of integers, initially [1,1] inputs n outputs the nth Fibonacci number do if n > #F Let a = Fibonacci_with_table(n 1) Let b = Fibonacci_with_table(n 2) Append a + b to F return F n

sage: F = [1,1] sage: Hand implementation def fibonacci_with_table(n): global F if n > len(f): print computing fibonacci #, n, a = fibonacci_with_table(n-2) b = fibonacci_with_table(n-1) F.append(a + b) return F[n-1]

sage: F = [1,1] sage: Hand implementation def fibonacci_with_table(n): global F if n > len(f): print computing fibonacci #, n, a = fibonacci_with_table(n-2) b = fibonacci_with_table(n-1) F.append(a + b) return F[n-1] Example sage: fibonacci_with_table(5) computing fibonacci # 5 computing fibonacci # 4 computing fibonacci # 3 5

sage: But... no need to implement! @cached_function def fibonacci_cached(n): print computing fibonacci #, n, if n == 1 or n == 2: return 1 else: return fibonacci_cached(n-2) + fibonacci_cached(n-1)

sage: But... no need to implement! @cached_function def fibonacci_cached(n): print computing fibonacci #, n, if n == 1 or n == 2: return 1 else: return fibonacci_cached(n-2) + fibonacci_cached(n-1) Example sage: fibonacci(5) computing fibonacci # 5 computing fibonacci # 3 computing fibonacci # 1 computing fibonacci # 2 computing fibonacci # 4 5

Avoid when possible can often rewrite as a loop can sometimes rewrite in closed form However...

Avoid when possible can often rewrite as a loop can sometimes rewrite in closed form Example Closed form for Fibonacci sequence: However... F n = 1+ 5 2 n 1 5 2 n 5.

Avoid when possible can often rewrite as a loop can sometimes rewrite in closed form Example Closed form for Fibonacci sequence: However... F n = 1+ 5 2 n 1 5 2 n 5. Coincidence? I think not... 1 + 5 = golden ratio 2

Looped Fibonacci: How? We will not use the closed form, but a loop Recursive: down, then up, then down, then up... F n 1 F n F n 2 F n 2 F n 3 F n 4.

Looped Fibonacci: How? We will not use the closed form, but a loop Recursive: down, then up, then down, then up... F n 1 F n F n 2 F n 2 F n 3 F n 4. Looped: only up, directly! F 2 F 3 F n +F1 +F2 +Fn 2 remember two previous computations remember? = variables

Looped Fibonacci: Pseudocode algorithm Looped_Fibonacci inputs n outputs the nth Fibonacci number do Define the base case Let F prev = 1, F curr = 1 Use the formula to move forward to F n for i {3,...,n} Compute next element, then move forward Let F next = F prev + F curr Let F prev = F curr Let F curr = F next return F curr

sage: def looped_fibonacci(n): Fprev = 1 Fcurr = 1 for i in xrange(3,n+1): Fnext = Fprev + Fcurr Fprev = Fcurr Fcurr = Fnext return Fcurr Looped Fibonacci: Implementation

sage: def looped_fibonacci(n): Fprev = 1 Fcurr = 1 for i in xrange(3,n+1): Fnext = Fprev + Fcurr Fprev = Fcurr Fcurr = Fnext return Fcurr sage: looped_fibonacci(100) 354224848179261915075 Looped Fibonacci: Implementation (Much faster than recursive version)

Faster, too sage: %time a = looped_fibonacci(30000) CPU time: 0.01 s, Wall time: 0.01 s sage: %time a = Fibonacci_with_table(30000) CPU time: probably crashes, Wall time: if not, get some coffee sage: %time a = Fibonacci(10000) CPU time: probably crashes, Wall time: if not, come back tomorrow

Recursive Recursive vs. Looped vs. Closed-form pros: simpler to write cons: slower, memory intensive, indefinite loop w/out loop structure Looped (also called dynamic programming) Closed-form pros: not too slow, not too complicated, loop can be definite cons: not as simple as recursive, sometime not obvious pros: one step (no loop) cons: finding it often requires significant effort

Outline 1 2 3

Recursion: function defined using other values of function Issues can waste computation can lead to infinite loops (bad design) Use when closed/loop form too complicated chains not too long memory table feasible