\documentclass{beamer} %include lhs2TeX.fmt \author{Piyush P Kurur\\ Office no: 224\\ Dept. of Comp. Sci. and Engg.\\ IIT Kanpur} \newcommand{\Token}[2][]{\node(#2#1)[token]{#2};} \newcommand{\Symbol}[2][]{\node(#2#1)[symbol]{#2};} \newcommand{\Point}[1]{\node(#1)[point]{#1};} \usepackage{tikz} \usetikzlibrary{positioning,shapes,chains,fit} \usetikzlibrary{shapes.symbols} \usetikzlibrary{matrix} \usetikzlibrary{backgrounds} \usetikzlibrary{shapes.geometric} \usepackage{multicol} \usepackage{algorithm2e} \usepackage{pgfkeys} \pgfkeys {% /tikz/redbox/.style={% shape=rectangle,% minimum size=1.5em,% top color = white, % bottom color=red!50!black!50, inner sep=0pt, draw=black }, /tikz/greybox/.style={ shape=rectangle,% minimum width=7em,% minimum height=1.5em, fill=black!20, inner sep=0pt, draw=black }, /tikz/blackbox/.style={ shape=rectangle,% minimum width=7em,% minimum height=1.5em, fill=black, inner sep=0pt, draw=black }, /tikz/whitebox/.style={ shape=rectangle, minimum width=7em, minimum height=1.5em, draw=black }, /tikz/bluebox/.style={ shape=rectangle, minimum width=7em, minimum height=1.5em, fill=blue!40, draw=black }, /tikz/whitetape/.style={ shape=rectangle, minimum width=4em, minimum height=1.5em, draw=black }, /tikz/bluearr/.style={ ->, line width=0.25em, draw=blue!40 }, /tikz/greenarr/.style={ ->, line width=0.25em, draw=green!40!black!60 } } \newcommand{\arraythree}[4]{ \begin{tikzpicture}[node distance=0] \pgfsetstrokeopacity{#4}; \node[#1](a0#3){#2}; \node[#1, right=of a0#3](a1#3){#2}; \node[#1, right=of a1#3](a1#3){#2}; \end{tikzpicture} } \newcommand{\arraytwo}[4]{ \begin{tikzpicture}[node distance=0] \pgfsetstrokeopacity{#4}; \node[#1](a0#3){#2}; \node[#1,right=of a0#3]{#2}; \end{tikzpicture} } \newcommand{\Array}[0]{ \arraythree{box}{}{a}{1} } \newcommand{\AArray}[0]{% \arraythree{bbox}{\Array}{b}{0.5}} \newcommand{\AAArray}[0]{% \arraytwo{bbbox}{\AArray}{c}{0.25}} \newcommand{\T}[1]{\ensuremath{T\left(#1\right)}} \newcommand{\BigOh}[1]{\ensuremath{O\left(#1\right)}} \title{Fundamentals of Computing: Lecture 21} \date{September 16, 2009} \begin{document} \begin{frame} \maketitle \end{frame} \begin{frame} \frametitle{Merge sort} \begin{itemize} \item In the last class we saw quick sort. \item This class we will see another algorithm called merge sort. \end{itemize} \end{frame} \begin{frame} \frametitle{Merging sorted lists} Given two sorted lists $\{a_1,\ldots, a_n\}$ and $\{b_1,\ldots, b_m\}$ how fast can we merge ? \pause \begin{code} void mergearrays(int a[], int b[], int c[], int n, int m) { int i=0,j=0,k=0; while( i < n && j < m) { if(a[i] < b[j]) { c[k] = a[i]; i++} else if(a[i] >= b[j]){ c[k] = b[j]; j++} k++; } while(i < n){ c[k] = a[i]; i++; k++;} while(j < m){ c[k] = b[j]; j++; k++;} } \end{code} \end{frame} \begin{frame} \frametitle{Main idea} \begin{itemize} \item If the array is of size 1 then it is already sorted. \item Otherwise divide the array into two equal parts say $A$ and $B$ \item Sort $A$ and $B$ recursively. \item Merge $A$ and $B$ to get the sorted list. \end{itemize} \pause \begin{block}{How many comparisons} \[ \T{n} = \T{\frac{n}{2}} + \T{\frac{n}{2}} + n = 2 \T{\frac{n}{2}} + n. \] \end{block} \end{frame} \begin{frame} \frametitle{Unrolling the recursion} \begin{eqnarray*} \T{n} & = & 2 \T{\frac{n}{2}} + n \\ & = & 2 \left(2 \T{\frac{n}{4}} + \frac{n}{2}\right) + n \\ & = & 2^2 \T{ \frac{n}{2^2} } + (n + n)\\ \ldots & \ldots & \ldots \\ & = & 2^k \T{\frac{n}{2^k}} + \overbrace{n + \ldots + n}^{k\textrm{ times}} \end{eqnarray*} Choosing $k = \log{n}$ we have \[ \T{n} = 2^{\log{n}} \T{1} + n \log{n} = \BigOh{n \log{n}}.\] \end{frame} \begin{frame} \begin{tikzpicture} \frametitle{Merging array slices} \begin{scope}[node distance=0]; \node[whitebox](a0){}; \node[bluebox, right=of a0](a1){}; \node[bluebox, right=of a1](a2){}; \node[whitebox, right=of a2](a3){}; \end{scope} \only<-2>{\node[greybox, minimum width=14em, below=of a2.west](b0){};} \only<3>{\node[bluebox, minimum width=14em, below=of a2.west](b0){};} \begin{scope}[node distance=0] \node[whitebox, left=of b0](b1){}; \node[whitebox, right=of b0](b2){}; \end{scope} \node[above=of a1.north west, label=above:$\texttt{src}$, redbox](src){$\cdot$}; \node[above=of a2.north east, label=above:$\texttt{src + mid}$, redbox](srcmid){$\cdot$}; \draw[->, thick] (src.mid) to [out = 0, in = 90] (a1.north west); \draw[->, thick] (srcmid.mid) to [out = 180, in = 90] (a2.north west); \node[above] at (a1.north east)(mid){$\texttt{mid}$} ; \node[above] at(a2.north east)(mid){$\texttt{n}$}; \only<2>{ \draw[bluearr](a1)--(b0.north -|| a1); \draw[bluearr](a2)--(b0.north -|| a2); \draw[bluearr](a2)--(b0.north -|| a1); \draw[bluearr](a1)--(b0.north -|| a2); \node[above] at(b0){{\Large merge}}; } \end{tikzpicture} \end{frame} \begin{frame} \frametitle{Merging array slices in |C|} \begin{code} int merge(int *src, int mid, int n, int *dest) { int i=0, j = 0, k = 0; int *a = src; int *b = src + mid; while( i < mid && j < n - mid) { if(a[i] < b[j]) { dest[k] = a[i]; i++} else if(a[i] >= b[j]){ dest[k] = b[j]; j++} k++; } while(i < mid) { dest[k] = a[i]; i++; k++;} while(j < n - mid) { dest[k] = b[j]; j++; k++;} } \end{code} \end{frame} \begin{frame} \frametitle{Merge-sorting array using an auxilary array} \begin{tikzpicture} \begin{scope}[node distance=0]; \node[whitebox](a0){}; \only<1>{ \node[greybox, minimum width=14em, right=of a0](a2) {$\texttt{n}$ elements}; } \only<2-9> { \node[greybox, right=of a0](a1){$\texttt{mid}$}; \node[greybox, right=of a1](a2){$\texttt{n-mid}$}; } \only<10->{ \node[bluebox, minimum width=14em, right=of a0](a2) {$\texttt{n}$ elements}; } \node[whitebox, right=of a2](a3){}; \end{scope} \node[whitebox, below=of a0](b0){}; \begin{scope}[node distance=0]; \only<1-2>{ \node[blackbox, minimum width=14em, right=of b0](b2){$\texttt{n}$ elements}; } \only<3> { \node[greybox, right=of b0](b1){$\texttt{mid}$}; \node[greybox, right=of b1](b2){$\texttt{n-mid}$}; } \only<4-9>{\node[blackbox] at (a1){};} \only<4>{ \node[greybox, right=of b0](b1){sort}; \node[whitebox, right=of b1](b2){}; } \only<5>{ \node[bluebox, right=of b0](b1){$\texttt{mid}$}; \node[whitebox, right=of b1](b2){}; } \only<6-9>{\node[blackbox] at (a2){};} \only<6>{ \node[whitebox, right=of b0](b1){}; \node[greybox, right=of b1](b2){sort}; } \only<7>{ \node[whitebox, right=of b0](b1){}; \node[bluebox, right=of b1](b2){$\texttt{n-mid}$}; } \only<8->{ \node[bluebox, right=of b0](b1){$\texttt{mid}$}; \node[bluebox, right=of b1](b2){$\texttt{n-mid}$}; } \node[whitebox, right=of b2](b3){}; \end{scope} \node[above=of a0.north east, redbox, label=above:$\texttt{src}$] (a){$\cdot$}; \node[below=of b0.south east, redbox, label=below:$\texttt{aux}$] (b){$\cdot$}; \draw[->, thick] (a) to [out = 0, in = 90] (a0.north east); \draw[->, thick] (b) to [out = 0, in = 270] (b0.south east); \only<3>{ \node[above] at (b1.north east){copy}; \draw[greenarr](a1)--(b1); \draw[greenarr](a2)--(b2); } \only<9>{ \draw[bluearr](b1)--(a1); \draw[bluearr](b1)--(a2); \draw[bluearr](b2)--(a1); \draw[bluearr](b2)--(a2); \node[below] at (a1.south east){merge}; } \end{tikzpicture} \end{frame} \begin{frame} \frametitle{Merge sorting in |C|} \begin{code} void mergeSort( int *ptr, int n, int *aux) { if(n <= 1) return; mid = n /2; copy(ptr, mid, aux); copy(ptr + mid, n-mid, aux + mid); mergeSort(aux,mid, ptr); mergeSort(aux + mid, n - mid, ptr + mid); merge(aux, mid, n, a); } \end{code} \end{frame} \end{document} \begin{scope}[node distance=0, every node/.style={draw}] \node (a0){3}; \node [right =of a0] (a1){8} \node [right=of a1] (a2) {9} \node [right=of a2] (a3) {10} \end{scope} \begin{tikzpicture} \only<2>{ \begin{scope}[node distance=0]; \node[whitebox](a0){}; \node[greybox, right=of a0](a1){$\texttt{mid}$}; \node[greybox, right=of a1](a2){$\texttt{n-mid}$}; \node[whitebox, right=of a2](a3){}; \end{scope} \node[whitebox, below=of a0](b0){}; \begin{scope}[node distance=0]; \node[blackbox, minimum width=14em, right=of b0](b1){$\texttt{n}$ elements}; \node[whitebox, right=of b1](b2){}; \end{scope} \node[above=of a1.north west, redbox, label=above:$\texttt{src}$] (a){$\cdot$}; \node[below=of b1.south west, redbox, label=below:$\texttt{aux}$] (b){$\cdot$}; \draw[->, thick] (a) to [out = 0, in = 90] (a1.north west); \draw[->, thick] (b) to [out = 0, in = 270] (b1.south west); \end{tikzpicture} \only<3>{ \begin{tikzpicture} \begin{scope}[node distance=0]; \node[whitebox](a0){}; \node[greybox, right=of a0](a1){$\ldots$}; \node[greybox, right=of a1](a2){}; \node[whitebox](a3){}; \end{scope} \node[greybox, minimum width=14em, below=of a2.west](a3){}; \node[above=of a1.north west, label=above:$\texttt{src}$, redbox](src){$\cdot$}; \node[above=of a2.north west, label=above:$\texttt{src + mid}$, redbox](srcmid){$\cdot$}; \draw[->, thick] (src.mid) to [out = 0, in = 90] (a1.north west); \draw[->, thick] (srcmid.mid) to [out = 0, in = 90] (a2.north west); \node[above] at (a1.north east)(mid){$\texttt{mid}$} ; \node[above] at(a2.north east)(mid){$\texttt{n}$}; \end{tikzpicture} }