CC 103 Report
CC 103 Report
Group 4
RECURSIVE DEFINITIONS
OUTPUT
B A S I C P R O P E RT I E S O F
RECURSION
1.Base Case
a.The condition that stops the recursion.
b.Prevents the function from calling itself forever.
2.Recursive Case
a.The part of the function where it calls itself with a modified
argument.
b.Helps break the problem into smaller parts.
3.Termination
a.A recursive function must always reach a base case to
stop recursion.
4.Stack Usage
a.Each recursive call uses a new frame in the call stack.
b.Too many recursive calls can lead to stack overflow.
TA I L R E C U R S I O N
DEFINITION
Tail recursion occurs when the recursive call is the last
operation in the function.
There is no additional work to be done after the recursive call
returns.
PROPERTIES
Optimization: Some languages or compilers can optimize
tail recursion to avoid adding new frames to the call stack,
making it as efficient as an iterative solution.
EXAMPLE
OUTPUT
N O N -TA I L R E C U R S I O N
DEFINITION
Non-tail recursion occurs when the recursive call is followed
by additional operations or computations.
The function must do more work after the recursive call
returns.
PROPERTIES
Stack Usage: Each recursive call adds a new frame to the
stack, potentially leading to high memory usage.
Performance: Less efficient compared to tail recursion in
EXAMPLE
OUTPUT
INDIRECT RECURSION
DEFINITION
Indirect recursion occurs when a function calls another
function, which in turn calls the original function, creating a
loop of function calls.
Unlike direct recursion, where a function calls itself, indirect
recursion involves multiple functions interacting with each
other.
Indirect recursion is useful in scenarios where a problem
naturally fits into a multi-step process involving multiple
EXAMPLE
OUTPUT
NESTED RECURSION
Occurs when a recursive function calls itself in a way
that involves multiple levels of recursion.
A function makes a recursive call that itself involves
further recursive calls.
This type of recursion can become quite complex
because it involves multiple layers of recursive calls.
EXAMPLE
Suppose you want to count the number of distinct paths from the top-left
corner to the bottom-right corner of an m x n grid, where you can only move
right or down. The function will use nested recursion to solve the problem.
OUTPUT
E XC E SS I V E R E C U R S I O N
Occurs when a recursive function makes too many recursive
calls, leading to inefficient performance or runtime errors.
This can happen due to deep recursion, a high number of
recursive calls, or poorly defined base cases.
P R O B L E M S W / E XC E SS I V E
RECURSION
1. Stack Overflow: Each recursive call adds a new frame
to the call stack. If the recursion depth is too high, it can
exceed the stack limit and cause a stack overflow error.
For large values of n, this function can make too many recursive calls and potentially
cause a stack overflow.
OUTPUT
BACKTRACKING
A problem-solving technique used to find solutions by
exploring all possible options and undoing choices when they
lead to dead ends.
It's particularly useful for problems that involve finding a
solution from a set of constraints or exploring all possibilities.
KEY CONCEPTS OF
BACKTRACKING
1.Choice: At each step, make a choice among the available
options.
2.Constraint: Ensure that the choice satisfies the problem
constraints.
3.Backtrack: If the choice leads to an invalid solution or a
dead end, undo the choice and try another option.
4.Base Case: Determine when to stop the recursion (e.g.,
when a valid solution is found or when all possibilities have
EXAMPLE
Example we'll generate all possible sequences of binary digits (0s
and 1s) of length n.
OUTPUT
BENEFITS OF RECURSION
1.Simplifies Code: Recursive solutions are often more concise
and easier to understand, especially for problems with self-
similar structures like trees.
2.Performance Concerns:
o Recursion can lead to redundant calculations and inefficient
performance, especially in cases like calculating Fibonacci
numbers.
5.Complexity in Understanding:
o Description: Recursion can be challenging to grasp and
manage, especially for those new to programming.