Recursion Tiziana Ligorio 1 Todays Plan Announcements Recursion - - PowerPoint PPT Presentation

recursion
SMART_READER_LITE
LIVE PREVIEW

Recursion Tiziana Ligorio 1 Todays Plan Announcements Recursion - - PowerPoint PPT Presentation

Recursion Tiziana Ligorio 1 Todays Plan Announcements Recursion 2 What do these images have in common 3 They contain a SMALLER copy of THEMSELVES 4 Print String Backwards Hello 5 Print String Backwards


slide-1
SLIDE 1

Recursion

Tiziana Ligorio

1

slide-2
SLIDE 2

Today’s Plan

Announcements Recursion

2

slide-3
SLIDE 3

3

What do these images have in common

slide-4
SLIDE 4

4

They contain a SMALLER copy of THEMSELVES

slide-5
SLIDE 5

Print String Backwards

“Hello”

5

slide-6
SLIDE 6

Print String Backwards

“Hello”

Procedure: If there are characters to print
 Print the last character and reverse the rest

6

Recursive Call Notice it’s the last thing it does

slide-7
SLIDE 7

Print String Backwards

Hello


  • Hello

Program Stack Active functions

slide-8
SLIDE 8

Print String Backwards

Hello


Hell


Hell Hello

Program Stack Active functions

slide-9
SLIDE 9

Hello


Hell


  • l


 


Print String Backwards

9

Hell Hello

9

Program Stack Active functions

slide-10
SLIDE 10

Print String Backwards

Hello


Hell


  • l

Hel
 


10

Hell Hello Hel

10

Program Stack Active functions

slide-11
SLIDE 11

Print String Backwards

Hello


Hell


  • l

Hel


  • l l

11

Hell Hello Hel

11

Program Stack Active functions

slide-12
SLIDE 12

Print String Backwards

Hello


Hell


  • l

Hel


  • l l

He 


12

Hell Hello Hel He

12

Program Stack Active functions

slide-13
SLIDE 13

Print String Backwards

Hello


Hell


  • l

Hel


  • l l

He 


  • l l e


13

Hell Hello Hel He

13

Program Stack Active functions

slide-14
SLIDE 14

Print String Backwards

Hello


Hell


  • l

Hel


  • l l

He 


  • l l e


H


14

Hell Hello Hel He H

14

Program Stack Active functions

slide-15
SLIDE 15

Print String Backwards

Hello


Hell


  • l

Hel


  • l l

He 


  • l l e


H


  • l l e H

15

Hell Hello Hel He H

15

Program Stack Active functions

slide-16
SLIDE 16

Print String Backwards

Hello


Hell


  • l

Hel


  • l l

He 


  • l l e


H


  • l l e H

16

BASE CASE

16

Hell Hello Hel He H

Program Stack Active functions

slide-17
SLIDE 17

Print String Backwards

Hello


Hell


  • l

Hel


  • l l

He 


  • l l e


H


  • l l e H

Hell Hello Hel He H

Program Stack Active functions

slide-18
SLIDE 18

Print String Backwards

Hello


Hell


  • l

Hel


  • l l

He 


  • l l e


H


  • l l e H

Hell Hello Hel He

Program Stack Active functions

slide-19
SLIDE 19

Print String Backwards

Hello


Hell


  • l

Hel


  • l l

He 


  • l l e


H


  • l l e H

Hell Hello Hel He

Program Stack Active functions

slide-20
SLIDE 20

Print String Backwards

Hello


Hell


  • l

Hel


  • l l

He 


  • l l e


H


  • l l e H

Hell Hello Hel

Program Stack Active functions

slide-21
SLIDE 21

Print String Backwards

Hello


Hell


  • l

Hel


  • l l

He 


  • l l e


H


  • l l e H

Hell Hello Hel

Program Stack Active functions

slide-22
SLIDE 22

Print String Backwards

Hello


Hell


  • l

Hel


  • l l

He 


  • l l e


H


  • l l e H

Hell Hello

Program Stack Active functions

slide-23
SLIDE 23

Print String Backwards

Hello


Hell


  • l

Hel


  • l l

He 


  • l l e


H


  • l l e H

Hell Hello

Program Stack Active functions

slide-24
SLIDE 24

Print String Backwards

Hello


Hell


  • l

Hel


  • l l

He 


  • l l e


H


  • l l e H

Hello

Program Stack Active functions

slide-25
SLIDE 25

Print String Backwards

Hello


Hell


  • l

Hel


  • l l

He 


  • l l e


H


  • l l e H

Hello

Program Stack Active functions

slide-26
SLIDE 26

Print String Backwards

Hello


Hell


  • l

Hel


  • l l

He 


  • l l e


H


  • l l e H

Program Stack

slide-27
SLIDE 27

Lecture Activity

If I hand you a printed dictionary (an actual book) and ask you to find the word “Kalimba”, what do you do? Write down precise steps (a procedure) as if someone who doesn’t know what a dictionary is must follow your instructions.

27

slide-28
SLIDE 28

28

Look in ?

slide-29
SLIDE 29

LOOK FOR WORD “Kalimba” IN DICTIONARY

  • Open dictionary at random page

_ If “Kalimba” is on page FOUND!!!

  • Else if “Kalimba” is lexicographically < first word on

page
 LOOK FOR WORD “Kalimba” IN LOWER HALF

  • Else if “Kalimba” is lexicographically > last word on page


LOOK FOR WORD “Kalimba” IN UPPER HALF

29

Recursive Call Recursive Call

slide-30
SLIDE 30

How is this different from recursive solution to print backwards?

30

slide-31
SLIDE 31

How is this different from recursive solution to print backwards?

  • Two recursive calls
  • Execute either one or the other
  • Cuts problem in 1/2

31

slide-32
SLIDE 32

The images in the next slides were adapted from Keith Schwarz at Stanford University

32

slide-33
SLIDE 33

33

slide-34
SLIDE 34

34

slide-35
SLIDE 35

35

slide-36
SLIDE 36

36

slide-37
SLIDE 37

37

What di$erentiates the smaller tree from the bigger one?

  • 1. It’s at a di$erent position.
  • 2. It’s at a di$erent orientation.
  • 3. It has a di$erent size.
  • 4. It has a di$erent order.

What di$erentiates the smaller tree from the bigger one?

  • 1. It’s at a di$erent position.
  • 2. It’s at a di$erent orientation.
  • 3. It has a di$erent size.
  • 4. It has a di$erent order.
slide-38
SLIDE 38

38

What di$erentiates the smaller tree from the bigger one?

  • 1. It’s at a di$erent position.
  • 2. It’s at a di$erent orientation.
  • 3. It has a di$erent size.
  • 4. It has a di$erent order.

What di$erentiates the smaller tree from the bigger one?

  • 1. It’s at a di$erent position.
  • 2. It’s at a di$erent orientation.
  • 3. It has a di$erent size.
  • 4. It has a di$erent order.
slide-39
SLIDE 39

39

What di$erentiates the smaller tree from the bigger one?

  • 1. It’s at a di$erent position.
  • 2. It’s at a di$erent size.
  • 3. It has a di$erent size.
  • 4. It has a di$erent order.

What di$erentiates the smaller tree from the bigger one?

  • 1. It’s at a di$erent position.
  • 2. It’s at a di$erent size.
  • 3. It has a di$erent size.
  • 4. It has a di$erent order.
slide-40
SLIDE 40

40

What di$erentiates the smaller tree from the bigger one?

  • 1. It’s at a di$erent position.
  • 2. It’s at a di$erent size.
  • 3. It has a di$erent orientation.
  • 4. It has a di$erent order.

What di$erentiates the smaller tree from the bigger one?

  • 1. It’s at a di$erent position.
  • 2. It’s at a di$erent size.
  • 3. It has a di$erent orientation.
  • 4. It has a di$erent order.
slide-41
SLIDE 41

41

What di$erentiates the smaller tree from the bigger one?

  • 1. It’s at a di$erent position.
  • 2. It’s at a di$erent size.
  • 3. It has a di$erent orientation.
  • 4. It has a di$erent order.

What di$erentiates the smaller tree from the bigger one?

  • 1. It’s at a di$erent position.
  • 2. It’s at a di$erent size.
  • 3. It has a di$erent orientation.
  • 4. It has a di$erent order.
slide-42
SLIDE 42

42

What di$erentiates the smaller tree from the bigger one?

  • 1. It’s at a di$erent position.
  • 2. It’s at a di$erent size.
  • 3. It has a di$erent orientation.
  • 4. It has a di$erent order.

What di$erentiates the smaller tree from the bigger one?

  • 1. It’s at a di$erent position.
  • 2. It’s at a di$erent size.
  • 3. It has a di$erent orientation.
  • 4. It has a di$erent order.

Recursive fractals are often described in terms of some parameter called the order of the fractal. Recursive fractals are often described in terms of some parameter called the order of the fractal.

slide-43
SLIDE 43

43

What di$erentiates the smaller tree from the bigger one?

  • 1. It’s at a di$erent position.
  • 2. It’s at a di$erent size.
  • 3. It has a di$erent orientation.
  • 4. It has a di$erent order.

What di$erentiates the smaller tree from the bigger one?

  • 1. It’s at a di$erent position.
  • 2. It’s at a di$erent size.
  • 3. It has a di$erent orientation.
  • 4. It has a di$erent order.

Recursive fractals are often described in terms of some parameter called the order of the fractal. Recursive fractals are often described in terms of some parameter called the order of the fractal.

An order-0 tree.

slide-44
SLIDE 44

44

What di$erentiates the smaller tree from the bigger one?

  • 1. It’s at a di$erent position.
  • 2. It’s at a di$erent size.
  • 3. It has a di$erent orientation.
  • 4. It has a di$erent order.

What di$erentiates the smaller tree from the bigger one?

  • 1. It’s at a di$erent position.
  • 2. It’s at a di$erent size.
  • 3. It has a di$erent orientation.
  • 4. It has a di$erent order.

Recursive fractals are often described in terms of some parameter called the order of the fractal. Recursive fractals are often described in terms of some parameter called the order of the fractal.

An order-1 tree.

slide-45
SLIDE 45

45

What di$erentiates the smaller tree from the bigger one?

  • 1. It’s at a di$erent position.
  • 2. It’s at a di$erent size.
  • 3. It has a di$erent orientation.
  • 4. It has a di$erent order.

What di$erentiates the smaller tree from the bigger one?

  • 1. It’s at a di$erent position.
  • 2. It’s at a di$erent size.
  • 3. It has a di$erent orientation.
  • 4. It has a di$erent order.

Recursive fractals are often described in terms of some parameter called the order of the fractal. Recursive fractals are often described in terms of some parameter called the order of the fractal.

An order-2 tree.

slide-46
SLIDE 46

46

What di$erentiates the smaller tree from the bigger one?

  • 1. It’s at a di$erent position.
  • 2. It’s at a di$erent size.
  • 3. It has a di$erent orientation.
  • 4. It has a di$erent order.

What di$erentiates the smaller tree from the bigger one?

  • 1. It’s at a di$erent position.
  • 2. It’s at a di$erent size.
  • 3. It has a di$erent orientation.
  • 4. It has a di$erent order.

Recursive fractals are often described in terms of some parameter called the order of the fractal. Recursive fractals are often described in terms of some parameter called the order of the fractal.

An order-3 tree.

slide-47
SLIDE 47

47

What di$erentiates the smaller tree from the bigger one?

  • 1. It’s at a di$erent position.
  • 2. It’s at a di$erent size.
  • 3. It has a di$erent orientation.
  • 4. It has a di$erent order.

What di$erentiates the smaller tree from the bigger one?

  • 1. It’s at a di$erent position.
  • 2. It’s at a di$erent size.
  • 3. It has a di$erent orientation.
  • 4. It has a di$erent order.

Recursive fractals are often described in terms of some parameter called the order of the fractal. Recursive fractals are often described in terms of some parameter called the order of the fractal.

An order-4 tree.

slide-48
SLIDE 48

48

What di$erentiates the smaller tree from the bigger one?

  • 1. It’s at a di$erent position.
  • 2. It’s at a di$erent size.
  • 3. It has a di$erent orientation.
  • 4. It has a di$erent order.

What di$erentiates the smaller tree from the bigger one?

  • 1. It’s at a di$erent position.
  • 2. It’s at a di$erent size.
  • 3. It has a di$erent orientation.
  • 4. It has a di$erent order.

Recursive fractals are often described in terms of some parameter called the order of the fractal. Recursive fractals are often described in terms of some parameter called the order of the fractal.

An order-11 tree.

slide-49
SLIDE 49

Lecture Activity

Give a sequence of precise instructions in English (algorithm) to DRAW an order-3 fractal tree

49

slide-50
SLIDE 50

Lecture Activity

Give a sequence of precise instructions in English (algorithm) to DRAW an order-3 fractal tree Hint:

50

Draw a line and then

slide-51
SLIDE 51

51

What di$erentiates the smaller tree from the bigger one?

  • 1. It’s at a di$erent position.
  • 2. It’s at a di$erent size.
  • 3. It has a di$erent orientation.
  • 4. It has a di$erent order.

What di$erentiates the smaller tree from the bigger one?

  • 1. It’s at a di$erent position.
  • 2. It’s at a di$erent size.
  • 3. It has a di$erent orientation.
  • 4. It has a di$erent order.

Recursive fractals are often described in terms of some parameter called the order of the fractal. Recursive fractals are often described in terms of some parameter called the order of the fractal.

An order-3 tree.

An order-0 tree is nothing at all. An order-n tree is a line with two smaller order-(n-1) trees starting at the end of that line. An order-0 tree is nothing at all. An order-n tree is a line with two smaller order-(n-1) trees starting at the end of that line.

slide-52
SLIDE 52

Lecture Activity

  • draw a line
  • tilt the canvas 45˚ left and draw an order-2 tree
  • tilt the canvas 45˚ right and draw an order-2 tree

52

Recursive Call Recursive Call

slide-53
SLIDE 53

Lecture Activity

  • draw a line
  • tilt the canvas 45˚ left and draw an order-2 tree
  • tilt the canvas 45˚ right and draw an order-2 tree

53

slide-54
SLIDE 54

Lecture Activity

  • draw a line

  • tilt the canvas 45˚ left and draw an order-2 tree

  • draw a line

  • tilt the canvas 45˚ left and draw an order-1 tree

  • tilt the canvas 45˚ right and draw an order-1 tree
  • tilt the canvas 45˚ right and draw an order-2 tree

  • draw a line

  • tilt the canvas 45˚ left and draw an order-1 tree

  • tilt the canvas 45˚ right and draw an order-1 tree

54

slide-55
SLIDE 55

Lecture Activity

  • draw a line

  • tilt the canvas 45˚ left and draw an order-2 tree

  • draw a line

  • tilt the canvas 45˚ left and draw an order-1 tree

  • tilt the canvas 45˚ right and draw an order-1 tree
  • tilt the canvas 45˚ right and draw an order-2 tree

  • draw a line

  • tilt the canvas 45˚ left and draw an order-1 tree

  • tilt the canvas 45˚ right and draw an order-1 tree

55

slide-56
SLIDE 56
  • draw a line

  • tilt the canvas 45˚ left and draw an order-2 tree

  • draw a line

  • tilt the canvas 45˚ left and draw an order-1 tree

  • draw a line

  • tilt the canvas 45˚ left and draw an order-0 tree

  • tilt the canvas 45˚ right and draw an order-0 tree

  • tilt the canvas 45˚ right and draw an order-1 tree

  • draw a line

  • tilt the canvas 45˚ left and draw an order-0 tree

  • tilt the canvas 45˚ right and draw an order-0 tree

  • tilt the canvas 45˚ right and draw an order-2 tree

  • draw a line

  • tilt the canvas 45˚ left and draw an order-1 tree

  • draw a line

  • tilt the canvas 45˚ left and draw an order-0 tree

  • tilt the canvas 45˚ right and draw an order-0 tree

  • tilt the canvas 45˚ right and draw an order-1 tree

  • draw a line

  • tilt the canvas 45˚ left and draw an order-0 tree

  • tilt the canvas 45˚ right and draw an order-0 tree

Lecture Activity

56

slide-57
SLIDE 57
  • draw a line

  • tilt the canvas 45˚ left and draw an order-2 tree

  • draw a line

  • tilt the canvas 45˚ left and draw an order-1 tree

  • draw a line

  • tilt the canvas 45˚ left and draw an order-0 tree

  • tilt the canvas 45˚ right and draw an order-0 tree

  • tilt the canvas 45˚ right and draw an order-1 tree

  • draw a line

  • tilt the canvas 45˚ left and draw an order-0 tree

  • tilt the canvas 45˚ right and draw an order-0 tree

  • tilt the canvas 45˚ right and draw an order-2 tree

  • draw a line

  • tilt the canvas 45˚ left and draw an order-1 tree

  • draw a line

  • tilt the canvas 45˚ left and draw an order-0 tree

  • tilt the canvas 45˚ right and draw an order-0 tree

  • tilt the canvas 45˚ right and draw an order-1 tree

  • draw a line

  • tilt the canvas 45˚ left and draw an order-0 tree

  • tilt the canvas 45˚ right and draw an order-0 tree

Nothing to draw at order 0 We stop! BASE CASE

Lecture Activity

57

slide-58
SLIDE 58

In general for n

  • draw a line
  • tilt the canvas 45˚ left and draw and order-(n-1) tree
  • tilt the canvas 45˚ right and draw and order-(n-1) tree

58

slide-59
SLIDE 59

Check This Out!!!

http://recursivedrawing.com/

59

slide-60
SLIDE 60

Different Flavors of Recursion

Reverse String: write first character, reverse the remaining single smaller string Dictionary: either inspect upper-half or lower-half Fractal Tree: draw both the left order-(n-1) and right

  • rder-(n-1) trees

All solve a problem by breaking it up into one or more smaller “similar” problems

60

slide-61
SLIDE 61

Recursive Problem-Solving

if (problem is sufficiently simple) {
 
 directly solve the problem 
 i.e. do something and/or return the solution 
 
 } else {
 
 split problem up into one or more smaller problems with the same structure as the original solve some or all of those smaller problems do something or combine results to return solution if necessary
 }

61

slide-62
SLIDE 62

Recursive Problem-Solving

if(problem is sufficiently simple){
 
 directly solve the problem 
 i.e. do something and/or return the solution 
 
 } else{
 
 split problem up into one or more smaller problems with the same structure as the original solve some or all of those smaller problems do something or combine results to return solution if necessary
 }

62

BASE CASE

slide-63
SLIDE 63

Why Recursion

An alternative to iteration Not always practical (some compilers optimize tail- recursive algorithms) Elegant and intuitive solution for some problems

63

slide-64
SLIDE 64

Factorial


 1 x 2 x 3 x … x n For example:
 0!=1,1!=1, 2!=2, 3!=6, 4!=24, 5!=120


64

n!= k

k=1 n

The empty product

slide-65
SLIDE 65

But what if we start from n?

n!=

65

slide-66
SLIDE 66

But what if we start from n?

n! = n x (n-1) x (n-2) x (n - 3) x … …. …. … 2 x 1

66

What is this?

slide-67
SLIDE 67

But what if we start from n?

n! = n x (n-1) x (n-2) x (n - 3) x … …. …. … 2 x 1

67

(n-1)!

slide-68
SLIDE 68

But what if we start from n?

n! = n x (n-1) x (n-2) x (n - 3) x … …. …. … 2 x 1 (n-1)! = (n-1) x (n-2) x (n - 3) x … …. …. … 2 x 1

68

(n-1)! What is this?

slide-69
SLIDE 69

But what if we start from n?

n! = n x (n-1) x (n-2) x (n - 3) x … …. …. … 2 x 1 (n-1)! = (n-1) x (n-2) x (n - 3) x … …. …. … 2 x 1

69

(n-1)! (n-2)!

slide-70
SLIDE 70

Recursion that Returns a Value

n! = n x (n-1)!

70

Same function being called within solution

slide-71
SLIDE 71

Recursion that Returns a Value

n! = n x (n-1)!

/** Computes the factorial of the nonnegative integer n.


@pre: n must be greater than or equal to 0.
 @post: None.
 @return: The factorial of n; n is unchanged. */
 int factorial(int n)
 {
 if (n == 0)
 return 1;
 else // n > 0 : n-1 >= 0, fact(n-1) returns (n-1)!
 return n * factorial(n - 1); // n * (n-1)! is n!
 } // end fact

71

slide-72
SLIDE 72

n! = n x (n-1)!

/** Computes the factorial of the nonnegative integer n.


@pre: n must be greater than or equal to 0.
 @post: None.
 @return: The factorial of n; n is unchanged. */
 int factorial(int n)
 {
 if (n == 0)
 return 1;
 else // n > 0 : n-1 >= 0, fact(n-1) returns (n-1)!
 return n * factorial(n - 1); // n * (n-1)! is n!
 } // end fact

Recursion that Returns a Value

72

BASE CASE

slide-73
SLIDE 73

n! = n x (n-1)!

/** Computes the factorial of the nonnegative integer n.


@pre: n must be greater than or equal to 0.
 @post: None.
 @return: The factorial of n; n is unchanged. */
 int factorial(int n)
 {
 if (n == 0)
 return 1;
 else // n > 0 : n-1 >= 0, fact(n-1) returns (n-1)!
 return n * factorial(n - 1); // n * (n-1)! is n!
 } // end fact

Recursion that Returns a Value

73

slide-74
SLIDE 74

n! = n x (n-1)!

/** Computes the factorial of the nonnegative integer n.


@pre: n must be greater than or equal to 0.
 @post: None.
 @return: The factorial of n; n is unchanged. */
 int factorial(int n)
 {
 if (n == 0)
 return 1;
 else // n > 0 : n-1 >= 0, fact(n-1) returns (n-1)!
 return n * factorial(n - 1); // n * (n-1)! is n!
 } // end fact

Recursion that Returns a Value

74

BASE CASE WILL LEAD TO BASE CASE

slide-75
SLIDE 75

75

slide-76
SLIDE 76

Writing a String Backwards

writeBackward(string s)
 {
 if(the string is empty)
 Do nothing - this is the base case
 else
 Write the last character of s
 writeBackward(s minus the last char)
 }

76

slide-77
SLIDE 77

Recursion that Performs an Action

/** Prints a string backward.
 @post: The string s is printed backwards
 @param: s The string to write backwards */ void writeBackward(string s) {
 size_t length = s.size(); // Length of string
 if (length > 0)//implicit base case: if length == 0 do nothing
 {
 // Print the last character
 cout << s.substr(length - 1, 1); 
 // Print the rest of the string backwards - recursive call
 writeBackward(s.substr(0, length - 1));
 } // end if // length == 0 is the base case - do nothing
 } // end writeBackward

77

slide-78
SLIDE 78

/** Prints a string backward.
 @post: The string s is printed backwards
 @param: s The string to write backwards */ void writeBackward(string s) {
 size_t length = s.size(); // Length of string
 if (length > 0)//implicit base case: if length == 0 do nothing
 {
 // Print the last character
 cout << s.substr(length - 1, 1); 
 // Print the rest of the string backwards - recursive call
 writeBackward(s.substr(0, length - 1));
 } // end if // length == 0 is the base case - do nothing
 } // end writeBackward

78

WILL LEAD TO BASE CASE

Recursion that Performs an Action

slide-79
SLIDE 79

Write String Backwards

Hello


Hell


  • l

Hel


  • l l

He 


  • l l e


H


  • l l e H

79

BASE CASE