Fondamenti di Informatica Recursion Prof. Emiliano Casalicchio

Slides:



Advertisements
Similar presentations
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
Advertisements

Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate.
Recursion. Binary search example postponed to end of lecture.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Fall 2008ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
1 Recursion Overview l Introduction to recursion and recursive methods l Simple popular recursive algorithms l Writing recursive methods l Preview: 1-D.
28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.
Recursion!. Can a method call another method? YES.
1 Memory Model of A Program, Methods Overview l Memory Model of JVM »Method Area »Heap »Stack.
Recursion.  Identify recursive algorithms  Write simple recursive algorithms  Understand recursive function calling  With reference to the call stack.
1 Recursion Dr. Bernard Chen Ph.D. University of Central Arkansas.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
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.
CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)
Algorithmic Recursion. Recursion Alongside the algorithm, recursion is one of the most important and fundamental concepts in computer science as well.
Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation.
M180: Data Structures & Algorithms in Java
Lecture 10 Recursion CSE225: Data Structures. 2 A Look Back at Functions #include double distance(double x1, double y1, double x2, double y2) { double.
Week 5 - Monday.  What did we talk about last time?  Linked list implementations  Stacks  Queues.
Fall Week 3 CSCI-141 Scott C. Johnson.  Say we want to draw the following figure ◦ How would we go about doing this?
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
CMPS 1371 Introduction to Computing for Engineers RECURSION.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
ICS220 – Data Structures and Algorithms Dr. Ken Cosh Week 5.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Engineering Computation with MATLAB Second Edition by David M. Smith.
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
Covenant College November 5, Douglas R. Sizemore, Ph.D. Professor of Computer Science COS 131: Computing for Engineers Chapter 9: Recursion This.
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.
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:
1 Recursion. 2 A process by which a function calls itself repeatedly  Either directly. X calls X  Or cyclically in a chain. X calls Y, and Y calls X.
A Brief Introduction to Recursion. Recursion Recursive methods … –methods that call themselves! –They can only solve a base case –So, you divide a problem.
1 Programming for Engineers in Python Autumn Lecture 8: Recursion.
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.
递归算法的效率分析. When a function is called... A transfer of control occurs from the calling block to the code of the function --It is necessary that there be.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
General Computer Science for Engineers CISC 106 Lecture 09 Dr. John Cavazos Computer and Information Sciences 03/04/2009.
Recursion Function calling itself
Recursion.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Recursion CENG 707.
Recursion CENG 707.
Topic: Recursion – Part 2
Topic 6 Recursion.
Introduction to Recursion
Recursion Lakshmish Ramaswamy.
Data Structures and Algorithms
Recursion: The Mirrors
C Functions -Continue…-.
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
Hassan Khosravi / Geoffrey Tien
Fibonacci Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 Definition:
Chapter 9 Recursion.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Recursion Recursion is a math and programming tool
Unit 3 Test: Friday.
Stack Frames and Functions
Module 1-10: Recursion.
Introduction to Problem Solving and Programming
Dr. Sampath Jayarathna Cal Poly Pomona
Last Class We Covered Recursion Stacks Parts of a recursive function:
Where is all the knowledge we lost with information? T. S. Eliot
Presentation transcript:

Fondamenti di Informatica Recursion Prof. Emiliano Casalicchio

1-2 Concept: Basic Stack Operations The Activation Stack is a stack used by the operating system to manage the execution of function calls

1-3 Concept: The Activation Stack When the user starts a program, the operating system allocates enough memory to load the application and a block of memory to contain its activation stack. When the user starts a program, the operating system allocates enough memory to load the application and a block of memory to contain its activation stack. When that application calls a function, the parameters and local variables occupy a stack frame that is pushed onto the activation stack. When that application calls a function, the parameters and local variables occupy a stack frame that is pushed onto the activation stack. The calling program is then suspended and control is passed to the function specified in the new frame. The calling program is then suspended and control is passed to the function specified in the new frame. When that function completes, its frame is destroyed; any returned data and control is returned to the frame beneath. When that function completes, its frame is destroyed; any returned data and control is returned to the frame beneath. Note that in a MATLAB implementation, the stack frame is the storage environment for the current workspace. Note that in a MATLAB implementation, the stack frame is the storage environment for the current workspace.

1-4 Function Specification vs Function Instances The file defining the function merely specifies how it would behave if you ever called it. This is the function specification. The file defining the function merely specifies how it would behave if you ever called it. This is the function specification. When you call the function, a stack frame is created, parameter values are supplied and you have a fully defined instance of that function. When you call the function, a stack frame is created, parameter values are supplied and you have a fully defined instance of that function. There is no reason in principle why a function cannot “call itself,” because each call is a different instance of the function. There is no reason in principle why a function cannot “call itself,” because each call is a different instance of the function.

Recursion Defined For a recursive program to succeed, it must satisfy three basic requirements: n There must be a terminating condition specifying when to stop the recursion n The function must call a clone of itself (with different parameters) n The parameter change must move the data towards the terminating condition

1-6 Implementing a Recursive Function in MATLAB The MATLAB template for recursion is: function = ( ) % % if if = = elseif elseif = =... else =... =... (,... (,... ( ) ) ( ) )end

Factorial n!=n*(n-1)!, 0!=1 (by def) n!=n*(n-1)!, 0!=1 (by def) n 3!=3*2!=3*2*1!=3*2*1*0!=3*2*1*1=6 function result = fact(N) % recursive computation of N! % recursive computation of N! % fprintf('fact( %d )\n', N); % testing only % fprintf('fact( %d )\n', N); % testing only if N == 0 if N == 0 result = 1; result = 1; else else result = N * fact(N - 1); result = N * fact(N - 1); end end 7

fact(3) N=3, fact(3), N=3, fact(3), N=2, fact(2), N=2, fact(2), N=1, fact(1), N=1, fact(1), N=0, fact(0), N=0, fact(0), 8 result = 1 result = 2 result = 6

1-9 The Fibonacci Series Leonardo Pisano Fibonacci, studied rabbit populations Leonardo Pisano Fibonacci, studied rabbit populations Starting with a pair of newborn rabbits, he wanted to calculate the rabbit population after a year. Starting with a pair of newborn rabbits, he wanted to calculate the rabbit population after a year. fib(n) = fib(n-1) + fib(n-2) This is, of course, recursive where fib(1) and fib(2) are both 1. This is, of course, recursive where fib(1) and fib(2) are both 1. Computationally, this turns out to be very unpleasant Computationally, this turns out to be very unpleasant Results in the series: Results in the series: n 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,89, 144, 233, …, This series occurs frequently in nature This series occurs frequently in nature

1-10 Fibonacci Rabbits

1-11 Fibonacci in Nature

fib(5) N=5, fib(5)=fib(4)+fib(3), N=5, fib(5)=fib(4)+fib(3), N=4, fib(4)=fib(3)+fib(2), N=4, fib(4)=fib(3)+fib(2), N=3, fib(3)=fib(2)+fib(1), N=3, fib(3)=fib(2)+fib(1), N=2, fib(2)=1 N=2, fib(2)=1 N=1, fib(1)=1 N=1, fib(1)=1 N=3, result=2 N=3, result=2 N=2, fib(2)=1 N=2, fib(2)=1 N=4 result=3 N=4 result=3 N=3.... N=3.... N=5 result=5 N=5 result=5

Palindromes alla alla onorarono onorarono Ingegni Ingegni radar radar anna anna otto otto parlo col rap parlo col rap i topi non avevano nipoti i topi non avevano nipoti 13

Palindromes function ans = isPal(str) % recursive palindrome detector % recursive palindrome detector if length(str) < 2 if length(str) < 2 ans = true; ans = true; elseif str(1) ~= str(end) elseif str(1) ~= str(end) ans = false; ans = false; else else ans = isPal(str(2:end-1)); ans = isPal(str(2:end-1)); end end 14

1-15 A Zero Crossing Problem Given an unknown function y = f(x), Given an unknown function y = f(x),

1-16 A Zero Crossing Problem