% vim: set ts=2 sw=2 tw=80 et: \documentclass[12pt]{article} \usepackage[margin=3cm]{geometry} \usepackage{xcolor} \usepackage{lmodern} \usepackage{listings} \title{Graded Assignment 3 -- DSA} \author{Claudio Maggioni} % listings configuration \lstset{ basicstyle=\small\ttfamily, frame=shadowbox, rulesepcolor=\color{black}, columns=fullflexible, commentstyle=\color{gray}, keywordstyle=\bfseries, keywords={,while,if,elif,else,FUNCTION,return,for,from,to,TRUE,FALSE}, mathescape=true, aboveskip=2em, captionpos=b, abovecaptionskip=1em, belowcaptionskip=1em } \begin{document} \maketitle \tableofcontents \lstlistoflistings \newpage \section{Exercise 1} {\small\ttfamily \begin{verbatim} 12 Insert 6: 12 / 6 Insert 1: 12 / 6 / 1 Insert 7: 12 /// 6 / \ 1 7 Insert 4: 12 /// 6 /// \ 1 7 \ 4 Insert 11: 12 ////// 6 /// \ 1 7 \ \ 4 11 Insert 15: 12 ////// \ 6 15 /// \ 1 7 \ \ 4 11 Insert 9: 12 //////// \ 6 15 /// \ 1 7 \ \\\ 4 11 / 9 Insert 5: 12 //////// \ 6 15 ///// \ 1 7 \ \\\ 4 11 \ / 5 9 Insert 13: 12 //////// \\\\ 6 15 ///// \ / 1 7 13 \ \\\ 4 11 \ / 5 9 Insert 8: 12 ////////// \\\\ 6 15 ///// \ / 1 7 13 \ \\\\\ 4 11 \ / 5 9 / 8 Insert 14: 12 ////////// \\\\\\\ 6 15 ///// \ //// 1 7 13 \ \\\\\ \ 4 11 14 \ / 5 9 / 8 Insert 3: 12 ////////// \\\\\\\ 6 15 /////// \ //// 1 7 13 \\\ \\\\\ \ 4 11 14 / \ / 3 5 9 / 8 Insert 10: 12 ///////////// \\\\\\\ 6 15 /////// \ //// 1 7 13 \\\ \\\\\\\\ \ 4 11 14 / \ //// 3 5 9 / \ 8 10 Insert 2: 12 ///////////// \\\\\\\ 6 15 ///////// \ //// 1 7 13 \\\\\ \\\\\\\\ \ 4 11 14 / \ //// 3 5 9 / / \ 2 8 10 Delete 6: 12 /////////// \\\\\\\ 7 15 ///////// \\\\\\\\ //// 1 11 13 \\\\\ //// \ 4 9 14 / \ / \ 3 5 8 10 / 2 Delete 2: 12 /////////// \\\\\\\ 7 15 /////// \\\\\\\\ //// 1 11 13 \\\ //// \ 4 9 14 / \ / \ 3 5 8 10 Delete 12: 15 //// 13 /////////// \ 7 14 /////// \\\\\\\\ 1 11 \\\ //// 4 9 / \ / \ 3 5 8 10 \end{verbatim} } \section{Exercise 2} \subsection{Point A} {\small\ttfamily \begin{verbatim} 12B Insert 6: 12B / 6R Insert 1: 6B / \ 1R 12R Insert 7: 6B / \\\\ 1B 12B / 7R Insert 4: 6B //// \\\\ 1B 12B \ / 4R 7R Insert 11: 6B //// \\\\ 1B 11B \ / \ 4R 7R 12R Insert 15: 11B //// \ 6R 12B //// \ \ 1B 7B 15R \ 4R Insert 9: 11B /////// \ 6R 12B //// \ \ 1B 7B 15R \ \ 4R 9R Insert 5: 6B //// \\\\\\\ 4B 11R / \ //// \ 1R 5R 7B 12B \ \ 9R 15R Insert 13: 11B /////// \\\\\ 6R 13B //// \ / \ 4B 7B 12R 15R / \ \ 1R 5R 9R Insert 8: 8B //// \\\\ 6R 11R //// \ / \\\\\ 4B 7R 9R 13B / \ / \ 1R 5R 12R 15R Insert 14: 8B //// \\\\ 6B 11B //// \ / \\\\\ 4B 7R 9B 13B / \ / \\\\\ 1R 5R 12B 15B / 14R Insert 3: 6B //// \\\\ 4B 8R //// \ / \\\\ 1B 5B 7B 11B \ / \\\\\ 3R 9B 13B / \\\\\ 12B 15B / 14R Insert 10: 6B //// \\\\ 4B 8R //// \ / \\\\\\\\ 1B 5B 7B 11B \ ///// \\\\\ 3R 9B 13B \ / \\\\\ 10R 12B 15B / 14R Insert 2: 6B //// \\\\ 4B 8R //// \ / \\\\\\\\ 2B 5B 7B 11B / \ ///// \\\\\ 1R 3R 9B 13B \ / \\\\\ 10R 12B 15B / 14R \end{verbatim} } \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 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)$. \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}