2

I have a recursive algorithm like that which computes the smallest integer in the array.

 ALGORITHM F_min1(A[0..n-1])
//Input: An array A[0..n-1] of real numbers
If n = 1
return A[0]
else
temp ← F_minl(A[0..n-2])
If temp ≤ A[n-1]
return temp
else
return A[n-1]

What I think that the reccurence relation should be

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

However I am not sure about the + n part. I want to be sure in which cases the recurrence is T(n)=T(n-1) + 1 and in which cases the recurrence is T(n)=T(n-1) + n.

1
  • 1
    Its T(n)=T(n-1) + 1 so it's Theta(n) globally. At least, at first glance. Where "+1" is an umbrella term to indicate a finite set of constant time operations. Commented Feb 24, 2017 at 10:32

1 Answer 1

3

The recurrence should be

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

because besides the recursive call to the smaller array, all computational effort (reading the last entry of A and doing the comparison) take constant time in unit cost measure. The algorithm can be understood as divide-and-conquer where the divide part is splitting the array into a prefix and the last entry; the conquer part, which is a comparison, cannot take more than constant time here. In total, a case where there is linear work after the recursive call does not exist.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much, what I tought is that after the recursion there will be n comparions so o(n).
@ayt_cem Indeed, there are n comparisons in total; however, there is only one comparison in each recursive call.
So in order to be + o(n) in each comparison after the recursive call, there should be n comparisons ?
Yes, but there is only one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.