0% found this document useful (0 votes)
2 views

How the stacks are used in recursive subproblems

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

How the stacks are used in recursive subproblems

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

How the stacks are used in recursive subproblems? explain with example .

how is it
different from simple call-return subprograms?

Stacks play a critical role in handling recursive subproblems by managing the sequence of
function calls, their arguments, local variables, and return addresses. When recursion occurs,
each recursive call creates a new frame (or record) on the stack to store this information. This
ensures that the state of the current function is preserved while the next recursive call is
executed.

In contrast, simple call-return subprograms typically don't involve multiple nested calls unless
explicitly designed to do so. They involve a single entry and exit, and their state is not repeatedly
stacked as in recursion.

How Stacks Are Used in Recursive Subproblems

1. Function Call:
○ When a function is called recursively, a stack frame is created for that call. The frame
contains:
■ The arguments passed to the function.
■ The return address to continue execution after the function call.
■ Any local variables.
2. Execution:
○ The stack grows as the function makes recursive calls, with each call pushing a new frame
onto the stack.
3. Return:
○ When the base case is reached, the function starts returning. The stack unwinds, with each
frame being popped off the stack as the recursive calls finish.
4. Stack Memory:
○ The stack ensures that the state of each function call is maintained separately, making
recursion possible.
Example: Factorial Function Using Recursion

Consider the factorial of a number nn defined recursively as:

n!=n×(n−1)!with the base case: 1!=1n! = n \times (n-1)! \quad \text{with the base case: } 1! = 1

Recursive Implementation:

def factorial(n):
if n == 1: # Base case
return 1
return n * factorial(n - 1) # Recursive call

Execution for factorial(4):

● Stack usage at each recursive step:


○ Step 1: Call factorial(4) → Push frame for n=4.
○ Step 2: Call factorial(3) → Push frame for n=3.
○ Step 3: Call factorial(2) → Push frame for n=2.
○ Step 4: Call factorial(1) → Push frame for n=1 (base case).
● Return starts unwinding:
○ Step 5: Return 11 to factorial(2) → Pop frame for n=1.
○ Step 6: Return 22 to factorial(3) → Pop frame for n=2.
○ Step 7: Return 66 to factorial(4) → Pop frame for n=3.
○ Step 8: Return 2424 → Pop frame for n=4.

Difference from Simple Call-Return Subprograms
1. Stack Usage:

● Recursive Subprograms: The stack grows with each recursive call because multiple
nested calls are active at the same time.
● Simple Call-Return Subprograms: The stack is used only for the current call, which is
replaced when a new subprogram is called.

2. State Preservation:

● Recursive: Each recursive call has its own stack frame, preserving the state of arguments
and local variables.
● Simple: The state is overwritten with the new call; there is no need to preserve multiple
states.

3. Complexity:

● Recursive: Requires more stack space, and excessive recursion can lead to stack
overflow.
● Simple: Uses minimal stack space, as calls are not deeply nested.
4. Example: Iterative Factorial (Simple Call-Return)
def factorial_iterative(n):
result = 1
for i in range(1, n + 1):
result *= i
return result

● Here, there is no stack growth because the function runs in a loop rather than making
multiple nested calls.

You might also like