Back to the 2024 paper

Module 1: Introduction

20247m

Suppose the number of basic operations in an algorithm is defined as: T(n)=T(n−1)+nT(n) = T(n-1) + n, with T(1)=1T(1) = 1.
Solve this recurrence using the substitution method and determine the time complexity.

Worked SolutionAI Assisted

T(n)=T(n−1)+nT(n)=T(n-1)+n, T(1)=1T(1)=1 — Solve by Iteration / Substitution

Iterative Unfolding:
T(n)=T(n−1)+n=[T(n−2)+(n−1)]+n=T(n−2)+(n−1)+n=[T(n−3)+(n−2)]+(n−1)+n  ⋮=T(1)+2+3+⋯+n=1+∑k=2nk=∑k=1nk=n(n+1)2 \begin{aligned} T(n) &= T(n-1)+n \\ &= [T(n-2)+(n-1)]+n = T(n-2)+(n-1)+n \\ &= [T(n-3)+(n-2)]+(n-1)+n \\ &\;\vdots \\ &= T(1) + 2+3+\dots+n \\ &= 1 + \sum_{k=2}^{n} k = \sum_{k=1}^{n} k = \frac{n(n+1)}{2} \end{aligned}

Substitution Proof: Guess T(n)=n(n+1)/2T(n)=n(n+1)/2.

  • Base n=1n=1: 1⋅2/2=11\cdot2/2=1 ✓
  • Step: assume T(n−1)=(n−1)n/2T(n-1)=(n-1)n/2 → T(n)=(n−1)n/2+n=n(n+1)/2T(n)=(n-1)n/2 + n = n(n+1)/2 ✓

Asymptotics:
T(n)=n22+n2=Θ(n2)=O(n2),Ω(n2)T(n)=\frac{n^2}{2}+\frac{n}{2}=\Theta(n^2) = O(n^2), \Omega(n^2)

Application: Sum of first nn naturals; time of naive incremental algorithms (e.g., insertion sort worst-case without divide).

Similar questions

Free AppAndroid App

Coursify Android App

Generate AI courses, take quick quizzes between classes, and master complex engineering concepts on the go.