Recursion Salim Arfaoui.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Recursion. Binary search example postponed to end of lecture.
Proof Techniques and Recursion. Proof Techniques Proof by induction –Step 1: Prove the base case –Step 2: Inductive hypothesis, assume theorem is true.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Recursion.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms.
© 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.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 12 Recursion.
Lecture 10 Recursion CSE225: Data Structures. 2 A Look Back at Functions #include double distance(double x1, double y1, double x2, double y2) { double.
Lecture 12 Recursion part 1 Richard Gesick. Recursion A recursive method is a method that calls itself. A recursive method is capable of solving only.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
ISOM MIS 215 Module 4 – Recursion. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Recursive Algorithms A recursive algorithm calls itself to do part of its work The “call to itself” must be on a smaller problem than the one originally.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Recursion Chapter 10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Recursion Chapter 10 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Recursion To understand recursion, you first have to understand recursion.
Recursion.
CS212: Data Structures and Algorithms
Sections 4.1 & 4.2 Recursive Definitions,
Recursion.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 19: Recursion.
Chapter 15 Recursion.
CprE 185: Intro to Problem Solving (using C)
Recursion DRILL: Please take out your notes on Recursion
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Chapter 15 Recursion.
Recursion
Recursion, Tail Recursion
Introduction to Computer Science - Alice
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Algorithm Analysis (for Divide-and-Conquer problems)
Recursion Chapter 11.
Lecture 17 Recursion part 1 Richard Gesick.
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion Data Structures.
Recursion.
And now for something completely different . . .
Lecture 12 Recursion part 1 CSE /26/2018.
Chapter 19: Recursion.
Dr. Sampath Jayarathna Cal Poly Pomona
Main() { int fact; fact = Factorial(4); } main fact.
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Recursion.
Recursive Thinking.
Recursive Function Prepared by Harprith ICT2102 Introduction to Data Structure.
Advanced Analysis of Algorithms
Presentation transcript:

Recursion Salim Arfaoui

Outline Learning objectives: Mystery to solve!!!! In this lecture we will: Study recursion. look at the stack . Learning objectives: Learn what a recursion is and when we need it. Why learn recursion. Demonstrate how a recursive process works. Give an insight of how a recursive function is realized. Mystery to solve!!!!

Recursion Definition Objects are defined in terms of other objects of the same type. Describing an action in terms of itself. Solving a problem using smaller occurrences of the same problem.

Recursion Example

Recursive Programming Definition A technique in which: Procedures and functions call themselves. Divide the bigger problem into a succession of smaller problems Successive repetitions are processed up to the (Base Case)

Recursive Programming Example Find your way home: If you are at home, stop moving. Take one step toward home. "find your way home".

Recursive Programming Factorial Example Problem: calculate n! (n factorial) Formula: n! = 1 if n = 0 n! = n · · ·3·2·1 if n > 0 5! = 5 · 4 · 3·2·1 if n = 5 n! = n·(n-1)·(n – 2) · · ·2·1 if n > 0 Recursively: if n = 0, then n! = 1 Base Case if n > 0, then n! = n * (n-1)! General Case

Recursive Programming Factorial Example * Iterative Solution: int factorial (int n){ int result; for (int i = 1; i <= n; i++) {     result = result * i; } return result;} * Recursive Solution: int factorial (int n){ if(n<=1) Base Case return 1; else return n * factorial(n-1);} Recursive call

Recursive Programming Factorial Example Fact. 6th: N=0, Finished: returns 1 Stack Fact. 5th: N=1, Unfinished: 1*Fact(0) Fact. 4th: N=2, Unfinished: 2*Fact(1) Fact (1) Fact. 3rd: N=3, Unfinished: 3*Fact(2) Fact (2) Fact. 2nd: N=4, Unfinished: 4*Fact(3) Fact (3) Fact. 1st: N=5, Unfinished: 5*Fact(4) Fact (4) Main Algorithm: Unfinished: answer <- Fact (5) Fact (5)

Recursive Programming Factorial Example Stack Fact. 5th: N=1, Finished: returns 1*1 Fact. 4th: N=2, Unfinished: 2*Fact(1) Fact (1) Fact. 3rd: N=3, Unfinished: 3*Fact(2) Fact (2) Fact. 2nd: N=4, Unfinished: 4*Fact(3) Fact (3) Fact. 1st: N=5, Unfinished: 5*Fact(4) Fact (4) Main Algorithm: Unfinished: answer <- Fact (5) Fact (5)

Recursive Programming Factorial Example Stack Fact. 4th: N=2, Finished: returns 2*1 Fact (1) Fact. 3rd: N=3, Unfinished: 3*Fact(2) Fact (2) Fact. 2nd: N=4, Unfinished: 4*Fact(3) Fact (3) Fact. 1st: N=5, Unfinished: 5*Fact(4) Fact (4) Main Algorithm: Unfinished: answer <- Fact (5) Fact (5)

Recursive Programming Factorial Example Stack Fact. 3rd: N=3, Finished: returns 3*2 Fact (2) Fact. 2nd: N=4, Unfinished: 4*Fact(3) Fact (3) Fact. 1st: N=5, Unfinished: 5*Fact(4) Fact (4) Main Algorithm: Unfinished: answer <- Fact (5) Fact (5)

Recursive Programming Factorial Example Stack Fact. 2nd: N=4, Finished: returns 4*6 Fact (3) Fact. 1st: N=5, Unfinished: 5*Fact(4) Fact (4) Main Algorithm: Unfinished: answer <- Fact (5) Fact (5)

Recursive Programming Factorial Example Stack Fact. 1st: N=5, Finished: returns 5*24 Fact (4) Main Algorithm: Unfinished: answer <- Fact (5) Fact (5)

Recursive Programming Factorial Example Main Algorithm: Finished: answer <- 120

Mystery!!!! When will the world end ?! Mystery: A 64-disk version of the puzzle lies in a Hanoi monastery, where monks work continuously toward solving the puzzle. When they complete the puzzle, the world will come to an end. Objective: Move the entire stack to another Pole Rules: 1) Only one disk can be moved at a time. 2) A disk can only be moved if it is the uppermost disk. 3) No disk may be placed on top of a smaller disk.

Mystery!!!! When will the world end ?! T(n) = 264 - 1 How long would it take the monks to solve the puzzle? Move 1 disc per millisecond: Approximately 584,600,000 years.  Move 1 disc per second: roughly 585 billion years (18,446,744,073,709,551,615 turns)

For Next Time Types of recursion Linear Recursive: only makes a single call to itself each time the it runs (Factoriel) Tail recursive:  the recursive call is the last thing the function does (GCD) Binary Recursive: functions with two recursive calls. Nested Recursion: one of the arguments to the recursive function is the recursive function itself! (Ackerman's function) Mutual Recursion: doesn't necessarily need to call itself. function A calls function B which calls function C which in turn calls function A.