1 Chapter 3. Recursion Lecture 6. In functions and data structures.

Slides:



Advertisements
Similar presentations
LEVEL II, TERM II CSE – 243 MD. MONJUR-UL-HASAN LECTURER DEPT OF CSE, CUET Recursive & Dynamic Programming.
Advertisements

Introduction to Recursion and Recursive Algorithms
© 2004 Goodrich, Tamassia Using Recursion1 Programming with Recursion.
Recursion. Binary search example postponed to end of lecture.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Unit 191 Recursion General Algorithm for Recursion When to use and not use Recursion Recursion Removal Examples Comparison of the Iterative and Recursive.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursion.
Revision Using Recursion1 Recursion. Revision Using Recursion2 Recall the Recursion Pattern Recursion: when a method calls itself Classic example--the.
© 2004 Goodrich, Tamassia Using Recursion1. © 2004 Goodrich, Tamassia Using Recursion2 Recall the Recursion Pattern (§ 2.5) Recursion: when a method calls.
Programming with Recursion
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.
Unit 181 Recursion Definition Recursive Methods Constructing Recursion Benefits and Usage Infinite Recursion Recursion Removal Examples Exercises.
Recursion CS Goals Discuss recursion as another form of repetition Do the following tasks, given a recursive routine Determine whether the routine.
Recursion.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Introduction to Recursion by Dr. Bun Yue Professor of Computer Science 2013
1 Object-Oriented Programming Using C++ CLASS 2. 2 Linear Recursion Summing the Elements of an Array Recursively Algorithm LinearSum(A, n): Input: An.
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
Recursion.
Introduction to Recursion CSCI 3333 Data Structures.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
M180: Data Structures & Algorithms in Java
Recursive. 2 Recursive Definitions In a recursive definition, an object is defined in terms of itself. We can recursively define sequences, functions.
Recursion Chapter 11. The Basics of Recursion: Outline Introduction to Recursion How Recursion Works Recursion versus Iteration Recursive Methods That.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Stephen P. Carl - CS 2421 Recursion Reading : Chapter 4.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Chapter 15 Recursion INTRODUCTION Recursion is a program-solving technique that expresses the solution of a problem in terms of the solutions of.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
CSE 501N Fall ‘09 12: Recursion and Recursive Algorithms 8 October 2009 Nick Leidenfrost.
Advanced Computer Architecture and Parallel Processing Rabie A. Ramadan http:
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
1 Recursion n what is it? n how to build recursive algorithms n recursion analysis n tracing simple recursive functions n hands on attempts at writing.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Chapter 111 Recursion Chapter Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar.
23 February Recursion and Logarithms CSE 2011 Winter 2011.
R ECURRSION Prepared by Miss Simab Shahid Lecturer computer Science and Software Engineering department, University of Hail Chapter.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Given a node v of a doubly linked list, we can easily insert a new node z immediately after v. Specifically, let w the be node following v. We execute.
Data Structures I (CPCS-204) Week # 5: Recursion Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
Welcome to Recursion! Say what?!? Recursion is… the process of solving large problems by simplifying them into smaller ones. similar to processing using.
Recursion Version 1.0.
Recursion 5/4/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
RECURSION.
Recursion DRILL: Please take out your notes on Recursion
Recursion and Logarithms
Java 4/4/2017 Recursion.
Ch 12 Recursion Mr. Dave Clausen La Cañada High School
Chapter 12 Supplement: Recursion with Java 1.5
Basics of Recursion Programming with Recursion
Programming with Recursion
Programming with Recursion
CS210- Lecture 3 Jun 6, 2005 Announcements
Presentation transcript:

1 Chapter 3. Recursion Lecture 6

In functions and data structures

5 What is recursion? ADS2 lecture 6 It is something defined in terms of itself … but it must have a stopping condition! … it must “bottom out”

7 Recursive functions A recursive function is one that calls itself. E.g. to calculate N! = N  (N-1)  (N-2) ... 1, do the following: if N = 0 then 1 else N  (N-1)! We have defined the factorial function ! In terms of itself. –Note the factorial function is applied to a smaller number every time until it is applied to 0. ADS2 lecture 6

The factorial function in Java

fact(4) =

The factorial function in Java fact(4) = 4 * fact(3)

The factorial function in Java fact(4) = 4 * fact(3) = 4 * 3 * fact(2)

The factorial function in Java fact(4) = 4 * fact(3) = 4 * 3 * fact(2) = 4 * 3 * 2 * fact(1)

The factorial function in Java fact(4) = 4 * fact(3) = 4 * 3 * fact(2) = 4 * 3 * 2 * fact(1) = 4 * 3 * 2 * 1 * fact(0)

The factorial function in Java fact(4) = 4 * fact(3) = 4 * 3 * fact(2) = 4 * 3 * 2 * fact(1) = 4 * 3 * 2 * 1 * fact(0) = 4 * 3 * 2 * 1 * 1

The factorial function in Java fact(4) = 4 * fact(3) = 4 * 3 * fact(2) = 4 * 3 * 2 * fact(1) = 4 * 3 * 2 * 1 * fact(0) = 4 * 3 * 2 * 1 * 1 = 4 * 3 * 2 * 1

The factorial function in Java fact(4) = 4 * fact(3) = 4 * 3 * fact(2) = 4 * 3 * 2 * fact(1) = 4 * 3 * 2 * 1 * fact(0) = 4 * 3 * 2 * 1 * 1 = 4 * 3 * 2 * 1 = 4 * 3 * 2

The factorial function in Java fact(4) = 4 * fact(3) = 4 * 3 * fact(2) = 4 * 3 * 2 * fact(1) = 4 * 3 * 2 * 1 * fact(0) = 4 * 3 * 2 * 1 * 1 = 4 * 3 * 2 * 1 = 4 * 3 * 2 = 4 * 6

The factorial function in Java fact(4) = 4 * fact(3) = 4 * 3 * fact(2) = 4 * 3 * 2 * fact(1) = 4 * 3 * 2 * 1 * fact(0) = 4 * 3 * 2 * 1 * 1 = 4 * 3 * 2 * 1 = 4 * 3 * 2 = 4 * 6 = 24

19 In general… A recursive function when calling itself makes a clone and calls the clone with appropriate parameters. Important: a workable recursive algorithm (function/procedure) must always: Rule 1: reduce size of data set, or the number its working on, each time it is recursively called and Rule 2: provide a stopping case (terminating condition) Can always replace recursive algorithm with an iterative one, but often recursive solution much sleeker Factorial function is only simple example, but recursion is valuable tool in more complex algorithms. ADS2 lecture 6

Linear recursion 20 With linear recursion a method is defined so that it makes at most one recursive call each time it is invoked. Useful when we view an algorithmic problem in terms of a first and/or last element plus a remaining set with same structure as original set. Example 1 : factorial example Example 2: summing the elements of an array - Algorithm LinearSum(A,n): Input: An integer array A and integer n  1, such that A has at least n elements Output:The sum of the first n integers in A if n=1 then return A[0] else return LinearSum(A,n-1)+A[n-1] This is pseudocode. Used in GoTa a lot. ADS2 lecture 6

21 Visualizing Recursion Recursion trace A box for each recursive call An arrow from each caller to callee An arrow from each callee to caller showing return value Example recursion trace: RecursiveFactorial(4) RecursiveFactorial(3) RecursiveFactorial(2) RecursiveFactorial(1) RecursiveFactorial(0) return1 call return1*1=1 2*1=2 3*2=6 4*6=24 final answer call ADS2 lecture 6 Actually we will remove the blue arrows on the right, to make it simpler

More examples

Raise m to the power n

We now have a recursive definition

Example 3: RaisePower Algorithm RaisePower(m,n): Input: Integers m,n Output: value of m n if n=0 then return 1 else return (m*RaisePower(m,n-1))

Example 3: RaisePower Algorithm RaisePower(m,n): Input: Integers m,n Output: value of m n if n=0 then return 1 else return (m*RaisePower(m,n-1)) Does RaisePower satisfy rules 1 and 2? Yes!

Example 3: RaisePower Algorithm RaisePower(m,n): Input: Integers m,n Output: value of m n if n=0 then return 1 else return (m*RaisePower(m,n-1)) Does RaisePower satisfy rules 1 and 2? Yes! Rule 1 (number function working on is decreased)

Example 3: RaisePower Algorithm RaisePower(m,n): Input: Integers m,n Output: value of m n if n=0 then return 1 else return (m*RaisePower(m,n-1)) Does RaisePower satisfy rules 1 and 2? Yes! Rule 1 (number function working on is decreased) Rule 2 ( stopping case)

ADS2 lecture 629 Recursion trace, m=2, n=4 RaisePower(2,4) return 1 return 2*1=2 return 2*8=16final answer call RaisePower(2,3) RaisePower(2,2) RaisePower(2,1) RaisePower(2,0) return 2*2=4 return 2*4=8

And in Java… 30 public static int raisePower(int m, int n){ if (n==0) return 1; else return(m*raisePower(m,n-1)); } ADS2 lecture 6

Another example … has9

has9 Given an integer (base 10) does it contain the digit 9?

33 Algorithm Has9(n): Input: Integer n Output: whether 9 appears in decimal expansion of n if (n mod 10)=9 then return true else if (n<10) then return false else return Has9(n/10) ADS2 lecture 6 has9

34 Does Has9 satisfy rules 1 and 2? Yes! Algorithm Has9(n): Input: Integer n Output: whether 9 appears in decimal expansion of n if (n mod 10)=9 then return true else if (n<10) then return false else return Has9(n/10) ADS2 lecture 6 has9

35 Does Has9 satisfy rules 1 and 2? Yes! Rule 1 (number function working on is decreased) Algorithm Has9(n): Input: Integer n Output: whether 9 appears in decimal expansion of n if (n mod 10)=9 then return true else if (n<10) then return false else return Has9(n/10) ADS2 lecture 6 has9

36 Does Has9 satisfy rules 1 and 2? Yes! Rule 1 (number function working on is decreased) Rule 2 ( stopping case) Algorithm Has9(n): Input: Integer n Output: whether 9 appears in decimal expansion of n if (n mod 10)=9 then return true else if (n<10) then return false else return Has9(n/10) ADS2 lecture 6 has9

37 Does Has9 satisfy rules 1 and 2? Yes! Rule 1 (number function working on is decreased) Rule 2 ( stopping case) Algorithm Has9(n): Input: Integer n Output: whether 9 appears in decimal expansion of n if (n mod 10)=9 then return true else if (n<10) then return false else return Has9(n/10) ADS2 lecture 6 has9

ADS2 lecture 638

ADS2 lecture 639 McCreesh & Prosser arXiv : v1

ADS2 lecture 640

ADS2 lecture 641 Matula & Beck JACM 30(3) 1983

ADS2 lecture 642 Ostergard DAM 120 (2002)

ADS2 lecture 643 San Segundo C&OR 38 (2011)

ADS2 lecture 644 Fleiner, Irving & Manlove TCS 412 (2011)

ADS2 lecture 645

example reverse

Reverse an array

Example … for you palindrome

example Binary search

Note overloading

example All permutations of a string

perm

Ouch!

Arithmetic! (on natural numbers) example Similar to … Church Numerals

Recursive Arithmetic

example list

We have been defining lists in terms of nodes, where a node has - a head (we call it “element” where we store data) - a tail (we call it “next” and it points to the next node or null) This IS inherently recursive

list

NOTE: this is NOT destructive! It produces a new list!

Tail recursion Recursion is useful tool for designing algorithms with short, elegant definitions But comes at a cost – need to use memory to keep track of the state of each recursive call When memory is of primary concern, useful to be able to derive non-recursive algorithms from recursive ones. Can use a stack data structure to do this (see ADT chapter), but some cases we can do it more easily and efficiently i.e. when algorithms use tail recursion An algorithm uses tail recursion when recursion is linear and recursive call is its very last operation 71ADS2 lecture 6