Recursion vs. Iteration Eliminating Recursion



Similar documents
Node-Based Structures Linked Lists: Implementation

Binary Heap Algorithms

Heaps & Priority Queues in the C++ STL 2-3 Trees

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Linked Lists: Implementation Sequences in the C++ STL

Sequences in the C++ STL

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Module 2 Stacks and Queues: Abstract Data Types

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

Loop Invariants and Binary Search

Symbol Tables. Introduction

Introduction to Data Structures

Lecture 11: Tail Recursion; Continuations

Fast Arithmetic Coding (FastAC) Implementations

Pseudo code Tutorial and Exercises Teacher s Version

Figure 1: Graphical example of a mergesort 1.

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Sources: On the Web: Slides will be available on:

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Analysis of Micromouse Maze Solving Algorithms

Recursive Fibonacci and the Stack Frame. the Fibonacci function. The Fibonacci function defines the Fibonacci sequence, using a recursive definition :

Semantic Analysis: Types and Type Checking

Chapter 3: Restricted Structures Page 1

Section IV.1: Recursive Algorithms and Recursion Trees

Recursive Algorithms. Recursion. Motivating Example Factorial Recall the factorial function. { 1 if n = 1 n! = n (n 1)! if n > 1

7.1 Our Current Model

C++FA 3.1 OPTIMIZING C++

Common Data Structures

Habanero Extreme Scale Software Research Project

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

Data Structures. Algorithm Performance and Big O Analysis

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

6. Standard Algorithms

5.1 Radical Notation and Rational Exponents

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Stack Allocation. Run-Time Data Structures. Static Structures

Compilers I - Chapter 4: Generating Better Code

Sequential Data Structures

Zabin Visram Room CS115 CS126 Searching. Binary Search

Data Structure with C

Cost Model: Work, Span and Parallelism. 1 The RAM model for sequential computation:

Chapter 7: Sequential Data Structures

Analysis of Binary Search algorithm and Selection Sort algorithm

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Chapter 13: Fibonacci Numbers and the Golden Ratio

Lesson 4 Annuities: The Mathematics of Regular Payments

Recursion. Slides. Programming in C++ Computer Science Dept Va Tech Aug., Barnette ND, McQuain WD

C++ Programming Language

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

recursion here it is in C++ power function cis15 advanced programming techniques, using c++ fall 2007 lecture # VI.1

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Recursion and Recursive Backtracking

Lecture Notes on Linear Search

Interpreters and virtual machines. Interpreters. Interpreters. Why interpreters? Tree-based interpreters. Text-based interpreters

Binary search algorithm

Software Engineering Concepts: Testing. Pointers & Dynamic Allocation. CS 311 Data Structures and Algorithms Lecture Slides Monday, September 14, 2009

Linear Programming. March 14, 2014

Divide: Paper & Pencil. Computer Architecture ALU Design : Division and Floating Point. Divide algorithm. DIVIDE HARDWARE Version 1

APP INVENTOR. Test Review

Addressing The problem. When & Where do we encounter Data? The concept of addressing data' in computations. The implications for our machine design(s)

NUMBER SYSTEMS APPENDIX D. You will learn about the following in this appendix:

10CS35: Data Structures Using C

Basic Components of an LP:

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

csci 210: Data Structures Recursion

Lecture 13 - Basic Number Theory.

Linear Programming Problems

recursion, O(n), linked lists 6/14

Recursion and Dynamic Programming. Biostatistics 615/815 Lecture 5

2) Write in detail the issues in the design of code generator.

What Is Recursion? 5/12/10 1. CMPSC 24: Lecture 13 Recursion. Lecture Plan. Divyakant Agrawal Department of Computer Science UC Santa Barbara

CS201: Architecture and Assembly Language

Ordered Lists and Binary Trees

Stacks. Linear data structures

Linear Programming Notes V Problem Transformations

Chapter 5 Functions. Introducing Functions

CS321. Introduction to Numerical Methods

6.080 / Great Ideas in Theoretical Computer Science Spring 2008

Chapter 5 Instructor's Manual

Lecture 12 Doubly Linked Lists (with Recursion)

Mathematical Induction. Lecture 10-11

Tail call elimination. Michel Schinz

Solving Problems Recursively

CS 103X: Discrete Structures Homework Assignment 3 Solutions

Formal Languages and Automata Theory - Regular Expressions and Finite Automata -

CS 2412 Data Structures. Chapter 2 Stacks and recursion

Linear Programming. April 12, 2005

PES Institute of Technology-BSC QUESTION BANK

CHAPTER 5. Number Theory. 1. Integers and Division. Discussion

AP Computer Science Java Mr. Clausen Program 9A, 9B

Transcription:

Recursion vs. Iteration Eliminating Recursion continued CS 311 Data Structures and Algorithms Lecture Slides Monday, February 16, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks CHAPPELLG@member.ams.org 2005 2009 Glenn G. Chappell

Unit Overview Recursion & Searching Major Topics (part) Introduction to Recursion Search Algorithms Recursion vs. Iteration Eliminating Recursion Recursive Search with Backtracking 16 Feb 2009 CS 311 Spring 2009 2

Review Search Algorithms [1/3] Binary Search is an algorithm to find a given key in a sorted list. Here, key = thing to search for. Often there is associated data. In computing, sorted = in (some) order. Procedure Pick an item in the middle: the pivot. Use this to narrow search to top or bottom half of list. Recurse. Example: Binary Search for 64 in the following list. 1. Looking for 64 in this list. 5 8 9 13 22 30 34 37 38 41 60 63 65 82 87 90 91 2. Pivot. Is 64 < 38? No. 3. Recurse: Looking for 64 in this list. Etc. 16 Feb 2009 CS 311 Spring 2009 3

Review Search Algorithms [2/3] Binary Search is much faster than Sequential Search, and so the amount of data it can process in the same amount of time is much greater. Number of Look-Ups We Have Time to Perform 1 2 3 4 10 20 40 k Maximum Allowable List Size: Binary Search 1 2 4 8 512 524,288 549,755,813,888 Roughly 2 k Maximum Allowable List Size: Sequential Search 1 2 3 4 10 20 40 k The fundamental law of computer science: As machines become more powerful, the efficiency of algorithms grows more important, not less. Nick Trefethen 16 Feb 2009 CS 311 Spring 2009 4

Review Search Algorithms [3/3] Equality vs. Equivalence a == b is equality.!(a < b) &&!(b < a) is equivalence. Equality & equivalence may not be the same thing when objects being compared are not numbers. Using equivalence instead of equality: Maintains consistency with operator<. Allows for use with objects that do not have operator==. Iterator operations that can be done in more than one way. Using Operators Random-access iterators only iter += n iter2 iter1 Using STL Algorithms Works with all forward iterators Still fast with random-access std::advance(iter, n) std::distance(iter1, iter2) 16 Feb 2009 CS 311 Spring 2009 5

Review Recursion vs. Iteration The Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, To get the next Fibonacci number, add the two before it. They are defined formally as follows: We denote the nth Fibonacci number by F n (n = 0, 1, 2, ). F 0 = 0. = 1. For n 2, F n = F n 2 + F n 1. As before, recurrence relations often translate nicely into recursive algorithms. TO DO Another recurrence relation Write a recursive function to compute a Fibonacci number: given n, compute F n. Done. See fibo1.cpp, on the web page. 16 Feb 2009 CS 311 Spring 2009 6

Recursion vs. Iteration Fibonacci Numbers Problem For high-ish values of n (above 35, say) function fibo in fibo1.cpp is extremely slow. What can we do about this? TO DO continued Rewrite the Fibonacci computation in a fast iterative form. Figure out how to do a fast recursive version. Write it. Done. See fibo2.cpp, on the web page. Done. See fibo3.cpp, on the web page. 16 Feb 2009 CS 311 Spring 2009 7

Recursion vs. Iteration Fibonacci Numbers Lessons [1/2] Choice of algorithm can make a huge difference in performance. Recursive calls made to compute F 6. In fibo1.cpp In fibo3.cpp F 6 F 6 F 4 F 5 F 5 & F 6 F 4 & F 5 F 2 F 3 F 3 F 4 F 3 & F 4 F 0 F 2 F 2 F 2 F 3 F 2 & F 3 F 0 F 0 F 0 F 2 & F 2 F 0 F 0 & F -1 & F 0 As we will see, data structures can make a similar difference. 16 Feb 2009 CS 311 Spring 2009 8

Recursion vs. Iteration Fibonacci Numbers Lessons [2/2] Some algorithms have natural implementations in both recursive and iterative form. Iterative means making use of loops. A struct can be used to return two values at once. The template std::pair (declared in <utility>) can be helpful. Sometimes we have a workhorse function that does most of the processing and a wrapper function that is set up for convenient use. Often the wrapper just calls the workhorse for us. This is common when we use recursion, since recursion can place inconvenient restrictions on how a function is called. We have seen this in another context. Remember tostring and operator<< in the package from Assignment #1. 16 Feb 2009 CS 311 Spring 2009 9

Recursion vs. Iteration Drawbacks of Recursion Two factors can make recursive algorithms inefficient. Inherent inefficiency of some recursive algorithms However, there are efficient recursive algorithms (fibo3.cpp). Function-call overhead Making all those function calls requires work: saving return addresses, creating and destroying automatic variables. And recursion has another problem. Memory-management issues Memory for automatic variables is allocated in a way that does not allow for normal error handling. Making too many recursive calls will cause stack overflow (resulting in a crash or worse). These two are important regardless of the recursive algorithm used. When we use iteration, we can manage memory ourselves. This is more work, but it also allows us to handle errors properly. 16 Feb 2009 CS 311 Spring 2009 10

Recursion vs. Iteration Note Even Faster fibo There is actually a simple formula for F n (we must use floating-point). 1+ 5 Let ϕ = 1.6180339887. This is called the golden ratio. 2 n ϕ Then for each nonnegative integer n, F n is the nearest integer to. 5 Here is fibo using this formula: int fibo(int n) { } const double sqrt5 = sqrt(5.); const double phi = (1. + sqrt5) / 2.; // "Golden ratio" double nearly = pow(phi, n) / sqrt5; // phi^n/sqrt(5) // Our Fibonacci number is the nearest integer return int(nearly + 0.5); See fibo5.cpp, on the web page. 16 Feb 2009 CS 311 Spring 2009 11

Eliminating Recursion In General [1/2] Fact. Every recursive function can be rewritten as an iterative function that uses essentially the same algorithm. Think: How does the system help you do recursion? It provides a Stack, used to hold return addresses for function calls, and values of automatic local variables. We can implement such a Stack ourselves. We need to be able to store: Values of automatic local variables, including parameters. The return value (if any). Some indication of where we have been in the function. Thus, we can eliminate recursion by mimicking the system s method of handling recursive calls using Stack frames. 16 Feb 2009 CS 311 Spring 2009 12

Eliminating Recursion In General [2/2] To rewrite any recursive function in iterative form: Declare an appropriate Stack. A Stack item holds all automatic variables, an indication of what location to return to, and the return value (if any). Brute-force method Replace each automatic variable with its field in the top item of the Stack. Set these up at the beginning of the function. Put a loop around the rest of the function body: while (true) { }. Replace each recursive call with: Push an object with parameter values and current execution location on the Stack. Restart the loop ( continue ). A label marking the current location. Pop the stack, using the return value (if any) appropriately. Replace each return with: If the return address is the outside world, really return. Otherwise, set up the return value, and skip to the appropriate label ( goto?). This method is primarily of theoretical interest. Thinking about the problem often gives better solutions than this. We will look at this method further when we study Stacks. 16 Feb 2009 CS 311 Spring 2009 13

Eliminating Recursion Tail Recursion [1/4] A special kind of recursion is tail recursion: when a recursive call is the last thing a function does. Tail recursion is important because it makes the recursion iteration conversion very easy. That is, we like tail recursion because it is easy to eliminate. In fact, tail recursion is such an obvious thing to optimize that some compilers automatically convert it to iteration. Note: We are speaking generally here, not specific to C++. Automatic tail-recursion elimination is common in functional languages. 16 Feb 2009 CS 311 Spring 2009 14

Eliminating Recursion Tail Recursion [2/4] For a void function, tail recursion looks like this: void foo(ttt a, UUU b) { foo(x, y); } For a function returning a value, tail recursion looks like this: SSS bar(ttt a, UUU b) { return bar(x, y); } 16 Feb 2009 CS 311 Spring 2009 15

Eliminating Recursion Tail Recursion [3/4] The reason tail recursion is so easy to eliminate is that we never need to return from the recursive call to the calling function. Because there is nothing more for the calling function to do. Thus, we can replace the recursive call by something essentially like a goto. Eliminating Tail Recursion Surround the function body with a big loop, as before. Replace the tail recursive call with: Set parameters to their new values (and restart the loop which happens automatically, since we are already at the end of the loop). There is no need to make any changes to non-recursive return statements, that is, the base case(s). If the only recursive call in a function is at the end, then eliminating tail recursion converts the function into nonrecursive form. 16 Feb 2009 CS 311 Spring 2009 16

Eliminating Recursion Tail Recursion [4/4] TO DO Eliminate the recursion in binsearch2.cpp. First, modify function binsearch so that it has exactly one recursive call, and this is at the end of the function. This should be easy. Next, eliminate tail recursion. This is a little trickier to think about, but very easy to do. Done. See binsearch3.cpp, on the web page. Done. See binsearch4.cpp, on the web page. 16 Feb 2009 CS 311 Spring 2009 17