diff --git a/GA3/ga3.pdf b/GA3/ga3.pdf index b8b4373..a4c5900 100644 Binary files a/GA3/ga3.pdf and b/GA3/ga3.pdf differ diff --git a/GA3/ga3.tex b/GA3/ga3.tex index 1ce79de..3ea7d56 100644 --- a/GA3/ga3.tex +++ b/GA3/ga3.tex @@ -359,12 +359,43 @@ Insert 2: \subsection{Point B} -A red-black tree of n distinct elements has an height between $log(n)$ -and $2 log(n)$ thanks to the red-black tree invariant. The worst-case insertion -complexity is $log(n)$ since finding the right place to insert is as complex as +A red-black tree of n distinct elements has an height between $\log(n)$ +and $2\log(n)$ thanks to the red-black tree invariant. The worst-case insertion +complexity is $\log(n)$ since finding the right place to insert is as complex as a regular tree (i.e. logarithmic) and the ``fixup'' operation is logarithmic as well (the tree traversal is logarithmic while operations in each iteration are constant). In asymptotic terms, the uneven height of leaves in the tree does not make a difference -since it is a constant factor (since the maximum height is $2 log(n)$ and the minimum one is $log(n)$. +since it is a constant factor (since the maximum height is $2\log(n)$ and the minimum one is $\log(n)$. + +\section{Exercise 3} + +\begin{lstlisting}[caption=Solution for exercise 3, label={lst:ex3}] +FUNCTION JOIN-INTERVALS(X) + if X.length == 0: + return + + for i from 1 to X.length: + if i % 2 == 1: + X[i] $\gets$ (X[i], 1) + else: + X[i] $\gets$ (X[i], -1) + + $\textit{sort X in place by 1st tuple element in O(n log(n))}$ + + c $\gets$ 1 + n $\gets$ 0 + start $\gets$ X[i][1] + for i from 2 to X.length: + c $\gets$ c + X[i][2] + if c == 0: + X[2 * n + 1] $\gets$ start + X[2 * n + 2] $\gets$ X[i][1] + start $\gets$ X[i+1][1] + n $\gets$ n + 1 + X.length $\gets$ 2 * n +\end{lstlisting} +\\ +The complexity of this algorithm is $O(n\log(n))$ since the sorting is in $\Theta(n\log(n))$ and the +union operation afterwards is $\Theta(n)$. \end{document} \ No newline at end of file