1 / 188

Introduction to Recursion

Introduction to Recursion. Please fasten your seat belts. Recursion is difficult to master. Recursion yield elegant, concise code Yet, it is difficult to debug. Minor mistakes can be disastrous for the program Infinite recursion is the most common problem

Télécharger la présentation

Introduction to Recursion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Recursion Please fasten your seat belts.

  2. Recursion is difficult to master Recursion yield elegant, concise code Yet, it is difficult to debug. Minor mistakes can be disastrous for the program Infinite recursion is the most common problem Infinite recursion is more complicated than infinite loop because it consume all the memory when stacking the current status of the function. For the inexperienced programmer, the program may sometimes report a error which has no immediate relation to the recursive function

  3. Recursion When “something” is defined in terms of itself! Recursive methods: A method that calls itself Recursive classes: A class that contains an instance of itself

  4. Recursion • Is the "art" of defining a concept using the concept itself • They are the computer science equivalent of mathematical induction • In computer science terms we talk about recursive functions: functions that invoke itself • Not allowed in some languages • When allowed they are very powerful • They express repetition without loops • They simplify code which would otherwise be confusing and unclear. • It is an important tool supporting data abstraction

  5. Recursive Definitions • Any recursive definition has to have two parts: • Basis of recursion: This is a non-recursive statement establishing definition for a fixed number of objects. The bases is the condition that makes the recursion stop • Recursion: It is a condition that is written in such a way that after repeated executions of this code it will reduce to the basis. // Definition of function (x raised to y) // Where y is positive BASE : x raised to 0 is 1 RECURSION: x raised to y is equal x times x raised to y-1

  6. Recurrence Relations • It is an relation describing the recursive function in terms of its values on smaller inputs • It will be widely used when we get to analysis of algorithms • There are three methods to solve recurrences • Substitution method: based on mathematical induction • Iteration method: converts the recurrence into a summation. • Master method: Provides bounds for recurrences of the form: T(n) = aT(n/b) + f(n) • where a >= 1, b > 1 and f(n) is a given function • Requires memorization of a few cases • Recurrence will be used quite frequently when describing sorting and tree algorithms • Since a few of these algorithms are recursive

  7. BNF (Backus-Naur Form) • Most Programming languages use the BNF notation to describe its syntax • It is less ambiguous than English • It is a generator technique. It defined how to construct, or generate, all possible syntactically valid strings of a language. • It is a recursive technique <IntegerConstant> ::= <Digit> | <Digit><IntegerConstant> <Digit> ::= 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0

  8. Understanding Recursion • To understand recursion we need to understand stacks • Each time a function A calls a function B the status of A is stored in the run-time stack. • When the function B returns the status of A is popped from the top of the stack. Let's consider this program void whatIsThis() { char inChar; cin.get(inChar); if (inChar == '\n') cout << '\n'; else { whatIsThis(); cout << inChar; } }

  9. "Exploding" the recursion • Instead of imagining as stacks we could also see this as creation of new scopes at each iteration of the recursion void whatIsThis() { char inChar; cin.get(inChar); if (inChar == '\n') cout << '\n'; else { char inChar; { cin.get(inChar); if (inChar == '\n') cout << '\n'; else { cout << inChar; } } cout << inChar; } }

  10. Stacks • Is a “pile” or “collection” or “set” of items. • Items can only be added to the top • Items can only be taken off the top • “Last-in-first-out” (”LIFO”) Push Pop Thing 3 Thing 2 Thing 1

  11. What is a Stack? A data structure for storing items which are to be accessed in last-in first-out order (LIFO).

  12. PUSH Thing 1 What is a Stack? A data structure for storing items which are to be accessed in last-in first-out order (LIFO).

  13. PUSH Thing 2 Thing 1 What is a Stack? A data structure for storing items which are to be accessed in last-in first-out order (LIFO).

  14. pop Thing 1 What is a Stack? A data structure for storing items which are to be accessed in last-in first-out order (LIFO).

  15. PUSH Thing 2 Thing 1 What is a Stack? A data structure for storing items which are to be accessed in last-in first-out order (LIFO).

  16. PUSH Thing 3 Thing 2 Thing 1 What is a Stack? A data structure for storing items which are to be accessed in last-in first-out order (LIFO).

  17. pop Thing 2 Thing 1 What is a Stack? A data structure for storing items which are to be accessed in last-in first-out order (LIFO).

  18. pop Thing 1 What is a Stack? A data structure for storing items which are to be accessed in last-in first-out order (LIFO).

  19. pop What is a Stack? A data structure for storing items which are to be accessed in last-in first-out order (LIFO).

  20. var2 var3 var1 The Activation Stack • The white box represents the computers memory. • The algorithm gets the first frame at the bottom of the stack. • Variables which are used within a module reside in that module’s stack frame. Algo

  21. Proc_1 this_var that_var Adding Modules to the Stack • When main calls a module, the module gets its own frame “pushed” on the stack. • The module’s variables live in that new frame. • ONLY the top frame is active. All frames underneath it are stopped. Algo var1 var2 var3

  22. Proc_1 this_var that_var Removing modules from the stack • When the module completes its instructions, it’s frame is “popped” off the stack and all of its data dies. • To survive, they must actually be stored in a frame that still exists (passed back). Algo var1 var2 var3

  23. How In Parameters Work In formal parameters get a copy of the matching actual parameter. In the algorithm: Proc_One (this_var) In the module: procedure Proc_One(this_one in num)

  24. this_one Proc_One 4 Algo How In Parameters Work • In formal parameters get a copy of the matching actual parameter. 4 7 4 9 this_var that_var other_var

  25. How In/Out Parameters Work • In/out formal parameters act as a reference to the matching actual parameter. • Reading and writing are allowed. In the algorithm: Proc_One (this_var) In the module: procedure Proc_One(this_one in/out num)

  26. 1 this_one Proc_One 7 1 Algo How In/Out Parameters Work • In/out formal parameters act as a reference to the matching actual parameter. • Reading and writing are allowed. Print(this_one) this_one <- 1 this_one <- 7 R 7 9 4 this_var that_var 1 other_var 7

  27. How Out Parameters Work • Out formal parameters act as a reference to the matching actual parameter. • Writing is only allowed at first. • After writing, reading is also allowed. In the algorithm: Proc_One (this_var) In the module: procedure Proc_One(this_one out num)

  28. this_one Proc_One 8 Algo How Out Parameters Work • Out formal parameters act as a reference to the matching actual parameter. • Writing is only allowed at first. • After writing, reading is also allowed. this_one <- 8 R 4 9 7 this_var that_var other_var 8

  29. Stack Trace Example procedure juggle (x isoftype in num, y isoftype out num, z isoftype in/out num) y <- x + z print (x, y, z) x <- z + 4 z <- x + 2 endprocedure algorithm TraceExample a, b, c isoftype num a <- 1 b <- 2 c <- 3 print(a, b, c) juggle(c, a, b) print(a, b, c) endalgorithm

  30. Algorithm Demo a, b, c isoftype num a <- 1        b <- 2        c <- 3        print(a,b,c)        juggle(c,a,b)        print(a,b,c)endalgorithm Juggle x y z Demo a c b Procedure Juggle(x iot in Num,                 y iot out Num,                 z iot in/out Num)        y <- x + z        print(x,y,z)        x <- z + 4        z <- x - 2endprocedure Output 1 2 3 3 3 5 2 6 R R 5 4 3 1 3 2 5 4

  31. Recursive Functions • Like mathematical definitions, functions can be recursive • Direct recursion: Occurs when a function is called in its own body • Indirect recursion: Occurs when one function initiates a sequence of function invocations that eventually invokes the original Indirect Recursion Direct Recursion func A(int x) { . . . A(20); . . . } func A(int x) { . . . B(20); . . . } func B(int x) { . . . A(20); . . . }

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

  33. 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

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

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

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

  37. 7 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=8 CountToTen: count=7

  38. 7 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=8 CountToTen: count=7

  39. 7 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=8 CountToTen: count=7

  40. 7 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 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=9 CountToTen: count=8 CountToTen: count=7

  41. 7 8 9 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 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=9 CountToTen: count=8 CountToTen: count=7

  42. 7 8 9 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 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=9 CountToTen: count=8 CountToTen: count=7

  43. 7 8 9 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 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=10 CountToTen: count=9 CountToTen: count=8 CountToTen: count=7

  44. 7 8 9 10 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 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=10 CountToTen: count=9 CountToTen: count=8 CountToTen: count=7

  45. 7 8 9 10 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 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=10 CountToTen: count=9 CountToTen: count=8 CountToTen: count=7

  46. 7 8 9 10 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 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 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=11 CountToTen: count=10 CountToTen: count=9 CountToTen: count=8 CountToTen: count=7

  47. 7 8 9 10 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 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=10 CountToTen: count=9 CountToTen: count=8 CountToTen: count=7

  48. 7 8 9 10 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 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=9 CountToTen: count=8 CountToTen: count=7

  49. 7 8 9 10 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=8 CountToTen: count=7

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

More Related