Recurrence Relations
Mathematical Induction
Problem Solving
Algorithms
Computational Complexity

How to solve Tn Tn - 1 n

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

In the realm of algorithm analysis, recurrence relations often emerge as tools to describe the performance of recursive algorithms. The recurrence relation T(n)=T(n1)+nT(n) = T(n - 1) + n, in particular, is a quintessential example of an arithmetic sequence in algorithm analysis often seen when evaluating simple recursive algorithms. This article will detail the solution for this recurrence using technical explanations and examples.

Understanding the Recurrence

The recurrence T(n)=T(n1)+nT(n) = T(n - 1) + n suggests that the value at T(n)T(n) depends linearly on its predecessor T(n1)T(n - 1) with an additional increment of nn. This is emblematic of an algorithm that adds a linearly increasing amount of work at each stage. Below is a step-by-step explanation to solve this recurrence with initial condition T(1)=1T(1) = 1:

Step-by-Step Solution

1. Setup of the Relation

Given the recurrence T(n)=T(n1)+nT(n) = T(n - 1) + n, we start with the base case T(1)=1T(1) = 1. The task is to express T(n)T(n) explicitly without recursion.

2. Unrolling the Recurrence

• Start from the recurrence and unwind it:

T(n)=T(n1)+n=(T(n2)+(n1))+n=((T(n3)+(n2))+(n1))+n=1+2++(n1)+n\begin{align*} T(n) & = T(n - 1) + n \\ & = (T(n - 2) + (n - 1)) + n \\ & = ((T(n - 3) + (n - 2)) + (n - 1)) + n \\ & \vdots \\ & = 1 + 2 + \ldots + (n - 1) + n \end{align*}

• This clearly shows that T(n)T(n) is equivalent to the sum of integers from 11 to nn.

3. Summation Formula

• The formula for the sum of the first nn natural numbers is:

T(n)=n(n+1)2T(n) = \frac{n(n + 1)}{2}

4. Verification

• Substitute n=1n = 1 into the formula to verify the base condition:

T(1)=1(1+1)2=1T(1) = \frac{1(1 + 1)}{2} = 1

• Consider n=2n = 2:

T(2)=2(2+1)2=3T(2) = \frac{2(2 + 1)}{2} = 3

• This matches the unrolled version as well: T(2)=1+2=3T(2) = 1 + 2 = 3

Evaluating Complex Scenarios

Given the simplicity, the solution enables quick calculations for larger nn. For example, for n=10n = 10, T(10)T(10) would be:

T(10)=10×112=55T(10) = \frac{10 \times 11}{2} = 55

Technical Explanation

Time Complexity Implications

The expression $T(n) = \frac\{n(n + 1)\}\{2\}$ is $O(n^2)$. This quadratic complexity arises because each call incurs work proportional to nn, leading to a cumulative work of the order n22\frac{n^2}{2}. As such, algorithms that follow this recurrence are not ideal for larger input sizes when optimum performance is critical.

Implementing in Code

Here is an example implementation of how this recurrence might be solved iteratively rather than recursively:


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.