90
More Recursion Tiziana Ligorio 1

13 more recursion - GitHub Pages

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 13 more recursion - GitHub Pages

More Recursion

Tiziana Ligorio!1

Page 2: 13 more recursion - GitHub Pages

Today’s Plan

Recursion Review

8 Qeens Problem

Permutations

Combinations

!2

Page 3: 13 more recursion - GitHub Pages

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

Page 4: 13 more recursion - GitHub Pages

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

Page 5: 13 more recursion - GitHub Pages

The Eight Queens Problem

!5

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

diagonal

Page 6: 13 more recursion - GitHub Pages

The Eight Queens Problem

!6

Page 7: 13 more recursion - GitHub Pages

The Eight Queens Problem

!7

Page 8: 13 more recursion - GitHub Pages

The Eight Queens Problem

!8

Page 9: 13 more recursion - GitHub Pages

The Eight Queens Problem

!9

Page 10: 13 more recursion - GitHub Pages

The Eight Queens Problem

!10

Page 11: 13 more recursion - GitHub Pages

The Eight Queens Problem

!11

Backtracking!

Page 12: 13 more recursion - GitHub Pages

The Eight Queens Problem

!12

Backtracking!

Page 13: 13 more recursion - GitHub Pages

The Eight Queens Problem

!13

Page 14: 13 more recursion - GitHub Pages

The Eight Queens Problem

How can we express this problem recursively?

!14

Page 15: 13 more recursion - GitHub Pages

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

Page 16: 13 more recursion - GitHub Pages

The Eight Queens Problem

How do we backtrack?

!16

Page 17: 13 more recursion - GitHub Pages

The Eight Queens Problem

How do we backtrack?

!17

Communicate to calling function that there are no options left, it should try

something else!

Page 18: 13 more recursion - GitHub Pages

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 forwardreturn true; //queen safely placed

}return false; //recursive backtracking

}}

!18

The Eight Queens Problem

Page 19: 13 more recursion - GitHub Pages

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 forwardreturn true; //queen safely placed

}return false; //recursive backtracking

}}

!19

Page 20: 13 more recursion - GitHub Pages

Think Algorithmically“Experienced Computer Scientists analyze and solve computational problems at a level of 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

Page 21: 13 more recursion - GitHub Pages

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

Page 22: 13 more recursion - GitHub Pages

Lecture ActivityWrite 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

Page 23: 13 more recursion - GitHub Pages

Lecture ActivityWrite 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

POrigin = P , Destination = Z

!23

Page 24: 13 more recursion - GitHub Pages

Lecture ActivityWrite 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

POrigin = P , Destination = Z

R

!24

Page 25: 13 more recursion - GitHub Pages

Lecture ActivityWrite 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

POrigin = P , Destination = Z

R

X

!25

Page 26: 13 more recursion - GitHub Pages

Lecture ActivityWrite 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

POrigin = P , Destination = Z

R

X

!26

Page 27: 13 more recursion - GitHub Pages

Lecture ActivityWrite 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

POrigin = P , Destination = Z

R

X

!27

Page 28: 13 more recursion - GitHub Pages

Lecture ActivityWrite 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

POrigin = P , Destination = Z

R

X

W

!28

Page 29: 13 more recursion - GitHub Pages

Lecture ActivityWrite 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

POrigin = P , Destination = Z

R

X

W

S

!29

Page 30: 13 more recursion - GitHub Pages

Lecture ActivityWrite 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

POrigin = P , Destination = Z

R

X

W

S

T

!30

Page 31: 13 more recursion - GitHub Pages

Lecture ActivityWrite 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

POrigin = P , Destination = Z

R

X

W

S

T

!31

Page 32: 13 more recursion - GitHub Pages

Lecture ActivityWrite 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

POrigin = P , Destination = Z

R

X

W

S

T

!32

Page 33: 13 more recursion - GitHub Pages

Lecture ActivityWrite 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

POrigin = P , Destination = Z

R

X

W

S

T

Y

!33

Page 34: 13 more recursion - GitHub Pages

Lecture ActivityWrite 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

POrigin = P , Destination = Z

R

X

W

S

T

Y

Z

!34

Page 35: 13 more recursion - GitHub Pages

Lecture ActivityWrite 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

POrigin = 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

Page 36: 13 more recursion - GitHub Pages

Lecture Activitybool 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

}

POrigin = P , Destination = Z

R

X

W

S

T

Y

Z

!36

Recursive call

Page 37: 13 more recursion - GitHub Pages

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 observation

Page 38: 13 more recursion - GitHub Pages

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

Page 39: 13 more recursion - GitHub Pages

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

Page 40: 13 more recursion - GitHub Pages

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

Page 41: 13 more recursion - GitHub Pages

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

Page 42: 13 more recursion - GitHub Pages

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

Page 43: 13 more recursion - GitHub Pages

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

Page 44: 13 more recursion - GitHub Pages

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

Page 45: 13 more recursion - GitHub Pages

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

Page 46: 13 more recursion - GitHub Pages

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 letterFor each letter you

lockPermute the rest

Page 47: 13 more recursion - GitHub Pages

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 letterFor each letter you

lock Permute the rest

DECISIONRECURSION

Page 48: 13 more recursion - GitHub Pages

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 CBAC B C A B A

Page 49: 13 more recursion - GitHub Pages

/** 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

ABCBAC CBA

Page 50: 13 more recursion - GitHub Pages

/** 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

Page 51: 13 more recursion - GitHub Pages

/** 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

Page 52: 13 more recursion - GitHub Pages

/** 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

Page 53: 13 more recursion - GitHub Pages

/** 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

Page 54: 13 more recursion - GitHub Pages

/** 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

Page 55: 13 more recursion - GitHub Pages

/** 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

Page 56: 13 more recursion - GitHub Pages

/** 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

Page 57: 13 more recursion - GitHub Pages

/** 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

Page 58: 13 more recursion - GitHub Pages

/** 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

Page 59: 13 more recursion - GitHub Pages

/** 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

Page 60: 13 more recursion - GitHub Pages

/** 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

Page 61: 13 more recursion - GitHub Pages

/** 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

Page 62: 13 more recursion - GitHub Pages

/** 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

Page 63: 13 more recursion - GitHub Pages

/** 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

Page 64: 13 more recursion - GitHub Pages

Recursive Decision Tree

void exploreFrom(current_state, decisions_made) { if (all decisions have been made) { //base case output 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

Page 65: 13 more recursion - GitHub Pages

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!

Page 66: 13 more recursion - GitHub Pages

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!

Page 67: 13 more recursion - GitHub Pages

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

Page 68: 13 more recursion - GitHub Pages

Combinations (n choose k)

!68

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

Page 69: 13 more recursion - GitHub Pages

Combinations (n choose k)

!69

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

Page 70: 13 more recursion - GitHub Pages

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

Page 71: 13 more recursion - GitHub Pages

Combinations (n choose k)

!71

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

Page 72: 13 more recursion - GitHub Pages

Combinations (n choose k)

!72

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

Page 73: 13 more recursion - GitHub Pages

Combinations (n choose k)

!73

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

Page 74: 13 more recursion - GitHub Pages

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 observation

Page 75: 13 more recursion - GitHub Pages

Combinations (n choose k)

!75

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

Page 76: 13 more recursion - GitHub Pages

Combinations (n choose k)

!76

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

Page 77: 13 more recursion - GitHub Pages

Combinations (n choose k)

!77

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

Page 78: 13 more recursion - GitHub Pages

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 one

Page 79: 13 more recursion - GitHub Pages

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

Page 80: 13 more recursion - GitHub Pages

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

Page 81: 13 more recursion - GitHub Pages

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

Page 82: 13 more recursion - GitHub Pages

//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

Page 83: 13 more recursion - GitHub Pages

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

Page 84: 13 more recursion - GitHub Pages

Exam Drill

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

!84

Page 85: 13 more recursion - GitHub Pages

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

Page 86: 13 more recursion - GitHub Pages

Exam Drill

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

!86

Page 87: 13 more recursion - GitHub Pages

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

Page 88: 13 more recursion - GitHub Pages

Exam Drill

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

!88

Page 89: 13 more recursion - GitHub Pages

Exam Drill

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

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

!89

Page 90: 13 more recursion - GitHub Pages

Exam Drill

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

!90