\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 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{spec} s = a[0]; i = 0 while( i < n ) { /* Do something to preserve the invarient */ i++; } \end{spec} \[ \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{code} i = 0; while( i < n) { /* Do something to restore invarient */ i++; } \end{code} \end{frame} \end{document}