math - Solving Recurrences in Loops -
i understand how solve recurrence relations when not involve additional looping i.e.:
int recursive_method(int n){ if(n == 1){ return 1; } constant statement; recursive_method(n-1); return n; }
my problem comes when trying solve recurrences inside of loops following:
int recursive_method(int n){ if(n == 1){ return 1; } for(int = 1; i<n; i++){ constant statement; recursive_method(n-1); } return n; }
when trying set recursive relation problem above
t(n) = 1 if n<2; sum(from i=1 n){t(n-1) + c} if n>=2
so in other words sum of costs 1 n? if not, how go thinking problem this?
the expression have t(n) in recursive case right, can simplified. in particular, notice work done in sum independent of summation index, can rewrite
sum i=1 n { t(n-1) + c }
as
nt(n - 1) + cn
so you'll end with
t(n) = 1 (if n ≤ 1)
t(n) = nt(n - 1) + cn (otherwise)
Comments
Post a Comment