\documentclass[landscape]{slides}
\usepackage{multicol}
\usepackage[all,poly,ps,dvips,color]{xy}
\input{preamble}
  
\input{SLIDES}
 
%\usepackage[dvips]{graphics}
%\usepackage[all,poly,ps,dvips,color]{xy}
%\UseCrayolaColors

%\DeclareMathOperator{\mathin}{in}
%\DeclareMathOperator{\mathout}{out}

\newcommand{\vindex}[2]{\left[ #1 \right] _ #2}
\newcommand{\eigen}{\mathbf a}
\newcommand{\elem}{\mathbf e}
\newcommand{\state}{\mathbf x}
\newcommand{\maxdeg}{\Delta}
\newcommand{\maxdegC}{d_{max}}

\newcommand{\din}{d_{\mathin}}
\newcommand{\dout}{d_{\mathout}}
\newcommand{\dmax}{d_{\max}}


\newcommand{\nlabel}[2]{\ar@{} @(#1,#1) |<<{#2}}
\newcommand{\gnlabel}[2]{[#1(.25)] {#2}}
\newcommand{\node}{{\bullet}}
\newcommand{\bypass}{{\circ}}

\begin{document}

\begin{titlepage}
\title{CS 591, Lecture 14}

\author {Jared Saia \\
{\red University of New Mexico}}

\date{}
\maketitle

\end{titlepage}


\begin{SLIDE}[Class Outline]

\begin{itemize}
\item Intro to Online Algorithms
\item Ski Rental Problem
\item Intro to Randomized Online Algorithms
\item Paging
\end{itemize}
\end{SLIDE}

\begin{SLIDE}[Online Algorithms]
\begin{itemize}
\item An online algorithm is one where the input is received in an
online manner and the output must be produced online
\item Example 1: Renting Skis
\item Example 2: Accessing Items in a List
\item Example 3: Paging
\end{itemize}
\end{SLIDE}


\begin{SLIDE}[Competitive Ratio]

\begin{itemize}
\item Optimization problem: want to find feasible solution which
optimizes some quantity (e.g. knapsack problem)

\item For a given optimization problem input, I, we will let \emph{OPT
(I)} be the optimum solution

\item For a given algorithm, ALG, and input sequence, I, let \emph{ALG
(I)} be the cost incurred be ALG on input I

\item An online algorithm is said to be \emph{$c$-competitive} if
there is a constant $\alpha$ such that for all finite input sequences
$I$,


$$ALG (I) \leq c * OPT (I) +\alpha.$$

\end{itemize}
\end{SLIDE}

\begin{SLIDE}[Ski Rental Problem]

\begin{itemize}
\item A set of skis costs $x$ dollars per day to rent
\item A set of skis costs $kx$ dollars to buy
\item Each day your kid decides either to 1) ski or 2)
end her ski career forever
\item Each day you decide whether to 1) rent skis or 2) buy skis
\item Question: What's the best online algorithm for minimizing the
money you spend?
\end{itemize}
\end{SLIDE}

\begin{SLIDE}[The Algorithm]
\begin{itemize}
\item There is a $2$-competitive algorithm.
\item The online algorithm: Rent until you've spent $kx$ dollars on
skis, and then buy them.
\end{itemize}
\end{SLIDE}

\begin{SLIDE}[Analysis]
Assume your kid skis for $n$ days
\begin{itemize}
\item Case 1: $n\leq k$: OPT is $nx$ and ALG is $n*x$, so the
competitive ratio in this case is $1$.
\item Case 2: $n > k$: OPT is $kx$ and ALG is $2*kx$, so the
competitive ratio in this case is $2$.
\end{itemize}
\end{SLIDE}

\begin{SLIDE}[The Paging Problem]
\begin{itemize}
\item Memory: stores all possible pages
\item Cache: can store a fixed set of $k$ pages, ($k$ is small)
\item Given a request, for page $p$, $p$ must be made available in the cache
\item If $p$ is already in the cache (a hit), we need do nothing, if
$p$ is not in the cache (a miss), we incur a page fault and must copy
$p$ into the cache
\item On a page fault, we might need to ``evict'' another page to make
room for $p$
\end{itemize}
\end{SLIDE}

\begin{SLIDE}[The Paging Problem]
\begin{itemize}
\item The pages that are requested arrive in an online manner
\item We need to make decisions about which page to evict in an online manner
\item We assume there is a cost of $1$ to bring a page into the cache
(a miss)
\item We want an online algorithm which incurs the least number of
misses
\item This problem mirrors many real-world problems, including:
efficiently utilizing a L1 cache, efficiently utilizing main memory,
and caching web pages efficiently
\end{itemize}
\end{SLIDE}

\begin{SLIDE}[Some Paging Algorithms]
\begin{itemize}
\item LRU: When eviction is necessary, replace the page whose most
recent request was earliest
\item FIFO: Replace the page that has been in the cache longest
\item FWF: On page fault, evict \emph{all} pages from cache
\item LFU: Replace the page that has been requested the least since
moving into the cache
\end{itemize}
\end{SLIDE}

\begin{SLIDE}[Theoretical Analysis]
\begin{itemize}
\item LRU:  $k$ is the competitive ratio
\item FIFO: $k$
\item FWF: $k$
\item LFU: \emph{not competitive}
\end{itemize}
\end{SLIDE}

\begin{SLIDE}[Upperbound]
\begin{itemize}
\item We'll show the bound of $k$ for FWF
\item Divide the request sequence, $\sigma$, into \emph{phases} as
follows:
\item Phase $0$ is the empty sequence
\item Phase $i$ is the maximal sequence following phase $i-1$ that
contains at most $k$ distinct requests
\end{itemize}
\end{SLIDE}

\begin{SLIDE}[Upperbound]
Two important facts:
\begin{itemize}
\item FWF incurs no more than $k$ faults per phase
\item OPT incurs at least one fault per phase 
\end{itemize}

This gives us a competitive ratio of $k$.

\end{SLIDE}



\begin{SLIDE}[Lower Bound]
\begin{itemize}
\item In fact, $k$ is the best possible competitive ratio for any algorithm:
\item Assume we've got $k+1$ pages and for any algorithm, ALG,
consider the sequence $\sigma$ which always requests the one page ALG
doesn't have in the cache
\item ALG faults at every request on this sequence
\item OPT faults at most once every $k$ requests.  (How?)
\end{itemize}
\end{SLIDE}

\begin{SLIDE}[Empirical Analysis]
\begin{itemize}
\item p. 41 in ``Online Computation and Competitive Analysis'' book
\item Request sequences were generated by page request for ``X windows''
programs, a makefile program and a ``disk copy'' program
\item $700,000$ requests, cache sizes ranged from $10$ to $600$
\item Algorithm Rankings (Best to Worst): LRU, FIFO, FWF
\item Both LRU and FIFO had competitive ratios less than $4$ even as
the cache size grew!
\end{itemize}
\end{SLIDE}


\begin{SLIDE}[Problems]
\begin{itemize}
\item $k$ is not too impressive a competitive ratio
\item Particularly since in practice, the competitive ratio seems to
be much better
\item Q: Can we do better if we use randomness?
\end{itemize}

\end{SLIDE}

\begin{SLIDE}[Randomized Online Algorithms]
\begin{itemize}
\item For a randomized online algorithm, we typically assume that the
input sequence is created without knowledge of the random bits used by
the algorithm
\item Typically the input sequence is thought of as being created by
an ``adversary'' who doesn't know the random bits
\item An adversary who doesn't know the random choices made by our
algorithm is called an \emph{Oblivious Adversary}
\item An online algorithm is said to be \emph{$c$-competitive} if
there is a constant $\alpha$ such that for all finite input sequences,
$I$:

$$E (ALG (I)) \leq c * OPT (I) +\alpha.$$
\end{itemize}
\end{SLIDE}

\begin{SLIDE}[Algorithm MARK]

We will consider the randomized algorithm MARK defined as follows:
\begin{itemize}
\item Initially all pages are marked
\item If there is a request for a page $p$ which is in the cache but
unmarked, then $p$ is marked
\item Otherwise if $p$ is not in the cache, $p$ is brought
\emph{marked} into the cache
\item If all the pages in the cache are marked when $p$ is brought in,
they are all unmarked first
\item The page that is evicted is chosen uniformly at random from all
unmarked pages currently in the cache.
\end{itemize}
\end{SLIDE}

\begin{SLIDE}[MARK Analysis]
\begin{itemize}
\item Defn: $H_{k} = 1 + 1/2 + 1/3 + \dots + 1/k$
\item Fact: $\ln k < H_{k} < 1 + \ln k$
\item \emph{Theorem:} The competitive ratio of Mark is $2*H_{k}$
\end{itemize}
\end{SLIDE}

\begin{SLIDE}[MARK Analysis]
\begin{itemize}
\item Fix a request sequence $\sigma$ and partition it into phases of
$k$ unique requests as before
\item For each phase $i$, call the pages in the cache just before the
start of the phase the \emph{old} pages and call all other pages
requested in the $i$-th phase \emph{new} pages
\item Note that each request in the $i$-th phase is to either an old
or a new phase
\item Note also that repeated requests to a new or old page don't
contribute to our cost since each page requested is kept in the cache
until the end of the phase
\end{itemize}
\end{SLIDE}

\begin{SLIDE}[MARK Analysis]
\begin{itemize}
\item Let $m_{i}$ be the number of new pages requested in the $i$-th phase
\item The worst order for the MARK algorithm is if all requests to
new pages precede requests to old pages
\item In this order, the requests to new pages incur $m_{i}$ faults
\item What about the remaining $k-m_{i}$ requests to old pages?
\end{itemize}
\end{SLIDE}

\begin{SLIDE}[MARK Analysis]
\begin{itemize}
\item What do we want?: We want to compute the expected number of
misses on the $k-m_{i}$ old pages

\item We can get this using linearity of expectation.  We need to
answer: What is the probability that the $j$-th old page requested
will not be in the cache?  (i.e. it will generate a fault)

\item This probability is $m_{i}/ (k- (j-1))$.  Why?
\end{itemize}
\end{SLIDE}

\begin{SLIDE}[Mark Analysis]

Hence the number of faults incurred by MARK is:
\begin{eqnarray}
m_{i}+\sum_{j=1}^{k-m_{i}} \frac{m_{i}}{k-j+1} & = & m_{i} (H_{k}-H_{m_{i}}+1)\\
& \leq  & m_{i}H_{k}
\end{eqnarray}

\end{SLIDE}

\begin{SLIDE}[Bound for OPT]
It's not hard to bound OPT for this sequence:
\begin{itemize}
\item During the $i$-th and $i+1$-th phase, at least $k+m_{i}$
distinct pages have been requested
\item Thus for each $i\geq 2$, the number of faults made by OPT in the
$i$-th and $i+1$-th phase is $m_{i}$
\item The number of faults made by OPT in the first phase is $m_{1}$
\item Thus the total number of faults made by OPT is at least
$1/2*\sum_{i} m_{i}$
\end{itemize}
\end{SLIDE}

\begin{SLIDE}[Take Away]
\begin{itemize}
\item So MARK has no more than $H_{k} * \sum_{i} m_{i}$ faults
\item OPT has at least $\sum_{i} m_{i}$ faults
\item Hence the competitive ration is $H_{k}$
\end{itemize}
\end{SLIDE}

\end{document}
