\documentclass[11pt]{article}
\usepackage{amsmath,amssymb,amsthm}
\usepackage{fullpage}
\usepackage{hyperref}
\usepackage[capitalise,nameinlink]{cleveref}
\usepackage{listings}% http://ctan.org/pkg/listings
\lstset{
  basicstyle=\ttfamily,
  mathescape
}
\hypersetup{hidelinks,hypertexnames=false,colorlinks=true,allcolors=blue}
\usepackage{cite}
\crefname{lemma}{Lemma}{Lemmas}
\crefname{fact}{Fact}{Facts}
\crefname{theorem}{Theorem}{Theorems}
\crefname{corollary}{Corollary}{Corollaries}
\crefname{claim}{Claim}{Claims}
\crefname{example}{Example}{Examples}
\crefname{problem}{Problem}{Problems}
\crefname{setting}{Setting}{Settings}
\crefname{definition}{Definition}{Definitions}
\crefname{assumption}{Assumption}{Assumptions}
\crefname{subsection}{Subsection}{Subsections}
\crefname{section}{Section}{Sections}

\DeclareMathOperator*{\E}{\mathbb{E}}
\let\Pr\relax
\DeclareMathOperator*{\Pr}{\mathbb{P}}

\newcommand{\eps}{\varepsilon}
\newcommand{\inprod}[1]{\left\langle #1 \right\rangle}
\newcommand{\R}{\mathbb{R}}

\newcommand{\handout}[5]{
  \noindent
  \begin{center}
  \framebox{
    \vbox{
      \hbox to 5.78in { {\bf CS 270: Combinatorial Algorithms and Data Structures
        } \hfill #2 }
      \vspace{4mm}
      \hbox to 5.78in { {\Large \hfill #5  \hfill} }
      \vspace{2mm}
      \hbox to 5.78in { {\em #3 \hfill #4} }
    }
  }
  \end{center}
  \vspace*{4mm}
}

\newcommand{\lecture}[4]{\handout{#1}{#2}{#3}{Scribe: #4}{Lecture #1}}

\newtheorem{theorem}{Theorem}[section]
\newtheorem*{theorem*}{Theorem}
\newtheorem{itheorem}{Theorem}

\newtheorem{subclaim}{Claim}[theorem]
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem*{proposition*}{Proposition}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem*{lemma*}{Lemma}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem*{conjecture*}{Conjecture}
\newtheorem{fact}[theorem]{Fact}
\newtheorem*{fact*}{Fact}
\newtheorem{exercise}[theorem]{Exercise}
\newtheorem*{exercise*}{Exercise}
\newtheorem{hypothesis}[theorem]{Hypothesis}
\newtheorem*{hypothesis*}{Hypothesis}
\newtheorem{conjecture}[theorem]{Conjecture}

\theoremstyle{definition}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{setting}[theorem]{Setting}
\newtheorem{construction}[theorem]{Construction}
\newtheorem{example}[theorem]{Example}
\newtheorem{question}[theorem]{Question}
\newtheorem{openquestion}[theorem]{Open Question}
% \newtheorem{algorithm}[theorem]{Algorithm}
\newtheorem{problem}[theorem]{Problem}
\newtheorem{protocol}[theorem]{Protocol}
\newtheorem{assumption}[theorem]{Assumption}
\newtheorem{exercise-easy}[theorem]{Exercise}
\newtheorem{exercise-med}[theorem]{Exercise}
\newtheorem{exercise-hard}[theorem]{Exercise$^\star$}
\newtheorem{claim}[theorem]{Claim}
\newtheorem*{claim*}{Claim}

\newtheorem{remark}[theorem]{Remark}
\newtheorem*{remark*}{Remark}
\newtheorem{observation}[theorem]{Observation}
\newtheorem*{observation*}{Observation}

% additional packages/commands

\usepackage{mathtools}
\usepackage{xparse}
\usepackage{suffix}  % commands with suffix
\usepackage{physics}
\usepackage{parskip, float}
\usepackage{microtype}  % Micro-kerning

\newcommand\definedas\coloneqq
\newcommand\from{:}
\renewcommand{\Pr}{\operatorname{\mathbb{P}}\mathopen{}\opbraces{}}  % Probability
\WithSuffix\newcommand\Pr*{\operatorname{\mathbb{P}}\mathopen{}}  % Probability (no qty)
\newcommand{\Exp}{\operatorname{\mathbb{E}}\mathopen{}\opbraces{}}  % Expected value
\WithSuffix\newcommand\Exp*{\operatorname{\mathbb{E}}\mathopen{}}  % Expected value (no qty)

% 1-inch margins, from fullpage.sty by H.Partl, Version 2, Dec. 15, 1988.
% \topmargin 0pt
% \advance \topmargin by -\headheight
% \advance \topmargin by -\headsep
% \textheight 8.9in
% \oddsidemargin 0pt
% \evensidemargin \oddsidemargin
% \marginparwidth 0.5in
% \textwidth 6.5in

% \parindent 0in
% \parskip 1.5ex

\begin{document}

\lecture{15 -- March 7th, 2023}{Spring 2023}{Prof.\ Jelani Nelson}{Vaibhav Agrawal, Eric  Nyugen}


\section*{Continuing Online Algorithms}

Today looking at mostly paging: cache replacing policy. 
\section{Deterministic Paging}
We consider the paging problem for a cache of size $k$. If a page is already in the cache, we can access it for free. Otherwise, we will incur cost by retrieving it from disk.
\begin{definition}[Page Fault]
A page fault occurs when the cache misses, which means the page we were requesting was not in the cache. If the cache is full, we will need to pick a page to evict to make room for the requested page. We define the cost of a page fault cost = 1.
\end{definition}

\subsection{Last Time: List Update Problem}
In the list update problem, we maintain a linked list of items. We had an operation access(x), which incurs cost of $i$, where i is the position of x in the list. We can view the paging problem as a list update problem but with a modified cost function, where
\begin{equation}
  cost(i)=\begin{cases}
    1, & \text{if $i>k$}.\\
    0, & \text{otherwise}.
  \end{cases}
\end{equation}
After any access(x), we move item $x$ to the front as the first $k$ items in the list represents what is in the cache. Also, we can assume some exchanges are free (for eviction).

\subsection{Heuristics for Paging}
Listed are heuristics for paging that choose which page to evict in the case the cache is full.
\begin{itemize}
\item $\textbf{Least Recently Used}$ or $\textbf{LRU}$: The page that was least accessed will be evicted.
\item $\textbf{First In First Out}$ or $\textbf{FIFO}$: For every page in the cache, we record the timestep when they first entered the cache. The page who has been there the longest (has the earliest timestep) will be evicted.
\item $\textbf{Least Frequently Used}$ or $\textbf{LFU}$: We track a counter for each page. Each time a page is accessed, add to its counter. We then evict the page with the smallest counter.
\item $\textbf{Belady's Algorithm (1966)}$: We evict the page that will be requested furthest in the future. This is the optimal algorithm.
\end{itemize}
\textbf{Resource augmentation:} $cost(LRU_k) \leq \frac{k}{k - h + 1} OPT_n$

\begin{lemma}
No deterministic algorithm can have competitive ratio less than k.

    \begin{proof}
        Imagine there are k+1 different memory address in my machine. Given any algorithm A, consider it currently has k things in its cache. It will bring something in and evict something. Then I will request the evicted page. Do this is a loop. This leads to m requests $\implies$ m page faults for A. 
    
        Look at OPT. It evicts the person that is requested farthest in the future. So every time it faults it does not fault any more for the next k+1 times. So Opt faults $\leq \lceil{\frac{m}{k}}\rceil$ times. 
    \end{proof}
\end{lemma} \\
\begin{definition}
    1-bit LRU
\begin{enumerate}
    \item unmark all pages
    \item request(x) 
        \begin{enumerate}
            \item if $x \notin $ cache
            If all pages are marked then unmark all. Always: Evict arbitrary unmarked pages and finally insert $x$ into cache
            \item mark x
        \end{enumerate}
\end{enumerate}
\end{definition}
\begin{theorem}
LRU/FIFO are k-competitive. \\
    \begin{proof}
        Will show \textbf{1-bit LRU} is k-competitive. LRU is a way of implementing 1-bit LRU so we just need to proof this.

        Divide time into phases:
        \begin{enumerate}
            \item everytime we execute step 1 starts new phase.
            \item in each phase, 1-bit LRU has $\leq k$ page faults. 
            \item OPT has $\geq 1$ fault per phase.
        \end{enumerate}
    \end{proof}
\end{theorem}

\section{Randomized Paging}
\subsection{Attack Model for Randomized Algorithms}
\begin{itemize}
\item \textbf{Omniscient adversary}: This adversary can see the future, including the outcomes of all our calls to random(). For example, it knows our future coin tosses. From the adversary's perspective, our randomized algorithm is essentially deterministic. No algorithm can beat k-competitive against this.
\item \textbf{Adaptive adversary}: This adversary cannot see the future, but they can see our past. They know what random decisions we made up until now. So, they know what is in our cache. No algorithm can beat k-competitive against this either.
\item \textbf{Oblivious adversary}: This adversary only knows the code for our algorithm. It does not know future outcomes for calling random(), and does not know what is in our cache. Then, we claim that $2H_k$-competitive is possible where $H_k$ is the kth harmonic number, defined as $H_k = \sum_{j=1}\frac{1}{j}$. We also bound $H_k \approx ln(k) + O(1)$.
\end{itemize}

It is known that $H_k$-competitive is actually possible due to [McGeoch, Sleaor, '91] and less than $H_k$ is not possible.

\begin{definition}
Competitive ratio of randomized algorithm $A$ is $C$ if for all sequences $\sigma$:
\begin{equation}
\mathbb{E}[cost(A(\sigma))] \leq C \cdot OPT(\sigma) + O(1)
\end{equation}
\end{definition}

\subsection{Randomized Marking Algorithm}
The randomized marking algorithm $Mark$ is an implementation of 1-bit LRU, but randomized. The modification to the algorithm is that we evict a uniformly random page, instead of an arbitrary page.


\begin{definition}
A page is clean if it was not requested in the last phase and not requested so far in the current phase.
\end{definition}
\begin{definition}
A page is stale if it was requested in the last phase but not yet in the current phase.
\end{definition}

For future analysis, we define $D_i :=$ the number of pages in $OPT$'s cache at the beginning of phase $i$, which are not in $Mark$'s cache. Equivalently, $D_{i+1}$ := the number of pages in $OPT$'s cache at the end of phase $i$, which are not in $Mark$'s cache. We also define $L_{i}$ := the number of clean requests in phase $i$.

\begin{claim}
$OPT \geq \frac{1}{2} \sum_{i}(L_i + D_{i+1} - D_i) \geq \frac{1}{2} \sum_{i} L(i)$ \\
This series telescopes. $D_0$ is 0 and $D_i+1$ is nonnegative, so we ultimately get an upper bound of $\frac{1}{2} \sum_{i} L(i)$.
\end{claim}
\begin{proof}
\begin{equation}
\text{Cost of $OPT$ in phase $i$} \geq max\{L_i - D_i, D_i+1\}.
\end{equation}
Let's say we just entered phase $i$. In this phase, there will be $L_i$ clean requests. For $Mark$, all clean page requests will end up being page faults. Since there exists $L_i$ clean requests that are not in $Mark$'s cache at the beginning of the phase, it follows that there exists $\geq L_i - D_i$ requests that are not in $OPT$'s cache at the beginning of the phase. \\ 
$D_{i+1}$ is the number of pages in $OPT$'s cache at the end of phase $i$, which are not in $Mark$'s cache. Everything requested in phase $i$ is in Mark's cache, which is $k$ different pages. These $k$ pages also had to be serviced by $OPT$ but $OPT$ had to service an additional $D_{i+1}$ that were left over somewhere. Therefore, $OPT$ had $\geq D_{i+1}$ page faults.
\end{proof}

\begin{claim}
$\mathbb{E}[cost(Mark(\sigma)] \leq H_k \sum_{i} L_i$
\end{claim}
\begin{proof}
Suppose that we have seen $c$ clean requests and $s$ stale requests so far in this phase. At the beginning of the phase, our cache had $k$ stale pages. As we serviced $s$ stale requests, $s$ pages are definitely in our cache and have been marked. However, the remaining $k-s$ stale pages might not be in our cache, as $c$ of them have been clobbered by clean requests. Then, the probability that a new stale request is not in our cache is $\leq \frac{c}{k-s}$
\begin{align*}
\mathbb{E}(cost(Mark(\sigma)) \text{ in phase i} &\leq L(i) + \sum_{s=0}^{k - L(i) - 1} \frac{c}{k-s}
\\ &= L(i) + \sum_{j = L(i) + 1}^{k} \frac{c_j}{j}
\\ &\leq L(i) + L_i \cdot H_k - L_i
\\ &= L_i \cdot H_k
\end{align*} 
\end{proof}

\begin{claim}
Randomized marking algorithm $Mark$ is 2$H_k$-competitive.
\end{claim}
\begin{proof}
Earlier, we have proven that
\begin{equation}
\mathbb{E}[cost(Mark(\sigma)] \leq H_k \sum_{i} L_i
\end{equation}
and
\begin{equation}
OPT \geq \frac{1}{2} \sum_{i} L(i)
\end{equation}
Then by combining the two,
\begin{equation}
\mathbb{E}[cost(Mark(\sigma)] \leq 2H_k \cdot OPT
\end{equation}
\end{proof}

\begin{claim}
It is impossible to beat $H_k$-competitive.
\end{claim}
\begin{proof}
At every point in time, request a uniform random page amongst pages \{1, ..., k+1\}. 
For any algorithm $A$, $\mathbb{E}(cost(A(\sigma))) = \frac{m}{k+1}$ for $m$ requests. The Coupon Collector problem implies that
\begin{equation}
OPT \leq \frac{m}{k\cdot H_k}
\end{equation}
\end{proof}

\textbf{Next time:} Online Primal-dual. Similar to LP duality to get a good competitive ratio. 
\bibliographystyle{alpha}
\begin{thebibliography}{1}

\bibitem{ST85}
Daniel Sleator, Robert Tarjan. 
\newblock Amortized Efficiency of List Update and Paging Rules.
\newblock {\em ACM},  28(2):202–208, 1985.

\bibitem{MT91}
Lyle McGeoch, Robert Tarjan. 
\newblock A Strongly Competitive Randomized Paging Algorithm.
\newblock {\em Algorithmica},  6(6):816–825, 1991.


\end{thebibliography}


\end{document}