38
Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of recursion Linear Tail Mutual Binary Recursion Comp Sci 1575 Data Structures

Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

  • Upload
    others

  • View
    22

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Recursion

Comp Sci 1575 Data Structures

Page 2: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Outline

1 IntroductionRecursive programming

2 Simple examplesExponentiationOrder of executionFactorial

3 The call stackActivation record stackActivation records

4 Types of recursionLinearTailMutualBinary

Page 3: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Definitions

“To understand recursion, you must understand recursion.”

Page 4: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Familiar examples of recursive definitions

• Natural numbers are either:n + 1, where n is a natural number, or1

• Exponentiation:bn = b ∗ bn−1, orb0 = 1

• Factorial:n! = n ∗ ((n − 1)!), or0! = 1

Page 5: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Outline

1 IntroductionRecursive programming

2 Simple examplesExponentiationOrder of executionFactorial

3 The call stackActivation record stackActivation records

4 Types of recursionLinearTailMutualBinary

Page 6: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Recursive programming paradigm

• Recursion in computer science is a self-referentialprogramming paradigm, as opposed to iteration with afor() loop, for example.

• Practically, recursion is a process in which a function callsitself.

• Often, the solution to a problem can employ solutions tosmaller instances of the same problem.

Page 7: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

The formal parts

• Recursive case

• Base case (which often results in termination)

• Condition or test, often an if()

Page 8: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Outline

1 IntroductionRecursive programming

2 Simple examplesExponentiationOrder of executionFactorial

3 The call stackActivation record stackActivation records

4 Types of recursionLinearTailMutualBinary

Page 9: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Outline

1 IntroductionRecursive programming

2 Simple examplesExponentiationOrder of executionFactorial

3 The call stackActivation record stackActivation records

4 Types of recursionLinearTailMutualBinary

Page 10: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Example: Exponentiation

• Recursive case:bn = b ∗ bn−1

• Base case (termination):b0 = 1 (or slightly faster: b1 = b)

• Condition/test, which checks for the base:if(n==0):

For example:b4 = b∗b3 = b∗(b∗b2) = b∗(b∗(b∗b1)) = b∗(b∗(b∗(b∗b0)))Observe code

Page 11: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Efficient exponentiation

To obtain bn, do recursively:

• if n is even, do bn/2 ∗ bn/2

• if n is odd, do b ∗ bn/2 ∗ bn/2

• with base case, b1 = b

Note: n/2 is integer division

What is b62 ?

1 b62 = (b31)2

2 b31 = b(b15)2

3 b15 = b(b7)2

4 b7 = b(b3)2

5 b3 = b(b1)2

6 b1 = b

What is b61 ?

1 b61 = b(b30)2

2 b30 = (b15)2

3 b15 = b(b7)2

4 b7 = b(b3)2

5 b3 = b(b1)2

6 b1 = b

How many multiplications when counting from the bottom up?

Page 12: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Outline

1 IntroductionRecursive programming

2 Simple examplesExponentiationOrder of executionFactorial

3 The call stackActivation record stackActivation records

4 Types of recursionLinearTailMutualBinary

Page 13: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Paying attention to order is important

• When a function calls itself once, instructions placedbefore the recursive call are executed once per recursion

• Instructions placed after the recursive call are executedrepeatedly after the maximum recursion has been reached.

Observe code

Page 14: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Outline

1 IntroductionRecursive programming

2 Simple examplesExponentiationOrder of executionFactorial

3 The call stackActivation record stackActivation records

4 Types of recursionLinearTailMutualBinary

Page 15: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Example: Factorial

• Recursive case:n! = n ∗ ((n − 1)!)

• Base case (termination):0! = 1

• Condition/test, which checks for the base:if(n==0):

Observe code

Page 16: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Example: Factorial

n! = n ∗ ((n − 1)!)0! = 1

Observe code, and evaluate calling factorial of 3:

S ince 3 i s not 0 , take second branch and c a l c u l a t e (n− 1 ) ! . . .S i n c e 2 i s not 0 , take second branch and c a l c u l a t e (n− 1 ) ! . . .

S i n c e 1 i s not 0 , take second branch and c a l c u l a t e (n− 1 ) ! . . .S i n c e 0 equa l s 0 , take f i r s t branch and r e tu rn 1 .

Return va lue , 1 , i s m u l t i p l i e d by n = 1 , and r e tu rn r e s u l t .Return va lue , 1 , i s m u l t i p l i e d by n = 2 , and r e tu rn r e s u l t

Return va lue , 2 , i s m u l t i p l i e d by n = 3 , and the r e s u l t , 6 , becomes ther e t u r n v a l u e o f the f u n c t i o n c a l l t h a t s t a r t e d the whole p r o c e s s .

Page 17: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Factorial

Page 18: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Factorial

Page 19: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Factorial

Page 20: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Outline

1 IntroductionRecursive programming

2 Simple examplesExponentiationOrder of executionFactorial

3 The call stackActivation record stackActivation records

4 Types of recursionLinearTailMutualBinary

Page 21: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Keeping the stack of activation records

• A subroutine call is implemented by placing necessaryinformation about the subroutine (including the returnaddress, parameters, and local variables) onto a stack.This information is called an activation record.

• Further subroutine calls add to the stack. Each returnfrom a subroutine pops the top activation record off thestack.

Page 22: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Outline

1 IntroductionRecursive programming

2 Simple examplesExponentiationOrder of executionFactorial

3 The call stackActivation record stackActivation records

4 Types of recursionLinearTailMutualBinary

Page 23: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Keeping the stack of activation records

Page 24: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Keeping the stack of activation records

Page 25: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Keeping the stack of activation records

Each record on the stack needs a return address (more later)

Page 26: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Outline

1 IntroductionRecursive programming

2 Simple examplesExponentiationOrder of executionFactorial

3 The call stackActivation record stackActivation records

4 Types of recursionLinearTailMutualBinary

Page 27: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Activation records

In practice, a recursive call doesn’t make an entire new copy ofa routine. Each function call gets an activation record where it:

1 Records the location to which it will return

2 Re-enters the new function code at the beginning

3 Allocates memory for the local data for this newinvocation of the function

4 ... until the base case, then:

5 Returns to the earlier function right after where it left off...

Page 28: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Outline

1 IntroductionRecursive programming

2 Simple examplesExponentiationOrder of executionFactorial

3 The call stackActivation record stackActivation records

4 Types of recursionLinearTailMutualBinary

Page 29: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Recursive programming variations: overview

• Single recursion contains a single self-reference, e.g., listtraversal, linear search, or computing the factorialfunction... Single can often be replaced by an iterativecomputation, running in linear time and requiring constantspace.

• Multiple recursion (binary included) contains multipleself-references, e.g., tree traversal, depth-first search(coming up)... This may require exponential time andspace, and is more fundamentally recursive, not being ableto be replaced by iteration without an explicit stack.

• Indirect (or mutual) recursion occurs when a function iscalled not by itself but by another function that it called(either directly or indirectly). Chains of three or morefunctions are possible.

• Generative recursion acts on outputs it generated, whilestructural recursion acts on progressive sets of inputdata.

Page 30: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Outline

1 IntroductionRecursive programming

2 Simple examplesExponentiationOrder of executionFactorial

3 The call stackActivation record stackActivation records

4 Types of recursionLinearTailMutualBinary

Page 31: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Linear recursion

Page 32: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Outline

1 IntroductionRecursive programming

2 Simple examplesExponentiationOrder of executionFactorial

3 The call stackActivation record stackActivation records

4 Types of recursionLinearTailMutualBinary

Page 33: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Tail recursion

Page 34: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Outline

1 IntroductionRecursive programming

2 Simple examplesExponentiationOrder of executionFactorial

3 The call stackActivation record stackActivation records

4 Types of recursionLinearTailMutualBinary

Page 35: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Mutual recursion

Page 36: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Outline

1 IntroductionRecursive programming

2 Simple examplesExponentiationOrder of executionFactorial

3 The call stackActivation record stackActivation records

4 Types of recursionLinearTailMutualBinary

Page 37: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Binary (a kind of multiple) recursion

Page 38: Recursion - MST · Introduction Recursive programming Simple examples Exponentiation Order of execution Factorial The call stack Activation record stack Activation records Types of

Introduction

Recursiveprogramming

Simpleexamples

Exponentiation

Order ofexecution

Factorial

The call stack

Activationrecord stack

Activationrecords

Types ofrecursion

Linear

Tail

Mutual

Binary

Fibonacci series

Recursive case: Fn = Fn−1 + Fn−2

Base case: F0 = 0,F1 = 11,1,2,3,5,8,13,...

Check out the code