1 Programming for Engineers in Python Autumn 2011-12 Lecture 8: Recursion.

Slides:



Advertisements
Similar presentations
Basics of Recursion Programming with Recursion
Advertisements

Introduction to Recursion and Recursive Algorithms
Recursion vs. Iteration The original Lisp language was truly a functional language: –Everything was expressed as functions –No local variables –No iteration.
Chapter 16 Recursion: Another Control Mechanism. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. a.
Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.
Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate.
Fall 2008Programming Development Techniques 1 Topic 3 Linear Recursion and Iteration September 2008.
1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can: Design recursive algorithms to solve.
Chapter 10 Recursion Instructor: alkar/demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe.
Recursion. Binary search example postponed to end of lecture.
1 Gentle Introduction to Programming Session 2: Functions.
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.
16/23/2015 9:48 AM6/23/2015 9:48 AM6/23/2015 9:48 AMRecursion Recursion Recursion is when a function calls itself to implement an algorithm. Really a paradigm.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Gentle Introduction to Programming Session 3: Higher Order Functions, Recursion.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
1 Gentle Introduction to Programming Session 3: Recursion.
Data Structures Using C++ 2E Chapter 6 Recursion.
Data Structures Using C++ 2E Chapter 6 Recursion.
1 Storage Classes, Scope, and Recursion Lecture 6.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms.
Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Chapter 15: Advanced Topics: Introducing Data Structures and Recursion Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Advanced Computer Architecture and Parallel Processing Rabie A. Ramadan http:
1 Programming for Engineers in Python Autumn Lecture 12: Dynamic Programming.
Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.
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.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
IB Computer Science Unit 5 – Advanced Topics Recursion.
Chapter 12. Recursion Basics of Recursion Programming with Recursion Computer Programming with JAVA.
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 6 Questions Quick Quiz
1 Lecture 3 Part 2 Storage Classes, Scope, and 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.
7. RECURSIONS Rocky K. C. Chang October 12, 2015.
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.
Review of Recursion  a recursive method calls itself  to prevent infinite recursion you need to ensure that: 1. the method reaches a base case 2. each.
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
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.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Recursion.
CS212: Data Structures and Algorithms
Recursion.
CMSC201 Computer Science I for Majors Lecture 20 – Recursion (Continued) Prof. Katherine Gibson Based on slides from UPenn’s CIS 110, and from previous.
Chapter 15 Recursion.
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Recursion Lakshmish Ramaswamy.
Chapter 15 Recursion.
Programming for Engineers in Python
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.
Applied Algorithms (Lecture 17) Recursion Fall-23
Chapter 12 Recursion (methods calling themselves)
Recursion Chapter 11.
Recursion Data Structures.
Unit 3 Test: Friday.
CSC 143 Recursion.
Dr. Sampath Jayarathna Cal Poly Pomona
Last Class We Covered Recursion Stacks Parts of a recursive function:
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

1 Programming for Engineers in Python Autumn Lecture 8: Recursion

2 Lecture 7: Highlights

3 Lecture 6 (OOP): Highlights Representation design options and implications (Rectangle) Inheritance and Polymorphism Rational - customize classes so that they are natural to use Attributes, methods, constructor Method overriding (e.g., __str__) Operators overloading (e.g., __add__)

4 Variables in Functions Defined only in the function’s scope Are not available for other functions/shell (even function’s parameters) Upon completion of execution: Variables are not defined anymore Variables values are not kept for future invocations of the function

5 The Call Stack call stack result main base exponent res i power = 2 = 20 = 3 = 15 = 5 = 17

6 Towers of Hanoi Objective: Move the entire stack of disks from rode S (source) to T (target) using A (auxiliary) STA

7 The Rules Only one disk may be moved at a time Each move consists of taking the upper disk from one of the rods and sliding it onto another rod No disk may be placed on top of a smaller disk STA

8 Examples STA 1 disk – very easy! STA 2 disks – very easy! STA 3 disks – easy STA 4 disks?

9 Solution for 4 Disks STASTASTASTA

10 For n Disks? STA

11 Assume We Have a Solution for n-1 Disks STA

12 Solution: Step 1 STA

13 Solution: Step 2 STA

14 Solution: Step 3 STA

15 How to Solve for n-1 Disks? Easy!

16 Algorithm for n Disks STA 1. If there is only a single disk – easy! 2. Move n-1 disks from S to A (T is the auxiliary rode) 3. Move disk from S to T 4. Move n-1 disks from A to T (S is the auxiliary rode) Let us write it in Python…

17 A Python Program for Towers of Hanoi

18 Remember the Calling Stack? hanoi(3,'S','T','A') main hanoi(2,'S',‘A',‘T')hanoi(1,‘S',‘T',‘A') And so on…

19 Recursion We use Recursion to solve the Towers of Hanoi problem Recursion: a function whose implementation references itself Recursion enables to solve a “large” problem using solutions to “small” problems that assemble it In every recursive call the problem is reduced. When the problem is small enough -solve directly (base case, מקרה קצה) Divide and conquer

20 Iterative Versus Recursive Step by step (iteratively): 4! = 1*2*3*4 = 2*3*4 = 6*4 = = 2*2*2*2 = 4*2*2 = 8*2 = 16 n! = 1*2*3*….*n a n = a*a*…..*a n iterations n! = n*(n-1)! 0! = 1 Recursively: 4! = 4*3! = 4*(3*2!) = 4*(3*(2*1!)) = 4*(3*(2*(1*0!))) = 4*(3*(2*(1*1))) = 4*(3*(2*1)) = 4*(3*2) = 4*6 = 24

21 Recursive Definition n! = n*(n-1)! 0! = 1 Base condition מקרה קצה Recursive call Smaller instance Factorial

22 Pros and Cons Pros: s hort and natural for many problems Cons: Computational inefficient, sometimes hard to understand or inconvenient

23 Recursion Example in Python Halting condition (and infinite loops) Advance towards base case

24 Recursive factorial – step by step factorial(4) n 4 Returns…

25 Recursive factorial – step by step factorial(4) n 4 Returns… 4*…

26 Recursive factorial – step by step factorial(4) n 4 Returns… factorial(3) n 3 Returns…

27 Recursive factorial – step by step factorial(4) n 4 Returns… factorial(3) n 3 Returns…

28 Recursive factorial – step by step factorial(4) n 4 Returns… factorial(3) n 3 Returns… 3*…

29 Recursive factorial – step by step factorial(4) n 4 Returns… factorial(3) n 3 Returns… factorial(2) n 2 Returns…

30 Recursive factorial – step by step factorial(4) n 4 Returns… factorial(3) n 3 Returns… factorial(2) n 2 Returns…

31 Recursive factorial – step by step factorial(4) n 4 Returns… factorial(3) n 3 Returns… factorial(2) n 2 Returns… 2*…

32 Recursive factorial – step by step factorial(4) n 4 Returns… factorial(3) n 3 Returns… factorial(2) n 2 Returns… factorial(1) n 1 Returns…

33 Recursive factorial – step by step factorial(4) n 4 Returns… factorial(3) n 3 Returns… factorial(2) n 2 Returns… factorial(1) n 1 Returns…

34 Recursive factorial – step by step factorial(4) n 4 Returns… factorial(3) n 3 Returns… factorial(2) n 2 Returns… factorial(1) n 1 Returns… 1*…

35 Recursive factorial – step by step factorial(4) n 4 Returns… factorial(3) n 3 Returns… factorial(2) n 2 Returns… factorial(1) n 1 Returns… factorial(0) n 0 Returns…

36 Recursive factorial – step by step factorial(4) n 4 Returns… factorial(3) n 3 Returns… factorial(2) n 2 Returns… factorial(1) n 1 Returns… factorial(0) n 0 Returns…

37 Recursive factorial – step by step factorial(4) n 4 Returns… factorial(3) n 3 Returns… factorial(2) n 2 Returns… factorial(1) n 1 Returns… factorial(0) n 0 Returns…

38 Recursive factorial – step by step factorial(4) n 4 Returns… factorial(3) n 3 Returns… factorial(2) n 2 Returns… factorial(1) n 1 Returns… factorial(0) n 0 Returns… 1

39 Recursive factorial – step by step factorial(4) n 4 Returns… factorial(3) n 3 Returns… factorial(2) n 2 Returns… factorial(1) n 1 Returns… 1*1

40 Recursive factorial – step by step factorial(4) n 4 Returns… factorial(3) n 3 Returns… factorial(2) n 2 Returns… 2*1

41 Recursive factorial – step by step factorial(4) n 4 Returns… factorial(3) n 3 Returns… 3*2

42 Recursive factorial – step by step factorial(4) n 4 Returns… 4*6

43 General Form of Recursive Algorithms Base case: small (non-decomposable) problem Recursive case: larger (decomposable) problem At least one base case, and at least one recursive case. test + base case recursive case

44 Short Summary Design a recursive algorithm by 1. Solving big instances using the solution to smaller instances 2. Solving directly the base cases Recursive algorithms have 1. Stopping criteria 2. Recursive case(s) 3. Construction of a solution using solution to smaller instances

45 Point for Attention: Memory Variables of a called function are kept in memory until the function returns Many recursive calls might fill the computers memory Even if we minimize the number of variables, recursion has its price

46 Example: Fibonacci Series Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 Definition: fib(0) = 0 fib(1) = 1 fib(n) = fib(n-1) + fib(n-2) en.wikipedia.org/wiki/Fibonacci_number

47 סלט פיבונאצ'י

48 Recursive Fibonacci Series Every call with n > 1 invokes 2 function calls, and so on…

49 Redundant Calls Fib(4) Fib(3)Fib(2) Fib(1) Fib(0) Fib(1) Fib(0) Fib(5) Fib(3) Fib(2)Fib(1) Fib(0)

50 Number of Calls to Fibonacci nvalueNumber of calls

51 Demonstration: Iterative Versus Recursive Fibonacci

52 Demonstration: Iterative Versus Recursive Fibonacci Output (shell):

53 Fibonacci: Recursion and Efficiency If a recursive function calls itself more than once it will be extremely inefficient Define it iteratively

54 So Do We Really Need Recursion? Yes! (specific examples next week)

55 Odd-Even (if time allows) Given a function ‘odd(n)’ Odd n - return True, Even n – return False Write a function ‘even(n)’ that: Even n - return True, Odd n – return False This is easy!

56 Odd-Even

57 Odd-Even

58 Odd-Even

59 Odd-Even

Questions?

61 Off Topics Documenting your programs Find the error

62 Documenting Your Programs An essential part of writing computer code Understand what was your intension when writing it For others to be able to coordinate their code with yours For the grader/lecturer to try and understand your code and grade it accordingly…

63 Documenting Your Programs with Comments Comments: pieces of code not interpreted or executed by the computer One line comments start with the hash character (#)

64 Meaningful Variables Names Variables names Meaningful names

Off Topic: Find the Error

And Now?

INDENTATION IS REALLY, REALLY IMPORTANT!