2
$\begingroup$

I'm reading Algorithm Design and Applications, by Michael T. Goodrich and Roberto Tamassia, published by Wiley. They teach the concept of primitive operations and how to count then in a given algorithm. Everything was clear to me until the moment they showed a recursive function (a simple recursive way to calculate the maximum value of an array) and its primitive operation count.

The function (in pseudo-code) is this:

Algorithm recursiveMax(A, n):
    Input: An array A storing n ≥ 1 integers.
    Output: The maximum element in A.
    
    if n = 1 then
        return A[0]
    return max{recursiveMax(A, n − 1), A[n − 1]}

where A is an array and n its length. The author states what follows concerning how we calculate the number of primitive operations this function has:

As with this example, recursive algorithms are often quite elegant. Analyzing the running time of a recursive algorithm takes a bit of additional work, however. In particular, to analyze such a running time, we use a recurrence equation, which defines mathematical statements that the running time of a recursive algorithm must satisfy. We introduce a function T (n) that denotes the running time of the algorithm on an input of size n, and we write equations that T (n) must satisfy. For example, we can characterize the running time, T (n), of the recursiveMax algorithm as T(n) = 3 if n = 1 or T(n - 1) + 7 otherwise, assuming that we count each comparison, array reference, recursive call, max calculation, or return as a single primitive operation. Ideally, we would like to characterize a recurrence equation like that above in closed form, where no references to the function T appear on the righthand side. For the recursiveMax algorithm, it isn’t too hard to see that a closed form would be T (n) = 7(n − 1) + 3 = 7n − 4.

I can clearly understand that in the case of a single item array, our T(n) would be just 3 (only 3 primitive operations will occur, i.e. the comparision n = 1, the array index A[0] and the return operation), but I cannot understand why in the case where n is not 1 we have T(n-1) + 7. Why + 7? From where did we get this constant?

Also, I cannot comprehend this closed form: how did he get that T(n) = 7(n - 1) + 3?

I appreciate any help.

$\endgroup$
1
  • 1
    $\begingroup$ Honestly, this constant doesn't matter. We almost never care for constants when we compute the running time. $\endgroup$
    – nir shahar
    Commented Jun 20, 2021 at 23:52

1 Answer 1

0
$\begingroup$

For $n\geq 2$, I believe that the $T(n) = T(n-1) + 7$ because of the comaparison operation $n = 1$, returning $\max$, computing $\max$, calling recursiveMax, computing recursiveMax, i.e., $T(n-1)$ term, computing $n-1$ twice, accesing $A[n-1]$.

However, the constant should not matter, since we are interested in asymptotic running time analysis, as nir shahar is saying.

Now, we have $T(n) = T(n-1) + 7$ for $n\geq 2$. You can expand this term, as follows:

\begin{align} T(n) &= T(n-2) + 2\cdot7 \\ &= T(n-3) + 3 \cdot 7 \\ &= T(n-4) + 4 \cdot 7 \\ &=... \\ &=T(1) + 7 \cdot (n-1) \\ &= 3 + 7 \cdot (n-1) \end{align}

This answers your second question.

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.