More Recursion Tiziana Ligorio 1 Todays Plan Recursion Review 8 - - PowerPoint PPT Presentation

more recursion
SMART_READER_LITE
LIVE PREVIEW

More Recursion Tiziana Ligorio 1 Todays Plan Recursion Review 8 - - PowerPoint PPT Presentation

More Recursion Tiziana Ligorio 1 Todays Plan Recursion Review 8 Qeens Problem Permutations Combinations 2 Types of Recursion Reverse String: - single recursive call - Base case: stop => no return value Dictionary: -


slide-1
SLIDE 1

More Recursion

Tiziana Ligorio

1

slide-2
SLIDE 2

Today’s Plan

Recursion Review 8 Qeens Problem Permutations Combinations

2

slide-3
SLIDE 3

Reverse String: 


  • single recursive call

  • Base case: stop => no return value

Dictionary: 


  • split problem into halves but solve only 1

  • Base case: stop => no return value

Fractal Tree: 


  • split problem into halves and solve both

  • Base case: stop => no return value

Factorial:


  • single recursive call

  • Base case: return a value for computation in each recursive call

Types of Recursion

3

slide-4
SLIDE 4

Why/When use recursion

Usually less efficient than iterative counterparts (we will see example later in the course) Inherent overhead associated with function calls Repeated recursive calls with same parameters Compilers can optimize tail-recursive (recursive call is the last statement in the function) functions to be iterative Sometimes logic of iterative solution can be very complex in comparison to recursive solution

4

slide-5
SLIDE 5

The Eight Queens Problem

5

Place 8 Queens on the board s.t. no queen is on the same row, column or diagonal

slide-6
SLIDE 6

The Eight Queens Problem

6

slide-7
SLIDE 7

The Eight Queens Problem

7

slide-8
SLIDE 8

The Eight Queens Problem

8

slide-9
SLIDE 9

The Eight Queens Problem

9

slide-10
SLIDE 10

The Eight Queens Problem

10

slide-11
SLIDE 11

The Eight Queens Problem

11

Backtracking!

slide-12
SLIDE 12

The Eight Queens Problem

12

Backtracking!

slide-13
SLIDE 13

The Eight Queens Problem

13

slide-14
SLIDE 14

The Eight Queens Problem

How can we express this problem recursively?

14

slide-15
SLIDE 15

The Eight Queens Problem

How can we express this problem recursively?

15

Place queen on column i Recursively solve on columns (i+1) to 8

slide-16
SLIDE 16

The Eight Queens Problem

How do we backtrack?

16

slide-17
SLIDE 17

The Eight Queens Problem

How do we backtrack?

17

Communicate to calling function that there are no

  • ptions left, it should try

something else!

slide-18
SLIDE 18

bool placeQueens(board, column) { if(column > BOARD_SIZE) return true; //Problem is solved! else
 { while(there are safe squares in this column) { //place queen in next safe square; if(placeQueen(board, column+1)) //recursively look forward return true; //queen safely placed } return false; //recursive backtracking
 }
 }

18

The Eight Queens Problem

slide-19
SLIDE 19

The Eight Queens Problem

bool placeQueens(board, column) { if(column > BOARD_SIZE) return true; //Problem is solved! else
 { while(there are safe squares in this column) { //place queen in next safe square; if(placeQueen(board, column+1)) //recursively look forward return true; //queen safely placed } return false; //recursive backtracking
 }
 }

19

slide-20
SLIDE 20

Think Algorithmically

“Experienced Computer Scientists analyze and solve computational problems at a level

  • f abstraction that is beyond that of any particular programming language /

representation / implementation”

Algorithm Design 


  • Identify the problem (input, output, states)

  • Come up with a procedure that will lead to solution

  • Independent of implementation detail

Model your problem/data


  • represent the problem to support your algorithm

Implement solution 


  • Language

  • Data structure

  • Implementation detail

20

Initial phase/step

slide-21
SLIDE 21

Think Algorithmically

  • Takes practice

  • The more you see/do the easier it gets

  • There are some frameworks that can guide you, we have

see only a few, you will continue to learn more throughout your career
 E.g.


  • Can I cast this as a backtracking problem?

  • Can I cast this as a decision-making / decision

tree problem?


  • Do I need to enumerate all solutions?

  • Am I looking for best/optimal solution?

21

slide-22
SLIDE 22

Lecture Activity

Write PSEUDOCODE for a RECURSIVE function that finds a path from origin to destination. 
 Assume cities are visited in alphabetical order.
 bool findPath(map, origin, destination)

22

Origin = P , Destination = Z

22

slide-23
SLIDE 23

Lecture Activity

Write PSEUDOCODE for a RECURSIVE function that finds a path from origin to destination. 
 Assume cities are visited in alphabetical order.
 bool findPath(map, origin, destination)

23

P Origin = P , Destination = Z

23

slide-24
SLIDE 24

Lecture Activity

Write PSEUDOCODE for a RECURSIVE function that finds a path from origin to destination. 
 Assume cities are visited in alphabetical order.
 bool findPath(map, origin, destination)

24

P Origin = P , Destination = Z R

24

slide-25
SLIDE 25

Lecture Activity

Write PSEUDOCODE for a RECURSIVE function that finds a path from origin to destination. 
 Assume cities are visited in alphabetical order.
 bool findPath(map, origin, destination)

25

P Origin = P , Destination = Z R X

25

slide-26
SLIDE 26

Lecture Activity

Write PSEUDOCODE for a RECURSIVE function that finds a path from origin to destination. 
 Assume cities are visited in alphabetical order.
 bool findPath(map, origin, destination)

26

P Origin = P , Destination = Z R X

26

slide-27
SLIDE 27

Lecture Activity

Write PSEUDOCODE for a RECURSIVE function that finds a path from origin to destination. 
 Assume cities are visited in alphabetical order.
 bool findPath(map, origin, destination)

27

P Origin = P , Destination = Z R X

27

slide-28
SLIDE 28

Lecture Activity

Write PSEUDOCODE for a RECURSIVE function that finds a path from origin to destination. 
 Assume cities are visited in alphabetical order.
 bool findPath(map, origin, destination)

28

P Origin = P , Destination = Z R X W

28

slide-29
SLIDE 29

Lecture Activity

Write PSEUDOCODE for a RECURSIVE function that finds a path from origin to destination. 
 Assume cities are visited in alphabetical order.
 bool findPath(map, origin, destination)

29

P Origin = P , Destination = Z R X W S

29

slide-30
SLIDE 30

Lecture Activity

Write PSEUDOCODE for a RECURSIVE function that finds a path from origin to destination
 Assume cities are visited in alphabetical order. 
 bool findPath(map, origin, destination)

30

P Origin = P , Destination = Z R X W S T

30

slide-31
SLIDE 31

Lecture Activity

Write PSEUDOCODE for a RECURSIVE function that finds a path from origin to destination
 Assume cities are visited in alphabetical order. 
 bool findPath(map, origin, destination)

31

P Origin = P , Destination = Z R X W S T

31

slide-32
SLIDE 32

Lecture Activity

Write PSEUDOCODE for a RECURSIVE function that finds a path from origin to destination
 Assume cities are visited in alphabetical order. 
 bool findPath(map, origin, destination)

32

P Origin = P , Destination = Z R X W S T

32

slide-33
SLIDE 33

Lecture Activity

Write PSEUDOCODE for a RECURSIVE function that finds a path from origin to destination
 Assume cities are visited in alphabetical order. 
 bool findPath(map, origin, destination)

33

P Origin = P , Destination = Z R X W S T Y

33

slide-34
SLIDE 34

Lecture Activity

Write PSEUDOCODE for a RECURSIVE function that finds a path from origin to destination
 Assume cities are visited in alphabetical order. 
 bool findPath(map, origin, destination)

34

P Origin = P , Destination = Z R X W S T Y Z

34

slide-35
SLIDE 35

Lecture Activity

Write PSEUDOCODE for a RECURSIVE function that finds a path from origin to destination
 Assume cities are visited in alphabetical order. 
 bool findPath(map, origin, destination)

35

P Origin = P , Destination = Z R X W S T Y Z

35

Don’t get bogged down by what a map is. In design phase you know it’s available and you can look up where you can go next from

slide-36
SLIDE 36

Lecture Activity

bool findPath(map, origin, destination)
 {
 mark origin as visited in map
 if origin == destination
 return true
 else
 for each unvisited city C reachable from origin
 if findPath(map, C, destination)
 return true
 return false //recursive backtracking
 }

P Origin = P , Destination = Z R X W S T Y Z

36

Recursive call

slide-37
SLIDE 37

Find Permutations

37

A B C A B C A B D D D C A C B A C D D B A D B A D C C B B A C B A D D C B C A B C D D A B D A B D C C A C A B C A D D B C B A C B D D A C D A C D B B A D A B D A C C B D B A D B C C A D C A D C B B A

Order Matters! Toy example to make initial

  • bservation
slide-38
SLIDE 38

Find Permutations

38

A B C A B C A B D D D C A C B A C D D B A D B A D C C B B A C B A D D C B C A B C D D A B D A B D C C A C A B C A D D B C B A C B D D A C D A C D B B A D A B D A C C B D B A D B C C A D C A D C B B A

slide-39
SLIDE 39

Find Permutations

39

A B C A B C A B D D D C A C B A C D D B A D B A D C C B B A C B A D D C B C A B C D D A B D A B D C C A C A B C A D D B C B A C B D D A C D A C D B B A D A B D A C C B D B A D B C C A D C A D C B B A

slide-40
SLIDE 40

Find Permutations

40

A B C A B C A B D D D C A C B A C D D B A D B A D C C B B A C B A D D C B C A B C D D A B D A B D C C A C A B C A D D B C B A C B D D A C D A C D B B A D A B D A C C B D B A D B C C A D C A D C B B A

slide-41
SLIDE 41

Find Permutations

41

A B C A B C A B D D D C A C B A C D D B A D B A D C C B B A C B A D D C B C A B C D D A B D A B D C C A C A B C A D D B C B A C B D D A C D A C D B B A D A B D A C C B D B A D B C C A D C A D C B B A

slide-42
SLIDE 42

Find Permutations

42

A B C A B C A B D D D C A C B A C D D B A D B A D C C B B A C B A D D C B C A B C D D A B D A B D C C A C A B C A D D B C B A C B D D A C D A C D B B A D A B D A C C B D B A D B C C A D C A D C B B A

slide-43
SLIDE 43

Find Permutations

43

A B C A B C A B D D D C A C B A C D D B A D B A D C C B B A C B A D D C B C A B C D D A B D A B D C C A C A B C A D D B C B A C B D D A C D A C D B B A D A B D A C C B D B A D B C C A D C A D C B B A

slide-44
SLIDE 44

Find Permutations

44

A B C A B C A B D D D C A C B A C D D B A D B A D C C B B A C B A D D C B C A B C D D A B D A B D C C A C A B C A D D B C B A C B D D A C D A C D B B A D A B D A C C B D B A D B C C A D C A D C B B A

slide-45
SLIDE 45

Find Permutations

45

A B C A B C A B D D D C A C B A C D D B A D B A D C C B B A C B A D D C B C A B C D D A B D A B D C C A C A B C A D D B C B A C B D D A C D A C D B B A D A B D A C C B D B A D B C C A D C A D C B B A

Very similar to 000 100 001 101 010 110 011 111

slide-46
SLIDE 46

Find Permutations

46

A B C A B C A B D D D C A C B A C D D B A D B A D C C B B A C B A D D C B C A B C D D A B D A B D C C A C A B C A D D B C B A C B D D A C D A C D B B A D A B D A C C B D B A D B C C A D C A D C B B A

Lock the first letter
 For each letter you lock
 Permute the rest
 


slide-47
SLIDE 47

Find Permutations

47

A B C A B C A B D D D C A C B A C D D B A D B A D C C B B A C B A D D C B C A B C D D A B D A B D C C A C A B C A D D B C B A C B D D A C D A C D B B A D A B D A C C B D B A D B C C A D C A D C B B A

Lock the first letter
 For each letter you lock Permute the rest
 DECISION
 RECURSION

slide-48
SLIDE 48

Find Permutations

48

A B C B C A C A B C B C A B A

A Decision Tree

A B C B A C C A B

ABC ACB BAC BCA CAB CBA

C B C A B A

slide-49
SLIDE 49

/** Prints permutations of a string @param str the string to be permuted @param first index of the leftmost character in str substring to be permuted @param last index of the rightmost character in str substring to be permuted */ void permuteStr(string & str, int first, int last) { if (first == last) cout << str << endl; //obtained one permutation to print else { for (int i = first; i <= last; i++) { swap(str[first],str[i]);//swap other characters with current first permuteStr(str, first+1, last); swap(str[first],str[i]); //restore first char } } }

49

ABC
 BAC CBA

slide-50
SLIDE 50

/** Prints permutations of a string @param str the string to be permuted @param first index of the leftmost character in str substring to be permuted @param last index of the rightmost character in str substring to be permuted */ void permuteStr(string & str, int first, int last) { if (first == last) cout << str << endl; //obtained one permutation to print else { for (int i = first; i <= last; i++) { swap(str[first],str[i]);//swap other characters with current first permuteStr(str, first+1, last); swap(str[first],str[i]); //restore first char } } }

50

i=1, first=0

ABC


slide-51
SLIDE 51

/** Prints permutations of a string @param str the string to be permuted @param first index of the leftmost character in str substring to be permuted @param last index of the rightmost character in str substring to be permuted */ void permuteStr(string & str, int first, int last) { if (first == last) cout << str << endl; //obtained one permutation to print else { for (int i = first; i <= last; i++) { swap(str[first],str[i]);//swap other characters with current first permuteStr(str, first+1, last); swap(str[first],str[i]); //restore first char } } }

51

BAC

i=1, first=0

slide-52
SLIDE 52

/** Prints permutations of a string @param str the string to be permuted @param first index of the leftmost character in str substring to be permuted @param last index of the rightmost character in str substring to be permuted */ void permuteStr(string & str, int first, int last) { if (first == last) cout << str << endl; //obtained one permutation to print else { for (int i = first; i <= last; i++) { swap(str[first],str[i]);//swap other characters with current first permuteStr(str, first+1, last); swap(str[first],str[i]); //restore first char } } }

52

BAC

i=1, first=0

slide-53
SLIDE 53

/** Prints permutations of a string @param str the string to be permuted @param first index of the leftmost character in str substring to be permuted @param last index of the rightmost character in str substring to be permuted */ void permuteStr(string & str, int first, int last) { if (first == last) cout << str << endl; //obtained one permutation to print else { for (int i = first; i <= last; i++) { swap(str[first],str[i]);//swap other characters with current first permuteStr(str, first+1, last); swap(str[first],str[i]); //restore first char } } }

53

BAC

i=1, first=1

slide-54
SLIDE 54

/** Prints permutations of a string @param str the string to be permuted @param first index of the leftmost character in str substring to be permuted @param last index of the rightmost character in str substring to be permuted */ void permuteStr(string & str, int first, int last) { if (first == last) cout << str << endl; //obtained one permutation to print else { for (int i = first; i <= last; i++) { swap(str[first],str[i]);//swap other characters with current first permuteStr(str, first+1, last); swap(str[first],str[i]); //restore first char } } }

54

BAC

i=1, first=1

slide-55
SLIDE 55

/** Prints permutations of a string @param str the string to be permuted @param first index of the leftmost character in str substring to be permuted @param last index of the rightmost character in str substring to be permuted */ void permuteStr(string & str, int first, int last) { if (first == last) cout << str << endl; //obtained one permutation to print else { for (int i = first; i <= last; i++) { swap(str[first],str[i]);//swap other characters with current first permuteStr(str, first+1, last); swap(str[first],str[i]); //restore first char } } }

55

BAC

i=1, first=1

slide-56
SLIDE 56

/** Prints permutations of a string @param str the string to be permuted @param first index of the leftmost character in str substring to be permuted @param last index of the rightmost character in str substring to be permuted */ void permuteStr(string & str, int first, int last) { if (first == last) cout << str << endl; //obtained one permutation to print else { for (int i = first; i <= last; i++) { swap(str[first],str[i]);//swap other characters with current first permuteStr(str, first+1, last); swap(str[first],str[i]); //restore first char } } }

56

BAC

i=2, first=2

slide-57
SLIDE 57

/** Prints permutations of a string @param str the string to be permuted @param first index of the leftmost character in str substring to be permuted @param last index of the rightmost character in str substring to be permuted */ void permuteStr(string & str, int first, int last) { if (first == last) cout << str << endl; //obtained one permutation to print else { for (int i = first; i <= last; i++) { swap(str[first],str[i]);//swap other characters with current first permuteStr(str, first+1, last); swap(str[first],str[i]); //restore first char } } }

57

BAC

i=1, first=1

slide-58
SLIDE 58

/** Prints permutations of a string @param str the string to be permuted @param first index of the leftmost character in str substring to be permuted @param last index of the rightmost character in str substring to be permuted */ void permuteStr(string & str, int first, int last) { if (first == last) cout << str << endl; //obtained one permutation to print else { for (int i = first; i <= last; i++) { swap(str[first],str[i]);//swap other characters with current first permuteStr(str, first+1, last); swap(str[first],str[i]); //restore first char } } }

58

BCA

i=2, first=1

slide-59
SLIDE 59

/** Prints permutations of a string @param str the string to be permuted @param first index of the leftmost character in str substring to be permuted @param last index of the rightmost character in str substring to be permuted */ void permuteStr(string & str, int first, int last) { if (first == last) cout << str << endl; //obtained one permutation to print else { for (int i = first; i <= last; i++) { swap(str[first],str[i]);//swap other characters with current first permuteStr(str, first+1, last); swap(str[first],str[i]); //restore first char } } }

59

BCA

i=2, first=1

slide-60
SLIDE 60

/** Prints permutations of a string @param str the string to be permuted @param first index of the leftmost character in str substring to be permuted @param last index of the rightmost character in str substring to be permuted */ void permuteStr(string & str, int first, int last) { if (first == last) cout << str << endl; //obtained one permutation to print else { for (int i = first; i <= last; i++) { swap(str[first],str[i]);//swap other characters with current first permuteStr(str, first+1, last); swap(str[first],str[i]); //restore first char } } }

60

BCA

i=2, first=2

slide-61
SLIDE 61

/** Prints permutations of a string @param str the string to be permuted @param first index of the leftmost character in str substring to be permuted @param last index of the rightmost character in str substring to be permuted */ void permuteStr(string & str, int first, int last) { if (first == last) cout << str << endl; //obtained one permutation to print else { for (int i = first; i <= last; i++) { swap(str[first],str[i]);//swap other characters with current first permuteStr(str, first+1, last); swap(str[first],str[i]); //restore first char } } }

61

BAC

i=2, first=1

slide-62
SLIDE 62

/** Prints permutations of a string @param str the string to be permuted @param first index of the leftmost character in str substring to be permuted @param last index of the rightmost character in str substring to be permuted */ void permuteStr(string & str, int first, int last) { if (first == last) cout << str << endl; //obtained one permutation to print else { for (int i = first; i <= last; i++) { swap(str[first],str[i]);//swap other characters with current first permuteStr(str, first+1, last); swap(str[first],str[i]); //restore first char } } }

62

ABC

i=1, first=0

slide-63
SLIDE 63

/** Prints permutations of a string @param str the string to be permuted @param first index of the leftmost character in str substring to be permuted @param last index of the rightmost character in str substring to be permuted */ void permuteStr(string & str, int first, int last) { if (first == last) cout << str << endl; //obtained one permutation to print else { for (int i = first; i <= last; i++) { swap(str[first],str[i]);//swap other characters with current first permuteStr(str, first+1, last); swap(str[first],str[i]); //restore first char } } }

63

ABC

i=2, first=0

slide-64
SLIDE 64

Recursive Decision Tree

void exploreFrom(current_state, decisions_made) { if (all decisions have been made) { //base case

  • utput the result of the decisions we’ve made;

} else { for (each decision we can make) { exploreFrom(result of making that decision, decisions_made + this_decision); } } }

64

Generally, if you can express a problem solution with a decision tree you can translate it into a recursive algorithm

slide-65
SLIDE 65

Find Combinations

65

A B C D A A B A C A D B C B D C D B C D A B C A B D A C D A B C D { } B C D

Order does Not matter!

slide-66
SLIDE 66

Find Combinations of size 2

66

A B C D A A B A C A D B C B D C D B C D A B C A B D A C D A B C D { } B C D

Order does Not matter!

slide-67
SLIDE 67

Combinations (n choose k)

67

One way to choose 5 out of 9 is to Exclude 1 and choose 5 out of 8 Start with toy problem to make observation

slide-68
SLIDE 68

Combinations (n choose k)

68

One way to choose 5 out of 9 is to Exclude 1 and choose 5 out of 8

slide-69
SLIDE 69

Combinations (n choose k)

69

One way to choose 5 out of 9 is to Exclude 1 and choose 5 out of 8

slide-70
SLIDE 70

Combinations (n choose k)

70

One way to choose 5 out of 9 is to Exclude 1 and choose 5 out of 8 Smaller problem! n-1

slide-71
SLIDE 71

Combinations (n choose k)

71

One way to choose 5 out of 9 is to Exclude 1 and choose 5 out of 8

slide-72
SLIDE 72

Combinations (n choose k)

72

One way to choose 5 out of 9 is to Exclude 1 and choose 5 out of 8

slide-73
SLIDE 73

Combinations (n choose k)

73

One way to choose 5 out of 9 is to Exclude 1 and choose 5 out of 8

slide-74
SLIDE 74

Combinations (n choose k)

74

One way to choose 5 out of 9 is to Include 1 and choose 4 out of 8 Need to make another

  • bservation
slide-75
SLIDE 75

Combinations (n choose k)

75

One way to choose 5 out of 9 is to Include 1 and choose 4 out of 8

slide-76
SLIDE 76

Combinations (n choose k)

76

One way to choose 5 out of 9 is to Include 1 and choose 4 out of 8

slide-77
SLIDE 77

Combinations (n choose k)

77

One way to choose 5 out of 9 is to Include 1 and choose 4 out of 8

slide-78
SLIDE 78

Count Combinations

int countCombinations(int n, int k) { if ( (k == 0) || (k == n)) return 1; else return countCombinations(n-1, k-1) + countCombinations(n-1, k); }

78

Recursive algorithm for computing binomial coefficients Include one Exclude

  • ne
slide-79
SLIDE 79

How can you be sure it works???

You come up with an algorithm You implement it You test it How can you be sure it will ALWAYS work???

79

slide-80
SLIDE 80

How can you be sure it works???

You come up with an algorithm You implement it You test it How can you be sure it will ALWAYS work??? PROVE IT!!!

80

slide-81
SLIDE 81

Recursion and Induction

Principle of Mathematical Induction:
 Suppose you want to prove that a statement P(n) about an integer n is true for every positive integer n. 
 To prove that P(n) is true for all n ≥ 1, do the following two steps:

  • Base Step: Prove that P(1) is true.
  • Inductive Step: Let k ≥ 1. Assume P(k) is true, and prove that

P(k + 1) is also true.

81

slide-82
SLIDE 82

//a: nonzero real number, n: nonnegative integer
 power(a, n) 
 {
 if (n = 0) 
 return 1 
 else 
 return a * power(a, n − 1)
 } 
 Prove by mathematical induction on n that the algorithm above is correct. We will show P(n) is true for all n ≥ 0, where 
 P(n): For all nonzero real numbers a, power(a, n) correctly computes an.

82

Recursion and Induction

slide-83
SLIDE 83

Base step: If n = 0, the first step of the algorithm tells us that power(a, 0)=1. This is correct because a0 = 1 for every nonzero real number a, so P(0) is true. Inductive step: 
 Let k ≥ 0. 
 Inductive hypothesis: power(a, k) = ak , for all a != 0. 
 We must show next that power(a, k+1)= ak+1 . 
 Since k + 1 > 0 the algorithm sets 
 power(a, k + 1) = a * power(a, k)
 By inductive hypotheses power(a, k) = ak 
 so power(a, k + 1) = a* power(a, k) = a * ak = ak+1

83

Recursion and Induction

slide-84
SLIDE 84

Exam Drill

Write a recursive function that returns true if the input string is a palindrome (same when reversed)

84

slide-85
SLIDE 85

Exam Drill

Write a recursive function that returns true if the input string is a palindrome (same when reversed)

bool isPalindrome(string s) { if(s.length() == 0 || s.length() == 1) //base case return true; //empty string or string of size 1 are palindrome if(s[0] == s[s.length()-1]) //if first and last char are same //check substring leaving out first and last character return isPalindrome(s.substr(1, s.length()-2)); return false; //not palindrome }

85

slide-86
SLIDE 86

Exam Drill

Write a recursive function for the fibonacci numbers where f(n) = f(n-1) + f(n-2)

86

slide-87
SLIDE 87

Exam Drill

Write a recursive function for the fibonacci numbers where f(n) = f(n-1) + f(n-2)

int fib(int n) { if (n <= 1)//base case return n; return fib(n-1) + fib(n-2); }

87

slide-88
SLIDE 88

Exam Drill

Write a recursive function to find the max value in an array

  • f integers

88

slide-89
SLIDE 89

Exam Drill

Write a recursive function to find the max value in an array

  • f integers

int findMax(int* a, int index) { if (index > 0) return max(a[index], findMax(a, index-1)); else return a[0]; }

89

slide-90
SLIDE 90

Exam Drill

Write a recursive function that finds a particular item in a sorted array (we will look at this algorithm soon)

90