\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{algorithm2e}
\usepackage{pgfkeys}
\usepackage{multicol}
\pgfkeys %
    {%
      /tikz/coin/.style={
        rectangle,
        minimum height=2.5mm,
        minimum width=1cm,
        draw=black,
        fill=black!20,
        rounded corners
      },
      /tikz/towercolor/.style={
        fill=black!90
      },
      /tikz/towerbase/.style={
        trapezium,
        trapezium angle=45,
        trapezium stretches=true,
        towercolor,
        minimum width=7.5mm,
        minimum height=2.5mm,
      },
      /tikz/tower/.style={
        rectangle,
        rounded corners,
        towercolor,
        minimum width=2.5mm,
        minimum height=17.5mm,
      }
    }

\title{Fundamentals of Computing: Lecture 11}
\date{August 21, 2009}
\begin{document}
\begin{frame}
  \maketitle
\end{frame}

\begin{frame}
  \frametitle{Summary of the previous lecture}
  \begin{itemize}
  \item Variables declared outside all functions are global.
  \item Variables declared inside functions are local.
  \item The scope of the variables insider a block is from the
    start of the declartion to the end of the corresponding brace.
  \item Local variables hide global variables.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Designing Loops}
  \begin{block}{Loop invarient}
    A loop invarient is a condition that is true always at the begining of
    the loop.
  \end{block}
  The key idea in designing a loop is finding a good loop invarient.
\end{frame}

\begin{frame}
  \frametitle{Finding the smallest in a sequence of $n$ numbers}

  \[ s = \mathrm{min}\{ a[0],\ldots, a[n-1]\}. \]
  \pause
  \begin{block}{The invarient}
    \[ s = \mathrm{min}\{ a[0],\ldots, a[i-1]\}. \]
  \end{block}
  \pause
  \begin{tabbing}\tt
~~~~~s~\char61{}~a\char91{}0\char93{}\char59{}\\
\tt ~~~~~i~\char61{}~0\\
\tt ~~~~~while\char40{}~i~\char60{}~n~\char41{}\\
\tt ~~~~~\char123{}\\
\tt ~~~~~~~\char47{}\char42{}~~Do~something~to~preserve~the~invarient~\char42{}\char47{}\\
\tt ~~~~~~~i\char43{}\char43{}\char59{}\\
\tt ~~~~~\char125{}
\end{tabbing}
  \[ \textrm{Invarient} \land \lnot (i \leq n) \Rightarrow s = \mathrm{min}\{a[0],\ldots,a[n-1]\}. \]
\end{frame}

\begin{frame}
  \frametitle{Outline of the method}
  \begin{itemize}
  \item First logically specify the desired condition $\varphi$
    \pause
  \item Parameterise it appropriately to get an invarient $I$.
    \pause
  \item Write the loop with condition $C$ such that $I \land \lnot C =
    \varphi$.
  \end{itemize}
\end{frame}

\newcommand{\Sorted}[3]{\mathrm{Sorted}\left(#1,#2,#3\right)}
\newcommand{\Length}[1]{\mathrm{length}\left(#1\right)}
\newcommand{\SortedArray}[1]{\mathrm{SortedArray}\left(#1\right)}

\begin{frame}
  \frametitle{Sorting}
  Let us define what is sorted array 
  
  \[ 
  \SortedArray{a} \equiv \forall i\ 0 \leq i < \Length{a} - 1 \Rightarrow a[i] \leq a[i+1].
  \]
  \pause
  
  \[
  \Sorted a r s \equiv \forall i \  r \leq i < s \Rightarrow a[i] \leq a[i+1]
  \]

  \begin{block}{Observations}
    \begin{itemize}
    \item For all integers $r$ $\Sorted{a}{r}{r+1}$. 
    \item $\Sorted{a}{r}{s} \land \Sorted{a}{s}{t} \Rightarrow \Sorted{a}{r}{t}$.
    \end{itemize}
  \end{block}
\end{frame}

\begin{frame}
  Choose the invarient $\Sorted{a}{0}{i}$ for a parameter $i$.
  \begin{tabbing}\tt
~~~~~i~\char61{}~0\char59{}\\
\tt ~~~~~while\char40{}~i~\char60{}~n\char41{}\\
\tt ~~~~~\char123{}\\
\tt ~~~~~~~\char47{}\char42{}~Do~something~to~restore~invarient~\char42{}\char47{}\\
\tt ~~~~~~~i\char43{}\char43{}\char59{}\\
\tt ~~~~~\char125{}
\end{tabbing}
\end{frame}
\end{document}
