2.1.1 Recurrence Relation (T(n) = T(n-1) + 1) (Part-1)
2.1.1 Recurrence Relation (T(n) = T(n-1) + 1) (Part-1)
Overview
In this lecture, we explore the concept of recurrence relations through the lens of recursive functions. We focus on tracing recursive function execution, establishing recurrence relations, and solving them using various methods. A simple C language function example is used to demonstrate these concepts.
Tracing a Recursive Function
-
Example Function:
void test(int n) { if (n > 0) { printf("%d ", n); test(n - 1); } } -
Execution Trace:
- Calling
test(3)initiates a sequence of prints: 3, 2, 1. - This illustrates the recursive nature of the function: each call prints a value and makes another recursive call with
n-1untilnequals 0.
- Calling
Writing a Recurrence Relation
- Conceptual Explanation:
- A recurrence relation expresses the time complexity of a recursive function. It often relates the complexity of a problem size
nto its subproblems.
- A recurrence relation expresses the time complexity of a recursive function. It often relates the complexity of a problem size
- Function Analysis:
- For the given
testfunction, every call to the function decreasesnby 1 and performs a constant amount of work (printing). - The recurrence relation is defined as:
- for
- , representing the base case where the function does no significant work.
- For the given
Solving the Recurrence Relation
Substitution Method
- Methodology:
- We iteratively substitute the recurrence relation into itself to express in terms of .
- Example Steps:
- Substitute to get
- Continue until
- Final Solution:
- Since , .
- This simplifies to , indicating the function's linear time complexity with respect to the number of recursive calls.
Summary
This lecture has demonstrated the process of tracing a recursive function, writing its recurrence relation, and solving it using the substitution method. The example function provided a clear illustration of how recursive calls unfold and how their time complexity can be captured and solved via a recurrence relation. The recurrence relation effectively represents the linear complexity of the recursive function, highlighting an important aspect of algorithm analysis.