Fixed GA3 again and again and again

This commit is contained in:
Claudio Maggioni 2019-05-14 08:57:03 +02:00
parent 59b66ac061
commit d593dc5e17
2 changed files with 21 additions and 14 deletions

Binary file not shown.

View file

@ -222,7 +222,8 @@ Delete 12:
\end{verbatim} \end{verbatim}
} }
The following printout was obtained by running the following BST implementation.with the following command: The following printout was obtained by running the following BST implementation.
with the following command:
\begin{verbatim} \begin{verbatim}
./tree.py 12 6 1 7 4 11 15 9 5 13 8 14 3 10 2 \| 6 2 12 ./tree.py 12 6 1 7 4 11 15 9 5 13 8 14 3 10 2 \| 6 2 12
@ -613,9 +614,9 @@ if __name__ == "__main__":
\end{lstlisting} \end{lstlisting}
\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)$ (as professor Carzaniga said in class) thanks to the red-black and $2\log(n)$ (as professor Carzaniga said in class) thanks to the red-black
tree invariant. The worst-case insertion complexity is $\log(n)$ since tree invariant. The worst-case insertion complexity is $\log(n)$ since
finding the right place to insert is as complex as 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).
@ -628,15 +629,15 @@ since it is a constant factor (since the maximum height is $2\log(n)$ and the mi
FUNCTION JOIN-INTERVALS(X) FUNCTION JOIN-INTERVALS(X)
if X.length == 0: if X.length == 0:
return return
for i from 1 to X.length: for i from 1 to X.length:
if i % 2 == 1: if i % 2 == 1:
X[i] $\gets$ (X[i], 1) X[i] $\gets$ (X[i], 1)
else: else:
X[i] $\gets$ (X[i], -1) X[i] $\gets$ (X[i], -1)
$\textit{sort X in place by 1st tuple element in O(n log(n))}$ $\textit{sort X in place by 1st tuple element in O(n log(n))}$
c $\gets$ 1 c $\gets$ 1
n $\gets$ 0 n $\gets$ 0
start $\gets$ X[1][1] start $\gets$ X[1][1]
@ -648,17 +649,23 @@ FUNCTION JOIN-INTERVALS(X)
if i < X.length: if i < X.length:
start $\gets$ X[i+1][1] start $\gets$ X[i+1][1]
n $\gets$ n + 1 n $\gets$ n + 1
X.length $\gets$ 2 * n X.length $\gets$ 2 * n
\end{lstlisting} \end{lstlisting}
% %
The complexity of this algorithm is $O(n\log(n))$ since the sorting is in $\Theta(n\log(n))$ and the The complexity of this algorithm is $O(n\log(n))$ since the sorting is in
union operation afterwards is $\Theta(n)$. $\Theta(n\log(n))$ and the union operation afterwards is $\Theta(n)$.
\section{Bonus} \section{Bonus}
The number of possible trees with $n$ nodes can be defined recursively. The number of trees with 0 or 1 node is clearly 1, since there is no freedom in arranging any remaining elements as children. For $n$ nodes, this number can be defined as the the sum, for each $x, y \in N_0$ s.t. $x + y = n - 1$, of the number of trees with $x$ nodes times the number of trees with $y$ nodes. We consider only $n - 1$ nodes since one node must be the root of the tree. The number of possible trees with $n$ nodes can be defined recursively. The
Using math notation, the number $T_n$ of trees with $n$ nodes can be expressed as: number of trees with 0 or 1 node is clearly 1, since there is no freedom in
arranging any remaining elements as children. For $n$ nodes, this number can be
defined as the the sum, for each $x, y \in N_0$ s.t. $x + y = n - 1$, of the
number of trees with $x$ nodes times the number of trees with $y$ nodes. We
consider only $n - 1$ nodes since one node must be the root of the tree.
Using math notation, the number $T_n$ of trees with $n$ nodes can be expressed
as:
$$T_n = 1 \hspace{1cm} n = 0 \lor n = 1$$ $$T_n = 1 \hspace{1cm} n = 0 \lor n = 1$$
$$T_n = \sum_{i = 0}^{n-1} C_i C_{n-1-i} \hspace{1cm} n \geq 2$$ $$T_n = \sum_{i = 0}^{n-1} T_i T_{n-1-i} \hspace{1cm} n \geq 2$$
\end{document} \end{document}