\input{preamble}

\title{Resource Analysis: Problem Set 2}% \\ Recurrence Relations and Amortized Analysis}
\setcounter{section}{2}

\begin{document}
\maketitle

\begin{center}
  \noindent
  \large{Due before 1:30pm on Monday, February 1}
\end{center}
\subsection{(16 Points) Analysis of Insertion Sort}

Insertion sort for lists of lists can be implemented in OCaml as
follows.

\begin{lstlisting}
let rec compare_list l1 l2 =
  match l1 with
  | [] -> true
  | x::xs ->
     match l2 with
     | [] -> false
     | y::ys ->
	if x = y then
	  compare_list xs ys
	else
	  x < y

let rec insert le x l = 
  match l with
  | [] -> [x]
  | y::ys ->
     if le y x then y::insert le x ys
     else x::y::ys
		  
let rec isort le l =
  match l with
  | [] -> []
  | x::xs -> insert le x (isort le xs)

let isort_list = isort compare_list
\end{lstlisting}

The function \code{isort\_list} sorts lists in lexicographical
order. Use recurrence relations to derive an asymptotic worst-case
bound for \code{isort\_list} in the same way we derived a bound for
quick sort in the lecture.
\begin{enumerate}[a)]
\item Define a system of recurrence relation so that there is one
  recurrence relation for \code{isort\_list} and for every function
  that can called by \code{isort\_list}.
\item Solve the system of recurrence relations in a bottom-up manner.
\end{enumerate}
%
{\bf Remarks:}
\begin{description}
\item[\textnormal{\em Cost model:}] Derive a bound on the
  \emph{number of function calls} that are performed by a
  funcstions. Constructor calls such as $\mathop{::}$ do not count as function calls.
\item[\textnormal{\em Higher-order functions:}]It is up to you how to
  treat higher-order functions. One possibility is to use higher-order
  recurrence relations like $T(n,f) = f(n)$. Another possibility is to
  simply model first-order functions such as \code{insert
    compare\_list} only.
\item[\textnormal{\em Polymorphism:}]In OCaml, \code{isort\_list} has
  the type \code{'a list list $\to$ 'a list list}.  Assume for the
  analysis the type \code{int list list $\to$ int list list}. In
  particular, you can account constant time for the operations $<$ and
  $=$ in the function \code{insert}.
\end{description}

\subsection{(8 Points) Amortized Analysis I (Potential Method)}

Consider a sequence of operations $f_1,\ldots,f_n$ in which the cost
of the $i$th operation is defined by

$$\mathit{cost}(f_i) = \left\{ 
  \begin{array}{ll}
    i & \text{if } i = 2^k \text{ for some } k\\
    1 & \text{otherwise}
  \end{array}
\right.$$
Use the potential method to determine the amortized cost per
operation.

\subsection{(12 Points) Amortized Analysis II (Functional Queue)}

A simple and elegant way of implementing a queue in a functional
programming language is to use two lists.
\begin{lstlisting}
type 'a queue = Queue of 'a list * 'a list

let empty = Queue ([], [])

let enqueue (Queue (inq, outq)) x =
     Queue (x::inq, outq)

let rec rev_append l1 l2 =
  match l1 with
  | [] -> l2
  | x::xs -> rev_append xs (x::l2)

let rev l = rev_append l []

let rec dequeue (Queue (inq, outq)) =
  match outq,inq with
  | [], []  -> raise Queue_empty
  | [], _   -> dequeue (Queue ([],rev inq))
  | y::ys,_ -> (Queue (inq, ys), y)
\end{lstlisting}

\begin{enumerate}[a)]
\item Briefly describe the idea of the \code{queue} data structure.
\item How many \code{cons} operations (uses of \code{::}) are
  performed by \code{dequeue} in the worst-cose?
\item Consider a sequence of $n$ \code{enqueue} or \code{dequeue}
  operations of the form
  \begin{lstlisting}
    let $b_1$ in
    let $b_2$ in
    $\ldots$
    let $b_k$ in    
    ()
  \end{lstlisting}
  \vspace{-1.2\baselineskip}
  where$$
  \begin{array}{ccc}
       b_i \equiv \hspace{1em}  \code{q = enqueue q n} \hspace{1em}  \text{ for an integer } n \hspace{1em}  & \text{or}&  \hspace{1em}    b_i \equiv \hspace{1em}  \code{(q,\_) = dequeue q}
  \end{array}$$

  Use the potential method to determine the amortized number of
  \code{cons} operations that is performed per operation. (Clearly
  define the potential function that you are using.) \label{aa3}
\item Consider the following OCaml expression.
  \begin{lstlisting}
    let q = Queue ([3;2;1],[]) in
    let (_,_) = dequeue q in
    let (_,_) = dequeue q
  \end{lstlisting}
  \vspace{-1.2\baselineskip}
  How many \code{cons} operation are performed if the above code is
  evaluated? What program property is necessary to make the analysis you
  performed in Part~(\ref{aa3}) applicable? 
\end{enumerate}

% Using the queue correctly: Linear type systems

\end{document}

%%% Local Variables:
%%% mode: latex
%%% mode: flyspell
%%% TeX-master: t
%%% End:
