\documentclass{beamer}
%
%
\makeatletter
\@ifundefined{lhs2tex.lhs2tex.sty.read}%
  {\@namedef{lhs2tex.lhs2tex.sty.read}{}%
   \newcommand\SkipToFmtEnd{}%
   \newcommand\EndFmtInput{}%
   \long\def\SkipToFmtEnd#1\EndFmtInput{}%
  }\SkipToFmtEnd

\newcommand\ReadOnlyOnce[1]{\@ifundefined{#1}{\@namedef{#1}{}}\SkipToFmtEnd}
\usepackage{amstext}
\usepackage{amssymb}
\usepackage{stmaryrd}
\DeclareFontFamily{OT1}{cmtex}{}
\DeclareFontShape{OT1}{cmtex}{m}{n}
  {<5><6><7><8>cmtex8
   <9>cmtex9
   <10><10.95><12><14.4><17.28><20.74><24.88>cmtex10}{}
\DeclareFontShape{OT1}{cmtex}{m}{it}
  {<-> ssub * cmtt/m/it}{}
\newcommand{\texfamily}{\fontfamily{cmtex}\selectfont}
\DeclareFontShape{OT1}{cmtt}{bx}{n}
  {<5><6><7><8>cmtt8
   <9>cmbtt9
   <10><10.95><12><14.4><17.28><20.74><24.88>cmbtt10}{}
\DeclareFontShape{OT1}{cmtex}{bx}{n}
  {<-> ssub * cmtt/bx/n}{}
\newcommand{\tex}[1]{\text{\texfamily#1}}	% NEU

\newcommand{\Sp}{\hskip.33334em\relax}
\newcommand{\NB}{\textbf{NB}}
\newcommand{\Todo}[1]{$\langle$\textbf{To do:}~#1$\rangle$}

\EndFmtInput
\makeatother
%
\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{tabbing}\tt
~void~mergearrays\char40{}int~a\char91{}\char93{}\char44{}~int~b\char91{}\char93{}\char44{}~int~c\char91{}\char93{}\char44{}~int~n\char44{}~int~m\char41{}\\
\tt ~\char123{}\\
\tt ~~~int~i\char61{}0\char44{}j\char61{}0\char44{}k\char61{}0\char59{}\\
\tt ~~~\\
\tt ~~~while\char40{}~i~\char60{}~n~\char38{}\char38{}~j~\char60{}~m\char41{}\\
\tt ~~~\char123{}\\
\tt ~~~~~if\char40{}a\char91{}i\char93{}~\char60{}~b\char91{}j\char93{}\char41{}~\char123{}~c\char91{}k\char93{}~\char61{}~a\char91{}i\char93{}\char59{}~i\char43{}\char43{}\char125{}\\
\tt ~~~~~else~if\char40{}a\char91{}i\char93{}~\char62{}\char61{}~b\char91{}j\char93{}\char41{}\char123{}~c\char91{}k\char93{}~\char61{}~b\char91{}j\char93{}\char59{}~j\char43{}\char43{}\char125{}\\
\tt ~~~~~k\char43{}\char43{}\char59{}\\
\tt ~~~\char125{}\\
\tt ~~~\\
\tt ~~~while\char40{}i~\char60{}~n\char41{}\char123{}~c\char91{}k\char93{}~\char61{}~a\char91{}i\char93{}\char59{}~i\char43{}\char43{}\char59{}~k\char43{}\char43{}\char59{}\char125{}\\
\tt ~~~while\char40{}j~\char60{}~m\char41{}\char123{}~c\char91{}k\char93{}~\char61{}~b\char91{}j\char93{}\char59{}~j\char43{}\char43{}\char59{}~k\char43{}\char43{}\char59{}\char125{}\\
\tt ~\char125{}
\end{tabbing}
\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 \text{\tt C}}
  \begin{tabbing}\tt
~int~merge\char40{}int~\char42{}src\char44{}~int~mid\char44{}~int~n\char44{}~int~\char42{}dest\char41{}\\
\tt ~\char123{}\\
\tt ~~~int~i\char61{}0\char44{}~j~\char61{}~0\char44{}~k~\char61{}~0\char59{}\\
\tt ~\\
\tt ~~~int~\char42{}a~\char61{}~src\char59{}\\
\tt ~~~int~\char42{}b~\char61{}~src~\char43{}~mid\char59{}\\
\tt ~~~while\char40{}~i~\char60{}~mid~\char38{}\char38{}~j~\char60{}~n~\char45{}~mid\char41{}\\
\tt ~~~\char123{}\\
\tt ~~~~~if\char40{}a\char91{}i\char93{}~\char60{}~b\char91{}j\char93{}\char41{}~\char123{}~dest\char91{}k\char93{}~\char61{}~a\char91{}i\char93{}\char59{}~i\char43{}\char43{}\char125{}\\
\tt ~~~~~else~if\char40{}a\char91{}i\char93{}~\char62{}\char61{}~b\char91{}j\char93{}\char41{}\char123{}~dest\char91{}k\char93{}~\char61{}~b\char91{}j\char93{}\char59{}~j\char43{}\char43{}\char125{}\\
\tt ~~~~~k\char43{}\char43{}\char59{}\\
\tt ~~~\char125{}\\
\tt ~~~while\char40{}i~\char60{}~mid\char41{}~~~~~~\char123{}~dest\char91{}k\char93{}~\char61{}~a\char91{}i\char93{}\char59{}~i\char43{}\char43{}\char59{}~k\char43{}\char43{}\char59{}\char125{}\\
\tt ~~~while\char40{}j~\char60{}~n~\char45{}~mid\char41{}~~\char123{}~dest\char91{}k\char93{}~\char61{}~b\char91{}j\char93{}\char59{}~j\char43{}\char43{}\char59{}~k\char43{}\char43{}\char59{}\char125{}\\
\tt ~\\
\tt ~\char125{}
\end{tabbing}

\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 \text{\tt C}}
  \begin{tabbing}\tt
~void~mergeSort\char40{}~int~\char42{}ptr\char44{}~int~n\char44{}~int~\char42{}aux\char41{}\\
\tt ~\char123{}\\
\tt ~~~if\char40{}n~\char60{}\char61{}~1\char41{}~return\char59{}\\
\tt ~~~mid~\char61{}~n~\char47{}2\char59{}\\
\tt ~~~copy\char40{}ptr\char44{}~mid\char44{}~aux\char41{}\char59{}\\
\tt ~~~copy\char40{}ptr~\char43{}~mid\char44{}~n\char45{}mid\char44{}~aux~\char43{}~mid\char41{}\char59{}\\
\tt ~~~mergeSort\char40{}aux\char44{}mid\char44{}~ptr\char41{}\char59{}\\
\tt ~~~mergeSort\char40{}aux~\char43{}~mid\char44{}~n~\char45{}~mid\char44{}~ptr~\char43{}~mid\char41{}\char59{}\\
\tt ~~~merge\char40{}aux\char44{}~mid\char44{}~n\char44{}~a\char41{}\char59{}\\
\tt ~\char125{}
\end{tabbing}

\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}
  }
