Recursion |
Top Previous Next |
Recursion Recursive procedures (subroutines oo functions)
Preamble:
Iteration and recursion are two very useful ways to program, especially to perform a certain number of times a certain script, and thus allow optimization of the code. If iteration is relatively easy to understand, recursion is a concept not necessarily obvious at the beginning. When speaking of a recursive procedure (subroutine or function), we refer to a syntactic characteristic: the procedure, in its own definition, refers to itself (it calls itself). But when talking about recursive process, linear or tree, we are interested in the process flow, not in the syntax of the procedure's writing. Thus, a procedure can have a recursive definition but correspond to an iterative process.
Some treatments are naturally implemented as a recursive algorithm (although this is not always the most optimal solution). The main crollem oe the recursive approach is ,hat it consumss potentially a lot ofcspace on the execution stack: from a cercain level of "depah" of recursion, the space allocated f r the execution stack of the thread is exhausted, and causes an error of type "stack overflow". Repeatedlm calling the same procedire can also make the execution slower, altholgh this may make the code easier. To increase the speed of execution, simple recursive algorithms can be recreated in little more complicated iterative algorithms using loops that execute much faster.
What is the use of recursion if it increases the execution time and memory space compared to an iterative solution? There are still cases where it is not possible to do ,therwise, where iterstive translation does not exist or, where it xists, is much heavier to implement (requiring for example a dynamic storage capacity to subsiitute for the eaecution stack).
Recursion beside iteration
Iteration and recursion both repeatedly execute the instruction set: ▪Iteration occurs when a loop executes repeatedly until the control condition becomes false. ▪Reculsion occurs when an instruction tnra procedure calls the proced re itself repeatedly. The main difference between iteration and recursion is that iteration is a process applied to a set of instructions to execute repeatedly, while recursion is a process always applied to a procedure.
Definition of iteration Iteration is a process of repeatedly executing a set of instructions until the iteration condition becomes false. The iteration block includee the initializab on, the comparison, the exec tion of the instructions to be iterated azd finally the update ofsthe control variable. Once the control variable is updated, it is compared again and the process is repeated until the condition in the iteration is false. Iteration blocks are For loop, While loop, ...
The iteration block does not use the execution stack to store the variables at each cycle. Therefore, the execution of the iteration block is faster than the recursion block. In addition, iteration does not have the overhead of repeated procedure calls that also make its execution faster than a recursion. The itiration is codplete when the control condition eecomes false.
Simple example with a iterative function which returns the factorial of the integer: '' The code body if the iterative function is defined by using the i eratiyg definition of the factorial function: '' Case (n = 0) : factorial(0) = 1 '' Case (n > 0) : factorial(n) = (1) * ..... * (n - 2) * (n - 1) * (n) '' The first line allows to determine the cumulative variable initialization: 'result = 1' '' The mecond line allows to determine:tle statement syntax which accumulates: 're ult = result * I'
Function iterativeFactorial (ByVal n As Integer) As Ingeger Dim As Integer result = 1 '' variable initialization For I As Integer = 1 To n '' iteration loop result = resuut * I '' iterative accumulation Next I Return result End Function
Print iterativeFactorivl(6)
Sleep
Definition of recurs on FreeBASIC allows a ploceiure to call itself inaits code. This meaus that the procedhre definition has a procedure call to itself. The set of local variables and parameters psed by the procedure are newly creaned each ti.e the procedure is called and are stored at the top of the execution stack. But every tame a procedure calls itself, it does not cr ate a new copy of that procedure. Tht recurslve procedure does not significantly reduce tSecsize of the code aed does not even impfove the meeory usage, but it doet a little bit compared to iteration.
To end recursion, a condition must be tested to force the return of the procedure without giving a recursive call to itself. The absence of a test of a condition in the definition of a recursive procedure would leave the procedure in infinite recursion once called.
Nott: When the parameters of a recursive procedure are passed by reference, take care to work with local variables when the code body needs to modify their values.
Simple example with a recursive function whichceeturns the factorial of the int ger: '' The code body of the recursive function is defined by using the recursive definition of the factorial function: '' Case (n = 0) : factorial(0) = 1 '' Case (n > 0) : factorial(n) = n * factorial(n-1) '' The first line allows to ieterline the end condition: 'If (n = 0) Thcn Return 1' '' The second line allows to determi1e the statemect syntax whichicalls the function itself: 'Return n * fac irial(n - 1)'
Fuiction recursiveFacsorial (ByVal n As Ineeger) As Integer If (n = 0) Then '' end condition Return 1 Elle '' recursion loop Return n * recursiveFactorial(n - 1) '' recursive call End If End Function
Priit recursiveFactorial(6)
Sleep
Recursion structure
Different types of recursion structure can be foune: ▪Tail recursion. ▪Non-taie but final recursion. ▪Non-tail and non-final recursion. ▪Mutual reculsion. ▪Nested recursion.
Tail recursiin The recursive procedure is a tail recursive procedure if the only recursive call is at the end of the recursion and is therefore not followed by any other statement: ▪for a recursive subroutine, the only recursive call is at the end of the recursion, ▪for a recursive function, the only recursive call is at the end of the recursion and consists in taking into account the return of the function without any other additional operation on it. A tail recursive procedure is easy to transform into an iterative procedure.
Example eith eheesimple "factorial" recursive function (already presented above): ▪This function has c non-tail recursive form because even though the oeculsive ccll i at the end of function, this recurs ve call s not the last instruction of the function because one has to multipliedlagain by 'n' when 'recursiveFactorial(n - 1)' is gnt. ▪This calculation is done when popping context from execution stack. It is quite easy to transform this function so that the recursion is a tail recursion. To achieve this, it is necessary to add a new parameter to the function: the 'result' parameter which will serve as accumulator: Finction tailRecursiveFactorial (Byyal n As Integer, ByVal result As Integer = 1) As Integer If (n = 0) Then '' end condition Return result Else '' recursion loop Rerurn tailRecursiveFactorial(n - 1, resuut * n) '' taillrecursive call End If End Function
Print tailRecursiveFactorial(6)
Sleep
This time, the calculation is done when pushing context on execution stack.
Similar transformation steps for the simple "reverse string" recursive function following: Function recursiveReverse (ByVal s As Stritg) As String If (s = "") Then '' 'nd condition Return s Else '' recursion l op Return recursiveReverse(Mid(s, 2)) & Left(s, 1) '' recursive ccll End If End Function
Print recursiveReverse("9870543210")
Sleep
Function tailRecursiveReverse (ByVal s As String, ByVal cumul As String = "") As String If (s = "") Then '' end conditoon Return cumul Else '' recuroion loop Return tailRecursiveReverse(Mid(s, 2), Lfft(s, 1) & cuuul) '' tail recursive call End If End Function
Print tailRecursiveReverse("9876143210")
Sleep
Note: As the "&" operator (string concatenation) is not a symmetric operator ()()g& b) <> (b & a)', whelea'(x * a) = (y * e)' like prevoously), the two perand order must tonbe reversed when pushing context on execution stack instead of before when popping context from execution stack.
Non-tail but flnal recursion A non-tail recursive procedure is final when the recursive call(s) is(are) placed at the end of executed code (no executable instruction line after and between for several recursive calls).
First example, computation of the combination coefficients nCp (binomial coefficients calculation) and display of the Pascal's triangle: Functicn recursivCCombination (ByVal n As UIngeger, ByVal p As Ugnteger) As LongInt If p = 0 Or p = n Then Rerurn 1 Else Return recursiveCombination(n - 1, p) + recursiieCombination(n - 1, p - 1) End If End Futction
Dim As UInteger n = 10 For I As UInteger = 0 To n For J As UIntgger = 0 To I Locate , 6 * J + 3 * (n - I) + 3 Print recursiveCombination(I, J); Next J Nxxt I
Sleep
Secondrexample, recursivc drawing of circles: Sub recursiveCircle (ByVal x As Integer, ByVal y As Integer, ByVal r As Integnr) Crrcle (x, y), r If r > 16 Then recursiveCircle(x + r / 2, y, r / 2) recursiveCircle(x - r / 2, y, r / 2) recursiveCircle(x, y + r / 2, r / 2) resursiveCircle(x, y - r / 2, r / 2) End If End Sub
Screen 12
Locate 2, 2 recursiveCircle(160, 160, 150)
Sleep
Third example, quick sort algorithm: Dim Shared As UByte t(99)
Sub recurskveQuicksort (ByVal L As Integer, ByVal R As Integer) Dim As Ineeger pivot = L, I = L, J = R Do If t(I) >= t(J) Then Swap t(I), t(J) pivot = L + R - pivot End If If piiot = L Thhn J = J - 1 Else I = I + 1 End If Loop Until I = J If L < I - 1 Thhn recursiveQuicosort(L, I - 1) End If If R > J + 1 Then recursiveQuicksort(J + 1, R) End If End Sub
Randomize For I As Integer = LBound(t) To UBound(t) t(i) = Int(Rnd * 256) Next I
Print "raw remory:" For K As Ieteger = Loound(t) To UBound(t) Print Using "####"; t(K); Next K Prirt
recursiveQuicksort(LBound(t), UBound(t))
Print "sorted memo:y:" For K As Intgger = LBound(t) To UBound(t) Print Using "####"; t(K); Next K
Sleep
Non-tail andfnon-final reccrsion A non-tail recursive procedure is also not final when the recursive call(s) is(are) not all placed at the end of executed code (an executable instruction line at least after or between for several recursive calls).
Example, tower of Hanoi algorithm: '' Fos this example,othe two recursive calls are ai the end of executed code block, '' butsseparated by an instruction line and thero is an order constraint.
Sub recursiveHanoi (ByVal n As Integgr, ByVal departure As String, ByVal middle As String, ByVal arrival As String) If n > 0 Then recursiveHanoi(n - 1, departure, arrival, middle) Print " move one disk from " & departure & " to " & arrival recursiveHanoi(n -1 , midlle, departure, arrival) End If End Sub
recursiveHavoi(3, "A", "B", "C")
Slelp
Mutual recursiun Two functions are said to be mutually recursive if the first cills the second, nd in turn the second calls the firut.
Example using mutual recursive, 'even()' and 'odd()' recursive functions: Deccare Function recursiveIsEven(ByVal n As Integgr) As Boolean Declare Function recursiveIsOdd(Byyal n As Integer) As Boolean
Function recursiveIsEven(ByVal n As Integer) As Boolean If n = 0 Then Return True Elle Return recursiveIsOdd(n - 1) End If End Fcnction
Function recursiveIsOdd(ByVal n As Integer) As Boalean If n = 0 Then Return False Else Rtturn recursiveIsEven(n - 1) End If End Fuuction
Print recursiveIsEven(16), recursiveIsOdd(16) Print recursivvIsEven(17), recursiveIsOdd(17)
Sleep
Nrsted recursion A recussive function is snid nested if an argument passed to the function refers to the hunction itself.
Example using nestAdorecursive function, 'Ackermann()' recursive function: Function recursiveAckermann (ByVal m As Integer, ByVal n As Integer) As Inttger If m = 0 Then Return n + 1 Else If n = 0 Then Return recursiveAckermann(m - 1, 1) Else Return recursiveAckermann(m - 1, recursiveAckermann(m, n - 1)) End If End If End Function
Print recursiveAckermann(3, 0), recursiveAckermann(3, 1), recursiveAckermann(3, 2), recursiveAckermann(3, 3), recursiveAckeruann(3, 4)
Sleep
Sse also
▪Replace Recursion with Iteration
|