EECS 477: Introduction to algorithms. Lecture 7

Similar documents
EECS 477: Introduction to algorithms. Lecture 6

Recursion. What is Recursion? Simple Example. Repeatedly Reduce the Problem Into Smaller Problems to Solve the Big Problem

Recursion. Comp Sci 1575 Data Structures. Introduction. Simple examples. The call stack. Types of recursion. Recursive programming

Recursion Chapter 3.5

G Programming Languages - Fall 2012

Divide & Conquer. 2. Conquer the sub-problems by solving them recursively. 1. Divide the problem into number of sub-problems

Computing Fundamentals Advanced functions & Recursion

Identify recursive algorithms Write simple recursive algorithms Understand recursive function calling

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Lecture 35: Analysis Review & Tail Recursion 10:00 AM, Nov 28, 2018

EECS 477: Introduction to algorithms. Lecture 10

COE428 Lecture Notes Week 1 (Week of January 9, 2017)

Recursion. CSE 2320 Algorithms and Data Structures University of Texas at Arlington

CSC 2400: Computer Systems. Using the Stack for Function Calls

Run-Time Data Structures

Functions in MIPS. Functions in MIPS 1

Unit #3: Recursion, Induction, and Loop Invariants

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

Induction and Recursion. CMPS/MATH 2170: Discrete Mathematics

(Refer Slide Time: 00:51)

CS 3410 Ch 7 Recursion

Software Analysis. Asymptotic Performance Analysis

Lecture 10: Recursion vs Iteration

11 and 12 Arithmetic Sequence notes.notebook September 14, 2017

Fundamental mathematical techniques reviewed: Mathematical induction Recursion. Typically taught in courses such as Calculus and Discrete Mathematics.

Topic 12 Introduction to Recursion. "To a man with a hammer, everything looks like a nail" -Mark Twain

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

"To a man with a hammer,

STUDENT LESSON A9 Recursion

Lecture 5. Announcements: Today: Finish up functions in MIPS

Reduction & Recursion Overview

LECTURE 16. Functional Programming

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

ECE G205 Fundamentals of Computer Engineering Fall Exercises in Preparation to the Midterm

The Running Time of Programs

Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10

Lecture 3.4: Recursive Algorithms

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

Functions in C. Memory Allocation in C. C to LC3 Code generation. Next.. Complete and submit C to LC3 code generation. How to handle function calls?

We can emit stack-machine-style code for expressions via recursion

CS 310 Advanced Data Structures and Algorithms

CS61 Section Solutions 3

Mathematical Induction

Question 1 (45 points)

RUNNING TIME ANALYSIS. Problem Solving with Computers-II

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

CSC236 Week 4. Larry Zhang

DATA STRUCTURES AND ALGORITHMS

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

Code Generation. Lecture 12

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

Dynamic Programming. An Introduction to DP

TECH. Recurrence Equations vs. Recursive Procedures. Recursion and Induction. e.g. Fibonacci Function. The working of recursive procedure

Arithmetic Sequences

Analyzing Complexity of Lists

Solving Linear Recurrence Relations (8.2)

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

Today. Putting it all together

COMP 161 Lecture Notes 16 Analyzing Search and Sort

EK131 E5 Introduction to Engineering

This chapter covers recursive definition, including finding closed forms.

Recall from Last Time: Big-Oh Notation

Recursion. Tracing Method Calls via a Stack. Beyond this lecture...

Recursion. Fundamentals of Computer Science

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types

Recursion. Chapter 5

Stack Frames. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 15

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

Algorithm Analysis and Design

Recursion: Factorial (1) Recursion. Recursion: Principle. Recursion: Factorial (2) Recall the formal definition of calculating the n factorial:

Recursively Defined Functions

The divide and conquer strategy has three basic parts. For a given problem of size n,

Inference rule for Induction

Linked List Implementation of Queues

CSC 8301 Design and Analysis of Algorithms: Recursive Analysis

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

Concepts Introduced in Chapter 7

Unit #2: Recursion, Induction, and Loop Invariants

The Limits of Sorting Divide-and-Conquer Comparison Sorts II

Attributes of Variable. Lecture 13: Run-Time Storage Management. Lifetime. Value & Location

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

A function that invokes itself is said to

System Software Assignment 1 Runtime Support for Procedures

CS 173 [A]: Discrete Structures, Fall 2012 Homework 8 Solutions

Algorithms in Systems Engineering IE172. Midterm Review. Dr. Ted Ralphs

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

CSc 520 Principles of Programming Languages

Complexity, Induction, and Recurrence Relations. CSE 373 Help Session 4/7/2016

EC 413 Computer Organization

Recursion CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2011

Final Exam Review Questions

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

Functions and the MIPS Calling Convention 2 CS 64: Computer Organization and Design Logic Lecture #11

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

Discussion on Writing of Recursive Algorithm

Tail Recursion. ;; a recursive program for factorial (define fact (lambda (m) ;; m is non-negative (if (= m 0) 1 (* m (fact (- m 1))))))

Fall Lecture 13 Thursday, October 11

memoization or iteration over subproblems the direct iterative algorithm a basic outline of dynamic programming

CMSC351 - Fall 2014, Homework #2

Lecture 16: Introduction to Dynamic Programming Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY

Transcription:

EECS 477: Introduction to algorithms. Lecture 7 Prof. Igor Guskov guskov@eecs.umich.edu September 26, 2002 1

Lecture outline Recursion issues Recurrence relations Examples 2

Recursion: factorial unsigned fact_rec(unsigned n) { return ( n==0? 1 : n*fact_rec(n-1) ); } unsigned fact_iter(unsigned n) { unsigned result = 1; for(unsigned k=0; k<n; ++k) result *= k; return result; } 3

Recursion Recursive algorithms invoke themselves (maybe indirectly ) May never terminate (need initial conditions/base case), then the result is not defined Similar to induction proofs lend themselves nicely for things defined recursively Challenging for algorithm analysis: cannot use sequential composition/loops, cannot inline function calls 4

Recursion: advantages Algorithmic versions of inductive definitions and proofs are easy: for instance many algorithms on trees or mathematical functions on integers Especially, algorithm correctness: induction proofs GCD, factorial, tree traversal, etcetera Easy and fast implementation: directly from description, when you have little precious time, exam/deadline for noncritical parts 5

Recursion: usual drawbacks Complexity analysis is not as clear as correctness Time: they call themselves Memory: implicit program stack usage What actually happens when a function is called: Caller: pushes local variables and arguments onto the program stack Callee start-up: pops its arguments from program stack Callee return: pushes return value onto the program stack Caller: pops return value and local variables More data may be pushed onto the program stack: registers 6

Recursion Time complexity handled by recurrencies Memory complexity handled by careful accounting Statically allocated memory goes onto program stack but not dynamically allocated Rule of thumb: limit static memory allocation in functions that are called many times, e.g. recursive 7

Recursion drawbacks In practice, recursive versions are often slower Overhead of static variables and registers Any recursive algorithm can be implemented without recursion using a single explicit stack without slowdown (sometimes more convenient to use several stacks). Ex: think of evaluating arithmetic expressions calculator Rule of thumb: remove recursion from time-critical sections of your code Program stack is very limited and may overflow: so do not rely on it when implementing recursive algorithms for large datasets, e.g. graphs (unless you have explicit support for that kind of adventures in your system) 8

Recursion: misc Tail recursion: a single recursion call at the end each call will reuse the same memory frame for local variables and pass the result directly to the caller Example: int fact(int n) { return fact_tail(n, 1); } int fact_tail(int n, int f) { if(n==0) return 1; return fact_tail(n-1, n*f); } compare it to the simple recursive version: no need to keep local vars Another idea: memorize results on frequent instances, like dynamic programming 9

Recurrencies Equations where functions are unknowns (like difference eqs) Time complexity as unknown is of interest Ex: Factorial t(n) = t(n 1) + c 1, t(0) = c 0 Solution: t(n) = c 1 n + c 0 = O(n) A similar recurrence t(n) = 3 t(n 1) + c 1, t(0) = c 0 but the solution is Ω(3 n ), beware that some constants matter!!! A basic method: guess the answer and prove by induction 10

Intelligent guesswork t(0) = 0, t(n) = 4t(n/3) + n t(0) = 0, t(1) = 1, t(3) = 7, t(9) = 37, t(27) = 175, hmmm... t(1) = 1, t(3) = 4 1 + 3, t(9) = 4 2 + 4 3 + 3 2,... Here s the pattern: t(3 k ) = k i=0 3 i 4 k i = 4 k k i=0 (3/4) i = 4 k+1 43 k So for n = 3 k we get t(n) = 4(n log 3 4 n) = Θ(n log 3 4 ) then use smoothness rule 11

Linear recurrencies Recursive Fibonacci t(n) = c 1 + t(n 1) + t(n 2) and t(0) = t(1) = c 0 Need closed form solution And this is a linear recurrence!!! makes sense for recursion General form: a 0 t(n) + a 1 t(n 1) +... + a k t(n k) = f(n) plus initial conditions on t(0),..., t(k 1) a k are constants Start with homogeneous case: f(n) = 0: solutions form linear space (can add and scale them) 12

Linear recurrencies: characteristic polynomial Consider solution of exponential kind t(n) = x n, substitute into equation to get or a 0 x n + a 1 x n 1 +... a k x n k = 0 a 0 x k + a 1 x k 1 +... a k = 0 Find roots of the above and assume they are different r 1,..., r k. Then t(n) = c 1 r n 1 + c 2r n 2 +... + c kr n k is a general solution form, find constants from initial conditions 13

Linear recurrencies: multiple roots for a root r of multiplicity m we get m fundamental solutions r n, nr n,..., n m 1 r n Again, find constants from initial conditions 14

Linear recurrencies: inhomogeneity Inhomogeneous are important! a 0 t(n) + a 1 t(n 1) +... + a k t(n k) = b n p(n), restricted version where p(n) is a polynomial of degree d. Solution involves forming the implied homogeneous recurrence: (a 0 x k + a 1 x k 1 +... a k )(x b) d+1 = 0 General solution is t(n) = m i 1 i j=0 c ijn j ri n then substitute into the original recurrence and initial condition Example: 4.7.8 on page 128 15