Time Complexity of CLRS Optimal Parenthesis Algorithm

asymptoticscomputer sciencerecursive algorithms

I am reading the Introduction to Algorithms CLRS book, and I am unsure about the time complexity of one of the algorithms that is a recursive algorithm that calls itself twice. This chain matrix multiplication algorithm has an accompany algorithm below it which is what I would like to find an upper bound for.

This one below is the one I would like to find the upper bound. It displays the optimal chain matrix multiplication with parenthesis:

I would like to know the upper bound time compexity of PRINT-OPTIMAL-PARENS where the input $n$ signifies the number of matrices given. I drew out a recursion tree for $5$ matrices and noticed it looks similar to divide and conquer where the first $3$ levels are full and the last $4$th level has $2$ more calls, giving a total of $9$ calls. But the number of calls can't be $O(n \log_2 n)$ like a divide and conquer merge sort since each call does $O(1)$ work and the problem isn't being divided. Which made me think that the function has no possible way going above the number of elements of $s$, $|s|$, where I could use that as the upper bound.

$$
|s| = O\left({n \choose 2}\right) = O\left(\frac{n(n-1)}{2}\right) = O(n^2)
$$

Is this correct? It looks wrong to me since this algorithm doesn't look $O(n^2)$.

Best Answer

Each call to Print() prints either one matrix name, or pair of parentheses. In output there are $n$ matrix names and $n - 1$ pairs of parentheses. Therefore there will be at most $2n - 1$ calls to Print().