\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{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 12} \date{August 24, 2009} \begin{document} \begin{frame} \maketitle \end{frame} \begin{frame} \frametitle{Summary of the last class} \begin{spec} while( C ) { /* invariant is a condition that is true here */ S } \end{spec} \begin{block}{How to design loop?} \begin{itemize} \item Write down the desired outcome $\varphi$. \pause \item Choose a loop invariant $I$ and a loop condition $C$ such that $I \land \lnot C \Rightarrow \varphi$. \pause \item $I$ is obtained by parameterisation (i.e. replacing constants of $\varphi$ by variables) \pause \item The condition $C$ is the negation of parameterisation. \pause \item Initialise variables so that $I$ is true in the base case \pause \item Design the body $S$ to preserve the validity of $I$. \end{itemize} \end{block} \end{frame} \begin{frame} \frametitle{Finding the smallest in a sequence of $n$ numbers} The desired condition is \[ \varphi \equiv s = \mathrm{min}\{ a[0],\ldots, a[n-1]\} \textrm{ (n is a constant here).} \] \pause Parameterising we get the invariant \[ I_i \equiv s = \mathrm{min}\{ a[0],\ldots, a[i-1]\}. \] \pause Note that \[ I_i \land (i = {n}) \Rightarrow \varphi. \] So the condition C is $i \neq n$. Hence the loop. \begin{spec} s = a[0]; i = 1; while( i != n ) { if( s > a[i] ) s = a[i]; i++; } \end{spec} \end{frame} \newcommand{\Length}[1]{\mathrm{length}\left(#1\right)} \newcommand{\SortedArray}[1]{\mathrm{SortedArray}\left(#1\right)} \newcommand{\Sorted}[2]{\mathrm{Sorted}\left(#1,#2\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 s \equiv \forall i \ 0 \leq i < s \Rightarrow a[i] \leq a[i+1] \] \end{frame} \newcommand{\SortedP}[3]{\mathrm{Sorted}'(#1,#2,#3)} \begin{frame} Choose the invariant $\Sorted{a}{i}$ for a parameter $i$. \begin{spec} i = 0; while( i != n) { S; /* Do something to restore invariant */ i++; } \end{spec} \pause $S$ itself is a loop statement. \[ \SortedP a r k \equiv \forall j\ 0 \leq j < r \ a[j] \leq a[j+1] ) \lor j = k - 1 \] \begin{spec} k = i+1; while( k != 0 ) { if( a[k-1] > a[k] ) /* swap a[k-1] and a[k] */ k--; } \end{spec} \end{frame} \end{document}