Added GA3 EX3
This commit is contained in:
parent
92e23c9507
commit
3876eb5208
2 changed files with 35 additions and 4 deletions
BIN
GA3/ga3.pdf
BIN
GA3/ga3.pdf
Binary file not shown.
39
GA3/ga3.tex
39
GA3/ga3.tex
|
@ -359,12 +359,43 @@ Insert 2:
|
||||||
|
|
||||||
\subsection{Point B}
|
\subsection{Point B}
|
||||||
|
|
||||||
A red-black tree of n distinct elements has an height between $log(n)$
|
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
|
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
|
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
|
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).
|
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
|
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}
|
\end{document}
|
Reference in a new issue