How To Set Recursion Limit In Python
Python | Handling recursion limit
When yous execute a recursive function in Python on a large input ( > 10^4), you lot might meet a "maximum recursion depth exceeded error". This is a common error when executing algorithms such as DFS, factorial, etc. on big inputs. This is also common in competitive programming on multiple platforms when you are trying to run a recursive algorithm on various test cases.
In this commodity, we shall look at why this error occurs and how to handle it in Python. To sympathize this, we need to first look at tail recursion.
Tail recursion –
In a typical recursive role, we normally make the recursive calls start, and and so take the return value of the recursive call to summate the result. Therefore, we only go the final result after all the recursive calls have returned some value. Just in a tail recursive function, the various calculations and statements are performed kickoff and the recursive telephone call to the function is made afterward that. By doing this, we pass the results of the electric current step to the next recursive call to the function. Hence, the terminal statement in a Tail recursive function is the recursive call to the office.
This means that when nosotros perform the next recursive call to the function, the current stack frame (occupied by the current part telephone call) is non needed anymore. This allows us to optimize the lawmaking. We Simply reuse the current stack frame for the next recursive step and repeat this procedure for all the other office calls.
Using regular recursion, each recursive call pushes some other entry onto the call stack. When the functions return, they are popped from the stack. In the case of tail recursion, we can optimize it and so that only one stack entry is used for all the recursive calls of the part. This ways that even on large inputs, there can be no stack overflow. This is chosen Tail recursion optimization.
Languages such every bit lisp and c/c++ have this sort of optimization. But, the Python interpreter doesn't perform tail recursion optimization. Due to this, the recursion limit of python is usually set to a small value (approx, 10^four). This means that when you provide a large input to the recursive function, y'all will get an error. This is done to avoid a stack overflow. The Python interpreter limits the recursion limit and so that infinite recursions are avoided.
Handling recursion limit –
The "sys" module in Python provides a function called setrecursionlimit() to modify the recursion limit in Python. It takes i parameter, the value of the new recursion limit. By default, this value is usually 10^three. If y'all are dealing with big inputs, you tin can gear up it to, 10^6 then that large inputs can be handled without any errors.
Instance:
Consider a program to compute the factorial of a number using recursion. When given a big input, the plan crashes and gives a "maximum recursion depth exceeded error".
Python3
def
fact(n):
if
(n
=
=
0
):
render
1
return
due north
*
fact(northward
-
one
)
if
__name__
=
=
'__main__'
:
f
=
int
(
input
(
'Enter the number: \north'
))
print
(fact(f))
Output :
Using the setrecursionlimit() method, nosotros can increase the recursion limit and the plan can be executed without errors fifty-fifty on large inputs.
Python3
import
sys
sys.setrecursionlimit(
10
*
*
6
)
def
fact(n):
if
(n
=
=
0
):
return
1
return
n
*
fact(n
-
1
)
if
__name__
=
=
'__main__'
:
f
=
int
(
input
(
'Enter the number: \northward'
))
print
(fact(f))
Output :
Source: https://www.geeksforgeeks.org/python-handling-recursion-limit/
0 Response to "How To Set Recursion Limit In Python"
Post a Comment