Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that.

Similar presentations


Presentation on theme: "Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that."— Presentation transcript:

1 Recursion

2 What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that calls itself! Another way to repeat code Takes advantage of the “activation stack”

3

4 You just moved in to a new house on a dead end street… In celebration you shout: “Woo hoo, my new house is awesome and the house number is 1282!”

5 You just moved in to a new house on a dead end street… But you don’t know the house number! You do know that your house number is 2 plus the address of your neighbor to the left So you decide to call your neighbor…

6 Call your neighbor and ask them for their house number. Add two to get your house number. You

7 Call your neighbor and ask them for their house number. Howdy neighbor. What is your house number? Neighbor 1 You

8 Wait for the neighbor to give you an answer. 42. Neighbor 1 You

9 Kindly thank them and hang up. Sweet, thanks! You Neighbor 1

10 Add two to your neighbor’s address to get your house number. So my address is 42 + 2 = 44!

11 What if your neighbor doesn’t know their address?

12 Call your neighbor and ask for their house number. Howdy neighbor. What is your house number? Neighbor 1 You

13 But they don’t know it! Hm, that’s a good question. Hold please! Neighbor 1 You

14 Neighbor 1 puts you on hold and calls their neighbor. Neighbor 1 Neighbor 2You

15 Neighbor 1 puts you on hold and calls their neighbor. Howdy neighbor. What is your house number? Neighbor 1 Neighbor 2You

16 But their neighbor doesn’t know their house number either! (Seriously, who are these people?!) Hm, that’s a good question. Hold please! Neighbor 1 Neighbor 2You

17 Neighbor 2 puts neighbor 1 on hold and calls neighbor 3. Howdy neighbor. What is your house number? Neighbor 1Neighbor 2YouNeighbor 3

18 Neighbor 3 tells neighbor 2 their house number (phew!) 22. Neighbor 1Neighbor 2YouNeighbor 3

19 Neighbor 2 thanks neighbor 3 and hangs up. Sweet, thanks! Neighbor 1Neighbor 2YouNeighbor 3

20 Neighbor 2 adds two to get address. So my address is 22 + 2 = 24! Neighbor 1Neighbor 2You

21 Neighbor 2 takes neighbor 3 off of hold and tells them their address. 24. Neighbor 1Neighbor 2You

22 Neighbor 1 thanks neighbor 2 and hangs up. Sweet, thanks! Neighbor 1Neighbor 2You

23 Your neighbor adds two to get their address. So my address is 24 + 2 = 26! Neighbor 1

24 Finally your neighbor takes you off hold and tells you their address (what took them so long?!) Sweet, thanks! Neighbor 1

25 You add two to your neighbors address to get your house number! So my address is 26 + 2 = 28!

26 Let’s analyze what just happened How many phone calls were made? When did the calling chain finally stop? From “your” perspective, what happened when you called your neighbor?

27 Algorithm in writing If you already know your house number, say it loud! If you don’t know your house number… – Call neighbor to your left and ask for their house number – When you get the number, add two to it, then say it loud!

28 But… What if you don’t have any neighbors?

29 Algorithm in writing If you already know your house number, say it loud If you don’t have a neighbor to the left and don’t know your house number, say 0 If you don’t know your house number… – Call neighbor to your left and ask for their house number – When you get the number, add two to it, then say it loud!

30 - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! 22You

31 - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! 22 You

32 - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! 22 You N1

33 - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! 22 You N1

34 - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! 22 You N1 N2

35 - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! 22 You N1 N2

36 - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! 22 You N1 N2 N3

37 - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! 22 You N1 N2 N3

38 - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! 22 You N1 N2 N3 22

39 - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! 22 You N1 N2 22 + 2 = 24

40 - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! 22 You N1 N2 22 + 2 = 24 24

41 - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! 22 You N1 24 + 2 = 26

42 - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! 22 You N1 24 + 2 = 26 26

43 - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! 22 You 26 + 2 = 28

44 22 You

45 But… What if no-one on your street knows their house number?

46 But… What if no-one on your street knows their house number? – Think about it….don’t we cover this case by adding a check to see if you have a neighbor to the left and returning 0 if you don’t?

47 Three “Pillars” of Recursion A recursive function… – Calls clone of itself – Has terminating condition(s) – Moves toward terminating condition(s)

48 House number problem components Calling clone of itself? Terminating condition(s)? Moving toward the terminating conditions? If you already know your house number, say it loud If you don’t have a neighbor to the left and don’t know your house number, say 0 If you don’t know your house number… – Call neighbor to your left and ask for their house number – When you get the number, add two to it, then say it loud!

49 House number problem components Calling clone of itself? Terminating condition(s)? Moving toward the terminating conditions? If you already know your house number, say it loud If you don’t have a neighbor to the left and don’t know your house number, say 0 If you don’t know your house number… – Call neighbor to your left and ask for their house number – When you get the number, add two to it, then say it loud! Terminating conditions Call clone and move toward term cond

50 Another problem: Factorial How do you calculate 5! 5*4*3*2*1

51 Another problem: Factorial How do you calculate 5! 5! = 5*4! 4! = 4*3! 3! = 3*2! 2! = 2*1! 1! = 1 (what about 0!)

52 In general… n! = n * (n-1)! If we had a factorial function instead of !, it would be fact(n) = n * fact(n-1)

53 Factorial Recursive Algorithm What are are terminating (aka base) conditions? What is the simplest factorial to calculate? What inputs does the function need? How do we move toward the terminating condition?

54 Factorial Recursive Algorithm If number is 0 or 1 – Answer is 1 Else – Answer is number * factorial of number-1

55 Call fact(3) from command window… >> myans = fact(3)

56 fact(3) if 3 == 1 | 3==0 ans = 1 else ans = 3 * fact(3-1) >> myans = fact(3)

57 fact(3) if 3 == 1 | 3==0 ans = 1 else ans = 3 * fact(3-1) Cal fact(3) from command window… >> myans = fact(3)

58 fact(3) if 3 == 1 | 3==0 ans = 1 else ans = 3 * fact(3-1) Cal fact(3) from command window… >> myans = fact(3) fact(2) if 2 == 1 | 2==0 ans = 1 else ans = 2 * fact(2-1)

59 fact(3) if 3 == 1 | 3==0 ans = 1 else ans = 3 * fact(3-1) Cal fact(3) from command window… >> myans = fact(3) fact(2) if 2 == 1 | 2==0 ans = 1 else ans = 2 * fact(2-1)

60 fact(3) if 3 == 1 | 3==0 ans = 1 else ans = 3 * fact(3-1) Cal fact(3) from command window… >> myans = fact(3) fact(2) if 2 == 1 | 2==0 ans = 1 else ans = 2 * fact(2-1) fact(1) if 1 == 1 | 1==0 ans = 1 else ans = 1 * fact(1-1)

61 fact(3) if 3 == 1 | 3==0 ans = 1 else ans = 3 * fact(3-1) Cal fact(3) from command window… >> myans = fact(3) fact(2) if 2 == 1 | 2==0 ans = 1 else ans = 2 * fact(2-1) fact(1) if 1 == 1 | 1==0 ans = 1 else ans = 1 * fact(1-1) 1

62 fact(3) if 3 == 1 | 3==0 ans = 1 else ans = 3 * fact(3-1) Cal fact(3) from command window… >> myans = fact(3) fact(2) if 2 == 1 | 2==0 ans = 1 else ans = 2 * fact(2-1) 1

63 fact(3) if 3 == 1 | 3==0 ans = 1 else ans = 3 * fact(3-1) Cal fact(3) from command window… >> myans = fact(3) fact(2) if 2 == 1 | 2==0 ans = 1 else ans = 2 * fact(2-1) 1 2

64 fact(3) if 3 == 1 | 3==0 ans = 1 else ans = 3 * fact(3-1) Cal fact(3) from command window… >> myans = fact(3) 2

65 fact(3) if 3 == 1 | 3==0 ans = 1 else ans = 3 * fact(3-1) Cal fact(3) from command window… >> myans = fact(3) 2 6

66 Cal fact(3) from command window… >> myans = fact(3) 6

67 Cal fact(3) from command window… >> myans = fact(3) myans = 6 >> 6

68 Answer is returned to place that called it, which in this case was the command window!

69 What about bad inputs? What happens if input is -1? If input is -5?

70 What about bad inputs? What happens if input is -1? If input is -5? We theoretically recurse forever! (though our activation stack keeping track of all the temporary workspaces will eventually run out of memory and we will get an error)

71 Wrapper Functions Use a “wrapper function” to preprocess your data! A wrapper function checks input(s), and if they are value, makes the first call to the recursive function Captures result from recursive function and then returns it back to where called the wrapper function originally Note, the wrapper function should usually only run once per call (unlike the recursive helper function)

72 Why recursion? In general, any recursive function could be written as a loop (likely a while loop) Loops are usually faster (because Matlab doesn’t have to make all those temporary workspaces and copy the parameter values in) But some problems are solved more simply using recursion – Printing out structures (e.g. nested structures) – Listing out directories and files – Crawling the web

73 Recursively List Contents of Directory

74 Given a directory, we want to list all items in that folder, including items in Don’t even worry about matlab yet…let’s think about the steps involved in general Given a directory (you, yourself), what would you do to start listing out the filenames? What would you do when you encountered another folder?

75 Recursively Listing Directories Open folder to see list of items For each item listed in folder – Check to see if it is a directory If it is a directory, we need to search it (start whole process over) Else, if not a directory (meaning it’s just a file name), print it out to command window and continue on

76 Translating to Matlab function mydir(dirname) % recursive dir traversal lst = dir(dirname); % returns a structure array for pos = 3:length(lst) %skip over first two items, as they are always '.' and '..' if lst(pos).isdir % check if current is a directory mydir([dirname '/' lst(pos).name]) else fprintf('%s\n', lst(pos).name); % print out file name end

77 When you shouldn’t use recursion Consider Fibonacci numbers fib(n) = fib(n-1) + fib(n-2) fib(0) = 0 fib(1) = 1

78 Redundant Calculations To compute fib(n), we recursively compute fib(n-1). When that recursive call returns, we compute fib(n-2) using another recursive call – We have already computed fib(n-2) in the process of computing fib(n-1) – We make two calls to fib(n-2)

79 Redundant Calculations Making two method calls would double the running time Compounding effect: each recursive call does more and more redundant work – Each call to fib(n-1) and each call to fib(n-2) makes a call to fib(n-3); there are 3 calls to fib(n-3) – Each call to fib(n-2) or fib(n-3) results in a call to fib(n-4), so 5 calls to fib(n-4)

80 Redundant Calculations III The recursive routine fib is exponential – Meaning the amount of work done grows exponentially based on the input value n

81


Download ppt "Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that."

Similar presentations


Ads by Google