Angad Sandhu
Search
Browse
On this page

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-1 until n equals 0.

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 n to its subproblems.
  • Function Analysis:
    • For the given test function, every call to the function decreases n by 1 and performs a constant amount of work (printing).
    • The recurrence relation is defined as:
      • T(n)=T(n−1)+1T(n) = T(n-1) + 1 for n>0n > 0
      • T(0)=1T(0) = 1, representing the base case where the function does no significant work.

Solving the Recurrence Relation

Substitution Method

  • Methodology:
    • We iteratively substitute the recurrence relation into itself to express T(n)T(n) in terms of T(n−k)T(n-k).
  • Example Steps:
    1. T(n)=T(n−1)+1T(n) = T(n-1) + 1
    2. Substitute T(n−1)T(n-1) to get T(n)=T(n−2)+2T(n) = T(n-2) + 2
    3. Continue until T(n)=T(0)+nT(n) = T(0) + n
  • Final Solution:
    • Since T(0)=1T(0) = 1, T(n)=1+nT(n) = 1 + n.
    • This simplifies to T(n)=n+1T(n) = n + 1, 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 T(n)=T(n−1)+1T(n) = T(n-1) + 1 effectively represents the linear complexity of the recursive function, highlighting an important aspect of algorithm analysis.

Browse the library