Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy: Solve T(n)=T(n-1)+n by Iteration Method

Can someone please help me with this ?

Use iteration method to solve it. T(n) = T(n-1) +n

Explanation of steps would be greatly appreciated.

like image 591
blackvitriol Avatar asked Dec 02 '12 22:12

blackvitriol


People also ask

How do you solve a recurrence relationship using iteration?

Iteration Method. The iteration method is a "brute force" method of solving a recurrence relation. The general idea is to iteratively substitute the value of the recurrent part of the equation until a pattern (usually a summation) is noticed, at which point the summation can be used to evaluate the recurrence.

What is the time complexity of T n )= t n 1 )+ n?

It is O(n^2) .

What is the solution to the recurrence t'n't n 2 )+ n?

this is a Geometric series with log(n) terms and each term gets halved. Hence answer is O(N).


2 Answers

T(n) = T(n-1) + n

T(n-1) = T(n-2) + n-1

T(n-2) = T(n-3) + n-2

and so on you can substitute the value of T(n-1) and T(n-2) in T(n) to get a general idea of the pattern.

T(n) = T(n-2) + n-1 + n

T(n) = T(n-3) + n-2 + n-1 + n
.
.
.

T(n) = T(n-k) + kn - k(k-1)/2    ...(1)

For base case:

n - k = 1 so we can get T(1)

=> k = n - 1
substitute in (1)

  T(n) = T(1) + (n-1)n - (n-1)(n-2)/2

Which you can see is of Order n2 => O(n2).

like image 189
Fyre Avatar answered Oct 09 '22 02:10

Fyre


Expand it!

T(n) = T(n-1) + n = T(n-2) + (n-1) + n = T(n-3) + (n-2) + (n-1) + n

and so on, until

T(n) = 1 + 2 + ... + n = n(n+1)/2   [= O(n^2)]

provided that T(1) = 1

like image 45
Haile Avatar answered Oct 09 '22 00:10

Haile