Hassan Khosravi / Geoffrey Tien

Slides:



Advertisements
Similar presentations
Recursion.  Understand how the Fibonacci series is generated  Recursive Algorithms  Write simple recursive algorithms  Analyze simple recursive algorithms.
Advertisements

Nested and Excessive Recursion
Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate.
Circular Arrays Neat trick: use a circular array to insert and remove items from a queue in constant time The idea of a circular array is that the end.
Recursion. Binary search example postponed to end of lecture.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Topic 7 – Recursion (A Very Quick Look). CISC 105 – Topic 7 What is Recursion? A recursive function is a function that calls itself. Recursive functions.
Fall 2009ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
CENG 7071 Recursion. CENG 7072 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive function.
Recursion.  Identify recursive algorithms  Write simple recursive algorithms  Understand recursive function calling  With reference to the call stack.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
1 Storage Classes, Scope, and Recursion Lecture 6.
Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that.
Recursion. Definition Recursion is a function calling on itself over and over until reaching an end state. One such example is factorial. 10! = 10 * 9.
Lecture 10 Recursion CSE225: Data Structures. 2 A Look Back at Functions #include double distance(double x1, double y1, double x2, double y2) { double.
Computer Science and Software Engineering University of Wisconsin - Platteville 9. Recursion Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Recursion Review: A recursive function calls itself, but with a smaller problem – at least one of the parameters must decrease. The function uses the results.
Functions-Recall 1. 2 Parameter passing & return void main() { int x=10, y=5; printf (“M1: x = %d, y = %d\n”, x, y); interchange (x, y); printf (“M2:
CMPT 225 Recursion. Objectives Understand how the Fibonacci series is generated Recursive Algorithms  Write simple recursive algorithms  Analyze simple.
1 Chapter 8 Recursion. 2 Recursive Function Call a recursion function is a function that either directly or indirectly makes a call to itself. but we.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
Recursion Chapter 10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion (Continued) Prof. Katherine Gibson Prof. Jeremy Dixon Based on slides from UPenn’s.
CS212: Data Structures and Algorithms
Recursion.
Fibonacci’s rabbits Fibonacci posed the following problem:
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Recursion CENG 707.
Recursion CENG 707.
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.
CMSC201 Computer Science I for Majors Lecture 20 – Recursion (Continued) Prof. Katherine Gibson Based on slides from UPenn’s CIS 110, and from previous.
Data Structures and Algorithms
A Question How many people here don’t like recursion? Why not?
Lesson #6 Modular Programming and Functions.
Recursion: The Mirrors
Lesson #6 Modular Programming and Functions.
Java 4/4/2017 Recursion.
Review: recursion Tree traversals
Functions, variables, operators, loops Modularity
Recursion Chapter 10.
Recursive functions.
Lesson #6 Modular Programming and Functions.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Applied Algorithms (Lecture 17) Recursion Fall-23
Recursion Chapter 11.
Hassan Khosravi / Geoffrey Tien
Hassan Khosravi / Geoffrey Tien
CMSC201 Computer Science I for Majors Lecture 17 – Recursion (cont)
Unit 3 Test: Friday.
Module 1-10: Recursion.
CS302 - Data Structures using C++
Basics of Recursion Programming with Recursion
Lesson #6 Modular Programming and Functions.
Recursion Taken from notes by Dr. Neil Moore
Yan Shi CS/SE 2630 Lecture Notes
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Recursion.
Dr. Sampath Jayarathna Cal Poly Pomona
Last Class We Covered Recursion Stacks Parts of a recursive function:
The structure of programming
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Thinking procedurally
ITEC324 Principle of CS III
Recursion: The Mirrors
Presentation transcript:

Hassan Khosravi / Geoffrey Tien Recursion October 23, 2017 Hassan Khosravi / Geoffrey Tien

Function/method calls A function or method call is an interruption or aside in the execution flow of a program: ... int a, b, c, d; a = 3; b = 6; c = foo(a, b); d = 9; int foo(int x, int y) { while (x > 0) { y++; x >>= 1; // bitwise right shift by 1 } return y; October 23, 2017 Hassan Khosravi / Geoffrey Tien

Function calls in daily life How do you handle interruptions in daily life? You're at home, working on your CPSC 259 assignment You stop to look up something in the textbook Your roommate/spouse/partner/parent/etc. asks you for help moving some stuff outside Your neighbour tells you a story The doorbell rings LIFO, that's a stack! You stop what you're doing, you memorize where you were in your task, you handle the interruption, and then you go back to what you were doing October 23, 2017 Hassan Khosravi / Geoffrey Tien

Activation records in daily life I am reading about deleting a linked list node in Thareja p. 172 I am working on line 177 of my linkedlist.c file October 23, 2017 Hassan Khosravi / Geoffrey Tien

Activation records in daily life I have moved 2 boxes of old papers to the back alley I am reading about deleting a linked list node in Thareja p. 173 I am working on line 177 of my linkedlist.c file October 23, 2017 Hassan Khosravi / Geoffrey Tien

Activation records in daily life I am listening to my neighbour tell me about some wild party he went to last night I have moved 4 boxes of old papers to the back alley I am reading about deleting a linked list node in Thareja p. 173 I am working on line 177 of my linkedlist.c file October 23, 2017 Hassan Khosravi / Geoffrey Tien

Activation records in daily life I am signing for a FedEx delivery My neighbour is just about to get to the part where he pukes... I have moved 4 boxes of old papers to the back alley I am reading about deleting a linked list node in Thareja p. 173 I am working on line 177 of my linkedlist.c file October 23, 2017 Hassan Khosravi / Geoffrey Tien

Activation records in daily life My neighbour has finally finished his story. "Good for you!" I say sarcastically I have moved 4 boxes of old papers to the back alley I am reading about deleting a linked list node in Thareja p. 173 I am working on line 177 of my linkedlist.c file October 23, 2017 Hassan Khosravi / Geoffrey Tien

Activation records in daily life I have moved 6 boxes of old papers to the back alley I have moved 8 boxes of old papers to the back alley I am reading about deleting a linked list node in Thareja p. 173 I am working on line 177 of my linkedlist.c file October 23, 2017 Hassan Khosravi / Geoffrey Tien

Activation records in daily life I am reading about deleting the last node in a linked list in Thareja p. 174 I am working on line 177 of my linkedlist.c file October 23, 2017 Hassan Khosravi / Geoffrey Tien

Activation records in daily life I finished my linkedlist.c file! October 23, 2017 Hassan Khosravi / Geoffrey Tien

Activation records on a computer A computer handles function/method calls in the same way ... int a, b, c, d; a = 3; b = 6; c = foo(a, b); d = 9; int foo(int x, int y) { while (x > 0) { y++; x >>= 1; // bitwise right shift by 1 } return y; y = 7 y = 6 y = 8 x = 0 x = 1 x = 3 return 8 d = ? d = 9 c = ? c = 8 b = ? b = 6 a = 3 a = ? October 23, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Rabbits! ...and recursion What happens when you put a pair of rabbits in a field? More rabbits! Let’s model the rabbit population, with a few assumptions: Newly-born rabbits take one month to reach maturity and mate Each pair of rabbits produces another pair of rabbits one month after mating Rabbits never die October 23, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien More rabbits... How many rabbit pairs are there after 5 months? Month 1 2 3 4 5 6 Month 1: start – 1 pair Month 2: first pair are now mature and mate – 1 pair 1 1 2 3 5 8 Month 3: first pair give birth to a pair of babies – original pair + baby pair = 2 pairs Month 5: the 3 pairs from month 4, and two new pairs – 5 pairs Month 4: first pair give birth to another pair of babies, pair born in month 3 are now mature – 3 pairs Month 6: the 5 pairs from month 5, and three new pairs – 8 pairs And so on... October 23, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Fibonacci series The nth number in the Fibonacci series, fib(n), is: 0 if n = 0, and 1 if n = 1 fib(n – 1) + fib(n – 2) for any n > 1 e.g. what is fib(23) Easy if we only knew fib(22) and fib(21) The answer is fib(22) + fib(21) What happens if we actually write a function to calculate Fibonacci numbers like this? October 23, 2017 Hassan Khosravi / Geoffrey Tien

Calculating the Fibonacci series Let’s write a function just like the formula fib(n) = 0 if n = 0, 1 if n = 1, otherwise fib(n) = fib(n – 1) + fib(n – 2) int fib(int n) { if (n <= 0) return 0; else if (n == 1) return 1; else return fib(n-1) + fib(n-2); } The function calls itself October 23, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Recursive functions The Fibonacci function is recursive A recursive function calls itself Each call to a recursive method results in a separate call to the method, with its own input Recursive functions are just like other functions The invocation (e.g. parameters, etc.) is pushed onto the call stack And removed from the call stack when the end of a method or a return statement is reached Execution returns to the previous method call October 23, 2017 Hassan Khosravi / Geoffrey Tien

Recursive function anatomy Recursive functions do not use loops to repeat instructions But use recursive calls, in if statements Recursive functions consist of two or more cases, there must be at least one Base case, and Recursive case October 23, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Recursion cases The base case is a smaller problem with a known solution This problem’s solution must not be recursive Otherwise the function may never terminate There can be more than one base case And base cases may be implicit The recursive case is the same problem with smaller input The recursive case must include a recursive function call There can be more than one recursive case if the problem is small enough to be solved directly solve it otherwise recursively apply the algorithm to one or more smaller instances use the solution(s) from smaller instances to solve the problem October 23, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Analysis of fib(5) int fib(int n) { if (n <= 0) return 0; else if (n == 1) return 1; else return fib(n-1) + fib(n-2); } 5 fib(5) 2 3 fib(4) fib(3) 1 2 1 1 fib(3) fib(2) fib(2) fib(1) 1 1 1 1 fib(2) fib(1) fib(1) fib(0) fib(1) fib(0) 1 Later in the course we may see how this is an extremely inefficient way to compute the Fibonacci series fib(1) fib(0) October 23, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Thinking recursively Opening a present Your friends have given you a camera as a birthday present. To prolong the suspense when opening the present, they have bundled it into several nested wrapped packages. How do you open the present? openPresent(pkg) { if you can see the actual gift say "thank you" else open the box to reveal spkg openPresent(spkg) } October 23, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Next class... Recursion, continued! with more examples October 23, 2017 Hassan Khosravi / Geoffrey Tien