Clarify number of calls

This commit is contained in:
Antti H S Laaksonen 2017-05-25 16:26:22 +03:00
parent 5e08fd3ded
commit 1e35801bf4
1 changed files with 9 additions and 5 deletions

View File

@ -169,15 +169,19 @@ void g(int n) {
\end{lstlisting} \end{lstlisting}
In this case each function call generates two other In this case each function call generates two other
calls, except for $n=1$. calls, except for $n=1$.
Hence, the call $\texttt{g}(n)$ causes the following calls: Let us see what happens when $g$ is called
with parameter $n$.
The following table shows the function calls
this single call yields:
\begin{center} \begin{center}
\begin{tabular}{rr} \begin{tabular}{rr}
parameter & number of calls \\ function call & number of calls \\
\hline \hline
$n$ & 1 \\ $g(n)$ & 1 \\
$n-1$ & 2 \\ $g(n-1)$ & 2 \\
$g(n-2)$ & 4 \\
$\cdots$ & $\cdots$ \\ $\cdots$ & $\cdots$ \\
$1$ & $2^{n-1}$ \\ $g(1)$ & $2^{n-1}$ \\
\end{tabular} \end{tabular}
\end{center} \end{center}
Based on this, the time complexity is Based on this, the time complexity is