John Perry. Fall 2009

Similar documents
Recursion. John Perry. Spring 2016

Section 2.3 Fibonacci Numbers and the Golden Mean

Fibonacci s Mathematical Contributions

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

Algorithms. How data is processed. Popescu

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

Lecture 9: Tuesday, February 10, 2015

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

The Fibonacci Sequence

MAMA SID'S PIZZA by Faith Goddard-Allen

The Golden ratio (or Divine Proportion)

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

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

A Note on H-Cordial Graphs

MUMmer 2.0. Original implementation required large amounts of memory

Name: Adapted from Mathalicious.com DOMINO EFFECT

The Fibonacci Numbers and the Golden Ratio

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

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

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

7.RP Cooking with the Whole Cup

Greenhouse Effect Investigating Global Warming

Introduction to the Practical Exam Stage 1

The Fibonacci Numbers and the Golden Ratio

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

VIII. Claim Drafting Methodologies. Becky White

Rice Paddy in a Bucket

ENGI E1006 Percolation Handout

The Fibonacci Numbers Geometry Minimal design. All cocktails 155 SEK

Greenhouse Effect. Investigating Global Warming

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

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

Instruction (Manual) Document

Lesson 11: Comparing Ratios Using Ratio Tables

Chapter 1: The Ricardo Model

Price & Time Symmetry DENNIS W. WILBORN, SR.

6.2.2 Coffee machine example in Uppaal

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

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

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

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

DEVELOPING PROBLEM-SOLVING ABILITIES FOR MIDDLE SCHOOL STUDENTS

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

Going Strong. Comparing Ratios. to Solve Problems

Injection, Modularity, and Testing

Asynchronous Circuit Design

TACO SAUCE PENNY CLEANER

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

Economics 101 Spring 2016 Answers to Homework #1 Due Tuesday, February 9, 2016

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

Applying Brewing Better Beer

GEORGIA DEPARTMENT OF CORRECTIONS Standard Operating Procedures. Policy Number: Effective Date: 2/9/2018 Page Number: 1 of 5

Planning: Regression Planning

Left Recursion. Lecture 8 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 4, 2015

Synchronous Systems. Asynchronous Circuit Design. Synchronous Disadvantages. Synchronous Advantages. Asynchronous Advantages. Asynchronous Systems

Honeyed Spelt and Oat

Concepts/Skills. Materials

Quotient Cordial Labeling of Graphs

CLASSROOM NEWS Week of January 23, 2017! jmccool3rdgrade.weebly.com! (302)

Reading Essentials and Study Guide

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

Hispanic Retail Pilot Test Summary

3-Total Sum Cordial Labeling on Some New Graphs

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

Dining Philosophers Theory and Concept in Operating System Scheduling

WARM-UP 2/10/15. Take out your cooking for 2, 10, or 82!!! Wkst. If you want to make 6 muffins:

Effect of Yeast Propagation Methods on Fermentation Efficiency

Predicting Wine Quality

Dough Dispensing Device for Earth Elements Market and Bakery December 4, 2009

Lesson 4. Choose Your Plate. In this lesson, students will:

Comparison of 256-bit stream ciphers

(Inflorescence: Is a.k.a. the infructescence when the flowers have set fruit)

The Effect of Almond Flour on Texture and Palatability of Chocolate Chip Cookies. Joclyn Wallace FN 453 Dr. Daniel

wine 1 wine 2 wine 3 person person person person person

ELECTRONIC OVEN CONTROL GUIDE

GLOBALIZATION UNIT 1 ACTIVATE YOUR KNOWLEDGE LEARNING OBJECTIVES

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

Frequently Asked Questions Nutrition Resolution

Pg. 2-3 CS 1.2: Comparing Ratios. Pg CS 1.4: Scaling to Solve Proportions Exit Ticket #1 Pg Inv. 1. Additional Practice.

HONDURAS. A Quick Scan on Improving the Economic Viability of Coffee Farming A QUICK SCAN ON IMPROVING THE ECONOMIC VIABILITY OF COFFEE FARMING

Chapter 3. Labor Productivity and Comparative Advantage: The Ricardian Model. Pearson Education Limited All rights reserved.

Oak and Barrel Alternatives: Art and Science

Falling Objects. computer OBJECTIVES MATERIALS

Did you know? Decimal Day

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

Dum Ka Biryani, Make for each other

Archdiocese of New York Practice Items

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

Maximising Sensitivity with Percolator

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

openlca case study: Conventional vs Organic Viticulture

Warren Peterson Schooner Exact Brewing Company

Remembering The Kana: The Hiragana / The Katakana By James W. Heisig, Helmut Morsbach READ ONLINE

Lunch and Breakfast Meal Patterns

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

Lesson 23: Newton s Law of Cooling

Feudalism. Chapter 15, Section 2. Slaves. Serfs Both. (Pages )

A WORLD FIRST FOR HIBISCUS (WE THINK)

Chapter 5, Section 2. Systems of Linear Equations in Two Variables

Out of Home ROI and Optimization in the Media Mix Summary Report

Transcription:

Lecture 11: Recursion University of Southern Mississippi Fall 2009

Outline 1 2 3 You should be in worksheet mode to repeat the examples.

Outline 1 2 3

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

When? At least one base case with no All recursive chains terminate at base case

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) using P (i) for 1 i < n

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

Fibonacci s Bunnies Leonardo da Pisa, called Fibonacci, 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;

Fibonacci s Bunnies Leonardo da Pisa, called Fibonacci, 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;

Fibonacci s Bunnies Leonardo da Pisa, called Fibonacci, 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 + F2 + F 1 = F 2 + F 1 + F2 + F2 + 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 + F2 + F 1 = F 2 + F 1 + F2 + F2 + F 1 = 3F 2 + 2F 1 = 5. F 100 = F 99 + F 98 =... = 218922995834555169026 F 2 + 135301852344706746049 F 1 = 354224848179261915075

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

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

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

Outline 1 2 3

Infinite loops must stop eventually

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

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

Modify program: Example sage: def fibonacci(n): print 'computing fibonacci #', n, if (n>2): return fibonacci(n-2) + fibonacci(n-1) else: return 1 sage: fibonacci(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

Modify program: Example sage: def fibonacci(n): print 'computing fibonacci #', n, if (n>2): return fibonacci(n-2) + fibonacci(n-1) else: return 1 sage: fibonacci(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

Maintain list of pre-computed values: 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) Let F n = a + b return F n Workaround

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

Implementation sage: F = [1,1] sage: def fibonacci_with_table(n): if (n>len(f)): print 'computing fibonacci #', n, a = fibonacci_with_table(n-1) b = fibonacci_with_table(n-2) F.append(a + b) return F[n-1] Example sage: fibonacci_with_table(5) computing fibonacci # 5 computing fibonacci # 4 computing fibonacci # 3 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? Recursive: backwards, then forwards again F n F n 1, F n 2 F 2, F 1 F n Looped: direct F 1, F 2 F 3 F n 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 Let i = 2 while i < n do Compute next element, then move forward Let F next = F prev + F curr Let F prev = F curr, F curr = F next Increment i return F curr

sage: def looped_fibonacci(n): Fprev = 1 Fcurr = 1 i = 2 while (i < n): Fnext = Fprev + Fcurr Fprev = Fcurr Fcurr = Fnext i = i + 1 return Fcurr Looped Fibonacci: Implementation

sage: sage: def looped_fibonacci(n): Fprev = 1 Fcurr = 1 i = 2 while (i < n): Fnext = Fprev + Fcurr Fprev = Fcurr Fcurr = Fnext i = i + 1 return Fcurr looped_fibonacci(100) 354224848179261915075 Looped Fibonacci: Implementation

sage: sage: def looped_fibonacci(n): Fprev = 1 Fcurr = 1 i = 2 while (i < n): Fnext = Fprev + Fcurr Fprev = Fcurr Fcurr = Fnext i = i + 1 return Fcurr looped_fibonacci(100) 354224848179261915075 Looped Fibonacci: Implementation (Much faster than recursive version)

Recursive pros: simpler to write cons Looped Recursive vs. Looped vs. Closed-form slower memory intensive indefinite loop w/out loop structure pros: not too slow, not too complicated, loop can be definite cons: not (usually) as simple as recursive, sometime not obvious Closed-form 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