Top Banner
1 Chapter 3. Recursion Lecture 6
71

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

Jan 02, 2016

Download

Documents

Ruby Craig
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Chapter 3. Recursion Lecture 6. In functions and data structures.

1

Chapter 3. Recursion

Lecture 6

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

In functions and data structures

Page 4: 1 Chapter 3. Recursion Lecture 6. In functions and data structures.
Page 5: 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”

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

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

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

The factorial function in Java

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

The factorial function in Java

fact(4) =

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

The factorial function in Java

fact(4) = 4 * fact(3)

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

The factorial function in Java

fact(4) = 4 * fact(3) = 4 * 3 * fact(2)

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

The factorial function in Java

fact(4) = 4 * fact(3) = 4 * 3 * fact(2) = 4 * 3 * 2 * fact(1)

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

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)

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

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

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

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

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

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

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

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

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

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

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

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

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

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 exampleExample 2: summing the elements of an array -

Algorithm LinearSum(A,n): Input: An integer array A and integer n1, 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

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

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)

return 1

call

call

call

call

return 1*1 = 1

return 2*1 = 2

return 3*2 = 6

return 4*6 = 24 final answercall

ADS2 lecture 6

Actually we will remove the blue arrows on the right, to make it simpler

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

More examples

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

Raise m to the power n

...... 21 nnn mmmmmm

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

Raise m to the power n

1

.0

1

m

mmm nn

We now have a recursive definition

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

Example 3: RaisePower

Algorithm RaisePower(m,n): Input: Integers m,n Output: value of mn

if n=0 then return 1 else return (m*RaisePower(m,n-1))

1

.0

1

m

mmm nn

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

Example 3: RaisePower

Algorithm RaisePower(m,n): Input: Integers m,n Output: value of mn

if n=0 then return 1 else return (m*RaisePower(m,n-1))

1

.0

1

m

mmm nn

Does RaisePower satisfy rules 1 and 2? Yes!

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

Example 3: RaisePower

Algorithm RaisePower(m,n): Input: Integers m,n Output: value of mn

if n=0 then return 1 else return (m*RaisePower(m,n-1))

1

.0

1

m

mmm nn

Does RaisePower satisfy rules 1 and 2? Yes!

Rule 1 (number function working on is decreased)

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

Example 3: RaisePower

Algorithm RaisePower(m,n): Input: Integers m,n Output: value of mn

if n=0 then return 1 else return (m*RaisePower(m,n-1))

1

.0

1

m

mmm nn

Does RaisePower satisfy rules 1 and 2? Yes!

Rule 1 (number function working on is decreased)

Rule 2 ( stopping case)

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

ADS2 lecture 6 29

Recursion trace, m=2, n=4

RaisePower(2,4)

return 1

return 2*1=2

return 2*8=16 final answercall

call

call

call

call

RaisePower(2,3)

RaisePower(2,2)

RaisePower(2,1)

RaisePower(2,0)

return 2*2=4

return 2*4=8

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

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

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

Another example … has9

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

has9

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

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

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

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

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

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

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

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

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

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

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

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

ADS2 lecture 6 38

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

ADS2 lecture 6 39

McCreesh & ProsserarXiv :1209.45601v1

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

ADS2 lecture 6 40

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

ADS2 lecture 6 41

Matula & Beck JACM 30(3) 1983

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

ADS2 lecture 6 42

OstergardDAM 120 (2002)

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

ADS2 lecture 6 43

San SegundoC&OR 38 (2011)

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

ADS2 lecture 6 44

Fleiner, Irving & ManloveTCS 412 (2011)

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

ADS2 lecture 6 45

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

example

reverse

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

Reverse an array

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

Reverse an array

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

Reverse an array

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

Reverse an array

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

Example … for you

palindrome

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

example

Binary search

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

Binary search

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

Binary search

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

Binary search

Note overloading

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

example

All permutations of a string

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

perm

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

perm

Ouch!

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

Arithmetic! (on natural numbers)

example

Similar to … Church Numerals

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

Recursive Arithmetic

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

Recursive Arithmetic

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

Recursive Arithmetic

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

Recursive Arithmetic

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

example

list

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

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

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

list

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

list

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

list

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

list

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

list

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

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

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