Recursion in C Computer Organization I 1 September 2009 ©2006-09 McQuain, Feng & Ribbens Recursion Around the year 1900 the illustration of the "nurse"

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Recursion Michael Ernst UW CSE 140 To seal: moisten flap, fold over, and seal.
Recursion Michael Ernst CSE 190p University of Washington To seal: moisten flap, fold over, and seal.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 16: Recursion.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Recursion. 2 CMPS 12B, UC Santa Cruz Solving problems by recursion How can you solve a complex problem? Devise a complex solution Break the complex problem.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Csci1300 Introduction to Programming Recursion Dan Feng.
CSC 2300 Data Structures & Algorithms January 30, 2007 Chapter 2. Algorithm Analysis.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 16: Recursion.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Prof. S.M. Lee Department of Computer Science. Answer:
More on Recursion Averting Program Crashes CSC 1401: Introduction to Programming with Java Week 9 – Lecture 3 Wanda M. Kunkle.
Recursion CS Goals Discuss recursion as another form of repetition Do the following tasks, given a recursive routine Determine whether the routine.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Recursion.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Recursion Review.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion.
Function Recursion Examples “an example is worth a 1000 lectures”
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 17: Recursion.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Prof. S.M. Lee Department of Computer Science.
1 Decrease-and-Conquer Approach Lecture 06 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
Programming Principles II Lecture Notes 5 Recursion Andreas Savva.
Recursive. 2 Recursive Definitions In a recursive definition, an object is defined in terms of itself. We can recursively define sequences, functions.
Lecture 10 Recursion CSE225: Data Structures. 2 A Look Back at Functions #include double distance(double x1, double y1, double x2, double y2) { double.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
Functions and an Introduction to Recursion.  Recursive function ◦ A function that calls itself, either directly, or indirectly (through another function)
Stephen P. Carl - CS 2421 Recursion Reading : Chapter 4.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
CP104 Introduction to Programming Recursion 2 Lecture 29 __ 1 Recursive Function for gcd Recursive formula for the greatest common divisor of m and n,
Advanced Computer Architecture and Parallel Processing Rabie A. Ramadan http:
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
IB Computer Science Unit 5 – Advanced Topics Recursion.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 18 Recursion Lecture 6 Dr. Musab.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 18 Recursion.
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Maitrayee Mukerji. Factorial For any positive integer n, its factorial is n! is: n! = 1 * 2 * 3 * 4* ….* (n-1) * n 0! = 1 1 ! = 1 2! = 1 * 2 = 2 5! =
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Program Development and Design Using C++, Third Edition
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Chapter Topics Chapter 16 discusses the following main topics:
Recursion The programs discussed so far have been structured as functions that invoke one another in a disciplined manner For some problems it is useful.
Topic 6 Recursion.
Chapter 10 Recursion Instructor: Yuksel / Demirer.
RECURSION.
Decrease-and-Conquer Approach
Chapter 19 Recursion.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Data Structures and Algorithms
Recursion Chapter 18.
Module 1-10: Recursion.
Java Programming: Chapter 9: Recursion Second Edition
ITEC324 Principle of CS III
Recursion Examples.
Presentation transcript:

Recursion in C Computer Organization I 1 September 2009 © McQuain, Feng & Ribbens Recursion Around the year 1900 the illustration of the "nurse" appeared on Droste's cocoa tins. This is most probably invented by the commercial artist Jan (Johannes) Musset, who had been inspired by a pastel of the Swiss painter Jean Etienne Liotard, La serveuse de chocolat, also known as La belle chocolatière. The illustration indicated the wholesome effect of chocolate milk and became inextricably bound with the name Droste. - Wikipedia Commons

Recursion in C Computer Organization I 2 September 2009 © McQuain, Feng & Ribbens Recursively-defined Functions recursiona method of defining functions in which the function being defined is applied within its own definition uint64_t Fibonacci(uint64_t n) { if ( n < 2 ) return 1; return Fibonacci(n – 1) + Fibonacci(n – 2); } Reality check: If (yes and) no, what are you thinking?? Does your recursive fn return a value? If yes, are the calls to it all in assignment or return statements?

Recursion in C Computer Organization I 3 September 2009 © McQuain, Feng & Ribbens Execution Trace Fibonacci(5) == Fibonacci(4) + Fibonacci(3) == Fibonaccci(3) + Fibonacci(2) + Fibonacci(2) + Fibonacci(1) == Fibonacci(2) + Fibonacci(1) + Fibonacci(1) + Fibonacci(1) + Fibonacci(1) + Fibonacci(1) + 1 == Fibonacci(1) + Fibonacci(1) == == 8 uint64_t Fibonacci(uint64_t n) { if ( n < 2 ) return 1; return Fibonacci(n – 1) + Fibonacci(n – 2); } Fibonacci(5) leads to 15 function calls… 14 of them recursive.

Recursion in C Computer Organization I 4 September 2009 © McQuain, Feng & Ribbens Printing Large Integers Very large integers are (somewhat) easier to read if they are not simply printed as a sequence of digits: vs 12,345,678,901,234,567,890 How can we do this efficiently? The basic difficulty is that printing proceeds from left to right, and the number of digits that should precede the left-most comma depends on the total number of digits in the number. Here's an idea; let N be the integer to be printed, then: if N has no more than 3 digits, just print it normally otherwise print all but the last 3 digits print a comma followed by the last 3 digits

Recursion in C Computer Organization I 5 September 2009 © McQuain, Feng & Ribbens Printing Large Integers The preceding analysis leads directly to a recursive solution: void printWithCommas(uint64_t N) { if ( N < 1000 ) printf("%d", N); else { printWithCommas( N / 1000 ); printf(",%#03d", N % 1000); } printWithCommas( ) printWithCommas( ) defer 890 printWithCommas( ) defer 567 printWithCommas( ) defer 234 printWithCommas( ) defer 901 printWithCommas( ) defer 678 printWithCommas( 12 ) defer 345 print 12

Recursion in C Computer Organization I 6 September 2009 © McQuain, Feng & Ribbens GCD If x and y are non-negative integers so that x >= y and y >= 0, and not both are 0, the greatest common divisor (GCD) of x and y is the largest integer z that divides both x and y. So:GCD(12, 9) = 3GCD(36, 28) = 4 GCD(18, 18) = 18GCD(12, 0) = 12 That is: uint64_t GCD(uint64_t x, uint64_t y) { if ( y == 0 ) return x; return GCD(y, x % y); }

Recursion in C Computer Organization I 7 September 2009 © McQuain, Feng & Ribbens Recursion vs Iteration uint64_t Fibonacci(uint64_t n) { if ( n < 2 ) return 1; return Fibonacci(n – 1) + Fibonacci(n – 2); } uint64_t iFibonacci(uint64_t n) { if ( n < 2 ) return 1; uint64_t FiboNMinusTwo = 1; uint64_t FiboNMinusOne = 1; uint64_t FiboN; for (uint64_t i = 2; i <= n; i++) { FiboN = FiboNMinusOne + FiboNMinusTwo; FiboNMinusTwo = FiboNMinusOne; FiboNMinusOne = FiboN; } return FiboN; }

Recursion in C Computer Organization I 8 September 2009 © McQuain, Feng & Ribbens Recursion vs Iteration uint64_t GCD(uint64_t x, uint64_t y) { if ( y == 0 ) return x; return GCD(y, x % y); } uint64_t iGCD(uint64_t x, uint64_t y) { while ( y != 0 ) { uint64_t Remainder = x % y; x = y; y = Remainder; } return x; }

Recursion in C Computer Organization I 9 September 2009 © McQuain, Feng & Ribbens Tail vs non-Tail Recursion uint64_t GCD(uint64_t x, uint64_t y) { if ( y == 0 ) return x; return GCD(y, x % y); } Tail-recursive algorithms are ones that end in a recursive call (the only recursive call) and do not leave any deferred operations: uint64_t Fibonacci(uint64_t n) { if ( n < 2 ) return 1; return Fibonacci(n – 1) + Fibonacci(n – 2); } deferred until the second call returns; sometimes called augmenting recursive How many function calls would result from Fibonacci(5)?

Recursion in C Computer Organization I 10 September 2009 © McQuain, Feng & Ribbens A Fibonacci Refinement Here's an alternative recursive Fibonacci calculator: uint64_t Fibonacci(unsigned int N, uint64_t FiboNMinusTwo, uint64_t FiboNMinusOne) { if (N < 2) { // base cases return FiboNMinusTwo; } else if (N == 2) { // don't recurse, trivial return FiboNMinusOne + FiboNMinusTwo; } else { return Fibonacci(N - 1, FiboNMinusOne, FiboNMinusOne + FiboNMinusTwo); } is this tail-recursive? How many function calls would result from Fibonacci(5,1,1)?

Recursion in C Computer Organization I 11 September 2009 © McQuain, Feng & Ribbens Execution Trace uint64_t Fibonacci(unsigned int N, uint64_t FiboNMinusTwo, uint64_t FiboNMinusOne) { if (N < 2) { // base cases return FiboNMinusTwo; } else if (N == 2) { // don't recurse, trivial return FiboNMinusOne + FiboNMinusTwo; } else { return Fibonacci(N - 1, FiboNMinusOne, FiboNMinusOne + FiboNMinusTwo); } Fibonacci(5, 1, 1) == Fibonacci(4, 1, 2) == Fibonacci(3, 2, 3) == Fibonacci(2, 3, 5) == 8 How much difference does this make? On my laptop, computing Fibonacci(10) 10,000,000 times using the original recursive version took seconds. This version took seconds. The iterative version took seconds.

Recursion in C Computer Organization I 12 September 2009 © McQuain, Feng & Ribbens Performance: Recursion vs Iteration Recursive algorithms can always be implemented using iteration instead (although some sort of auxiliary structure like a stack may be required). A recursive implementation is often shorter and more obvious. An recursive implementation adds cost due to the number of function calls that are required and the related activity on the run-time stack. An iterative implementation may, therefore, be faster… but not always. An iterative implementation may require more memory from the heap (dynamic allocations) and a recursive implementation may require more memory on the stack… this has implications if you're targeting a limited environment, like an embedded system.