Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Recursion. Recursion Defined A procedure or function which calls itself. Powerful mechanism for repetition. Makes algorithms more compact.

Similar presentations


Presentation on theme: "Introduction to Recursion. Recursion Defined A procedure or function which calls itself. Powerful mechanism for repetition. Makes algorithms more compact."— Presentation transcript:

1 Introduction to Recursion

2 Recursion Defined A procedure or function which calls itself. Powerful mechanism for repetition. Makes algorithms more compact and simple. Module calls a “clone” of itself. Very useful, especially for dynamic data types.

3 Three Characteristics of Recursion Calls itself recursively Has some terminating condition Moves “closer” to the terminating condition.

4 Two Flavors of Recursion if (terminating condition) then do final actions else move one step closer to terminating condition call “clone” of module endif - or - if (NOT (terminating condition)) then move one step closer to terminating condition call “clone” of module endif

5 Everyday Example with Final Actions procedure Pay_The_Bills() if (No_More_Bills_To_Pay) then Mail_The_Bills else Grab a bill Determine payment Write a check Place check in envelope Place stamp on envelope Pay_The_Bills() endif endprocedure //Pay_The_Bills

6 Everyday Example with No Final Actions procedure Wash_The_Dishes() if (More_Dirty_Dishes) then Grab a dirty dish Wash it Put it in drainer Wash_The_Dishes() endif endprocedure //Wash_The_Dishes

7 A Recursive Procedure Problem: Count from N to 10. procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen Call the procedure CountToTen (7)

8 Tracing The Recursion To keep track of recursive execution, do what a computer does: maintain information on an activation stack. Each stack frame contains: Module identifier and variables Any unfinished business ModuleID: Data values Unfinished business

9 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=7

10 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=7 7

11 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=7 7

12 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=7 7 CountToTen: count=8 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen

13 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=7 7878 CountToTen: count=8 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen

14 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=7 7878 CountToTen: count=8 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen

15 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=7 7878 CountToTen: count=8 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=9

16 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=7 789789 CountToTen: count=8 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=9

17 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=7 789789 CountToTen: count=8 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=9

18 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=7 789789 CountToTen: count=8 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=9 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=10

19 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=7 7 8 9 10 CountToTen: count=8 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=9 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=10

20 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=7 7 8 9 10 CountToTen: count=8 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=9 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=10

21 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=7 7 8 9 10 CountToTen: count=8 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=9 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=10 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=11

22 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=7 7 8 9 10 CountToTen: count=8 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=9 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=10

23 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=7 7 8 9 10 CountToTen: count=8 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=9

24 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=7 7 8 9 10 CountToTen: count=8 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen

25 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=7 7 8 9 10

26 7 8 9 10 Return to the algorithm.

27 Reversing the Work and Recursion Problem: Count from 10 to N. procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen Now the work will happen as the frames pop off the stack!

28 procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen CountToTen: count=7

29 procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen CountToTen: count=7

30 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=7 CountToTen: count=8 procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen

31 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=7 CountToTen: count=8 procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen

32 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=7 CountToTen: count=8 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen CountToTen: count=9

33 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=7 CountToTen: count=8 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen CountToTen: count=9

34 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=7 CountToTen: count=8 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=9 procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen CountToTen: count=10

35 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=7 CountToTen: count=8 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=9 procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen CountToTen: count=10

36 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=7 CountToTen: count=8 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=9 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=10 procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen CountToTen: count=11

37 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=7 10 CountToTen: count=8 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=9 procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen CountToTen: count=10

38 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=7 10 CountToTen: count=8 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=9 procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen CountToTen: count=10

39 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=7 10 9 CountToTen: count=8 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen CountToTen: count=9

40 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=7 10 9 CountToTen: count=8 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen CountToTen: count=9

41 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=7 10 9 8 CountToTen: count=8 procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen

42 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=7 10 9 8 CountToTen: count=8 procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen

43 procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen CountToTen: count=7 10 9 8 7

44 procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen CountToTen: count=7 10 9 8 7

45 10 9 8 7 Return to the algorithm.

46 Palindrome: Problem A palindrome is a string that reads the same backward or forward e.g. R A C E C A R How do you check to see if a string is a palindrome –What is our sub-problem? –What is the base case? –What is the recursive case?

47 Palindrome public static bool IsPalindrome(string s, int left, int right) { if (left >= right) //terminating case 1 return true; else if (s[left] != s[right]) //terminating case 2 return false; else //recursion return IsPalindrome(s, left + 1, right -1); }


Download ppt "Introduction to Recursion. Recursion Defined A procedure or function which calls itself. Powerful mechanism for repetition. Makes algorithms more compact."

Similar presentations


Ads by Google