\documentclass[11pt]{article}
\usepackage{amsmath,amssymb,amsthm,graphicx}
\usepackage{fullpage}
\usepackage[capitalise,nameinlink]{cleveref}
\usepackage{float}
\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}

% 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{4 --- January 26, 2023}{Spring 2023}{Prof.\ Jelani Nelson}{Nishant Bhakar, Ryan Zhu}

\section{Overview}
In the last lecture we introduced blocking flows, but these require link-cut trees to operate efficiently. Before discussing these, we'll motivate them by first discussing another data structure in this lecture, splay trees. (Sleator, Tarjan '85)

\section{Binary Search Trees}
In general, binary search trees store $\left(\text{key},\text{value}\right)$ pairs in such a way that keys are comparable. Binary search trees can be used to solve many problems, including the dictionary problem, the predecessor problem, and others.

\begin{definition}
    A \emph{dictionary} is a data structure that stores a set of $\left(\text{key},\text{value}\right)$ pairs and allows for the following operations:
    \begin{enumerate}
        \item \emph{Insert$\left(\text{k},\text{v}\right)$}: Insert a key-value pair into the dictionary.
        \item \emph{Query$\left(\text{k}\right)$}: Return the value associated with the given key in the dictionary.
        \item \emph{Delete$\left(\text{k}\right)$}: Delete the corresponding key-value pair from the dictionary.
    \end{enumerate}
\end{definition}

\begin{definition}
    A \emph{binary search tree} is a binary tree that satisfies the following properties:
    \begin{enumerate}
        \item Each node has a key.
        \item Each node has a value.
        \item All keys of the left subtree of a node are less than the key of the node.
        \item All keys of the right subtree of a node are greater than the key of the node.
    \end{enumerate}
\end{definition}

\begin{definition}
    The \emph{BST Model} is a model of computation that represents binary search trees.

    Suppose the current node is x. At each time step, we can perform the following operations:
    \begin{enumerate}
        \item \emph{child(x)}: Go to the left or right child of $x$.
        \item \emph{parent(x)}: Go to the parent of $x$.
        \item \emph{rotate(x)}: Rotate x. Rotating a node to the left or right swaps the node with its child. For example, if we rotate the node $x$ to the left, then $x$ becomes the left child of its right child, and the right child of $x$ becomes the left child of $x$'s parent. If $x$ is the root, then the new root is the right child of $x.$ An example is shown in the figure below:
        \begin{figure}[H]
        \centering
        \includegraphics[scale=0.5]{rotate.PNG}
            \caption{Rotating $x$.}
        \label{fig:Drawing1}
        \end{figure}
    \end{enumerate}
\end{definition}

The above operations relate to online algorithms. Online algorithms run in real time and receive operations sequentially. Most importantly, future operations are unknown.

\section{Splay trees}
We now turn the discussion from binary search trees in general to splay trees specifically.

\begin{definition}
    The \emph{Dynamic Optimality Conjucture} states that the cost of splaying is always within a constant factor of the optimal algorithm for performing searches. More specifically, suppose $\sigma$ is a sequence of operations, and $\text{OPT}\left(\sigma\right)$ is the minimum cost to service $\sigma$ under the BST model. Then $\text{cost}_{\text{splay}}(\sigma)=\mathcal{O}(\text{OPT}(\sigma))$
\end{definition}

The best competitive ratio to the offline optimal BST among all online BSTs is $\mathcal{O}\left(\log\log{n}\right)$, achieved by Tango trees \cite{tango}.

\subsection{Notable Properties}

\subsubsection{$O(\log{n})$ Amortized Operation Cost}

Say the amortized cost of query/insert/delete are $t_{q}$/$t_{i}$/$t_{d}$. Then, for all operation sequences $\sigma$, we have that the total time to serve $\sigma$ is upper bounded by $\left(\text{\# of queries}\right)t_{q}+\left(\text{\# of inserts}\right)t_{i}+\left(\text{\# of deletes}\right)t_{d}$.

\subsubsection{Working Set}

For item $j$, let $t_{j}$ be the number of operations since $j$ was last accessed, then the amortized cost of accessing $j$ is $\mathcal{O}\left(t_{j}\right)$.

\subsubsection{Static Optimality}

Suppose the optimal $T$ (found by dynamic programming) put the item $i$ at level $\ell_{i}$ in $T$. Then, the amortized cost of accessing $i$ is $\mathcal{O}\left(\ell_{i}\right)$. This is different from dynamic optimality because it does not take into account the order of operations. If we did know the ordering, we could be more efficient by rotating up frequently accessed items.

\subsubsection{Static Finger}

\begin{definition}
    A \emph{finger} is a pointer to a node in a tree. A \emph{static finger} is a finger that points to a node in a tree, and the finger is not updated after the node is accessed. The amortized cost of accessing a node with a static finger is $\mathcal{O}\left(\log\left|f-i+1\right|\right)$, where $f$ is the node pointed to by the finger and $i$ is the node being accessed.
\end{definition}

\subsubsection{Dynamic Finger}

\begin{definition}
    A \emph{dynamic finger} is a finger that points to a node in a tree, and the finger is updated after the node is accessed. For instance, if the sequence $\sigma=\left\{x_{1},x_{2},x_{3},\cdots\right\}$ then the amortized cost of the $i$th query is $\mathcal{O}\left(\log\left|x_{i}-x_{i-1}+1\right|\right)$.
\end{definition}

\subsection{Implementation}
We've spent a lot of time talking about properties. Now it's time to finally explain what a splay tree actually is.

\begin{definition}
    A \emph{splay tree} is a binary search tree that satisfies the following additional properties in addition to the binary search tree properties:
    \begin{enumerate}
        \item The tree is balanced.
        \item The tree is splayed.
    \end{enumerate}
\end{definition}

\subsubsection{Operations}
Supported operations on a splay tree include:
\begin{enumerate}
    \item \emph{query($k$)}: Find the node with key $k$ by following pointers and then splay($k$).
    
    \begin{figure}[H]
    \centering
    \includegraphics[scale=0.5]{query.PNG}
    \caption{Querying a splay tree.}
    \label{fig:Drawing2}
    \end{figure}
    
    \item \emph{insert($k,v$)}: Insert a node with key $k$ and value $v$ into the tree. Begin by following pointers to where the node with key $k$ should be. Insert it a new node with key $k$ and value $v$, then splay the newly created node to bring it to the root.

    \begin{figure}[H]
    \centering
    \includegraphics[scale=0.5]{insert.PNG}
    \caption{Inserting into a splay tree.}
    \label{fig:Drawing3}
    \end{figure}
    
    \item \emph{delete($k$)}: Delete the node with key $k$ from the tree. Begin by following pointers to find where the node with key $k$ is in the tree. Bring it up to the root by splaying it. Then, delete the node, creating two subtrees $A$ and $B$. Next, splay the node with the largest key in $A$ to the root, and attach $B$ as that node's new right child to join the two subtrees together.

    \begin{figure}[H]
    \centering
    \includegraphics[scale=0.5]{delete.PNG}
    \caption{Deleting from a splay tree.}
    \label{fig:Drawing4}
    \end{figure}
    
    \item \emph{splay($k$)}: Rotate the node with key $k$ to the root of the tree. Suppose $y=\text{parent}(x)$ and $z=\text{parent}(y)$. Then there are three cases:
    \begin{enumerate}
        \item $z=\text{nil}$: Rotate $x$.

        \begin{figure}[H]
        \centering
        \includegraphics[scale=0.5]{case1.PNG}
        \caption{Splaying, case 1}
        \label{fig:Drawing5}
        \end{figure}
        
        \item $x=y\text{.left}$ and $y=z\text{.left}$ or $x=y\text{.right}$ and $y=z\text{.right}$: Rotate $y$ and then rotate $x$.

        \begin{figure}[H]
        \centering
        \includegraphics[scale=0.5]{case2.PNG}
        \caption{Splaying, case 2}
        \label{fig:Drawing6}
        \end{figure}
        
        \item All other cases: Rotate $x$ and then rotate $x$.

        \begin{figure}[H]
        \centering
        \includegraphics[scale=0.5]{case3.PNG}
        \caption{Splaying, case 3}
        \label{fig:Drawing7}
        \end{figure}
    \end{enumerate}
\end{enumerate}

\subsubsection{Operation Properties}
Properties of operations include:

\begin{claim}
    The amortized cost of splay($k$) is $\mathcal{O}\left(\log(\frac{W}{s\left(k\right)}) + 1\right)$. For each $k$ in the database, give it weight $w\left(k\right)$ and define $s\left(k\right):=\sum_{u\in\text{subtree}(k)}w\left(u\right)|W=\sum_{k\in\text{databasae}}w\left(k\right)=s\left(\text{root}\right)$. Additionally, $r\left(k\right):=\log\left(s\left(k\right)\right)$.
    \begin{proof}
        For some state of our data structure, we assign a potential $\Phi\left\{\text{states}\right\}\rightarrow\mathcal{R}$. Note $\Phi$ can be any function. The potential cost of any operation is $\text{true cost}+\Delta\Phi$ where $\Delta\Phi=\Phi_{\text{after operation}}-\Phi_{\text{before operation}}$. Now splay $\Phi=\sum_{k}r\left(k\right)$. We want to demonstrate splay($k$)$\leq3\left(r\left(\text{root}\right)-r\left(x\right)\right)+1$. There are the following cases:
        \begin{enumerate}
            \item Case 1: $\text{potential cost}=\text{true cost}+\Delta\Phi\leq\left(3r'\left(x\right)-r\left(x\right)\right)+1$.
            \item Cases 2-4: $\text{potential cost}=\text{true cost}+\Delta\Phi\leq\left(3r'\left(x\right)-r\left(x\right)\right)$.
            \item Note $\text{potential cost}=2+\Delta\Phi=2+\left(r'\left(x\right)+r'\left(y\right)+r'\left(z\right)\right)-\left(r\left(x\right)+r\left(y\right)+r\left(z\right)\right)$. Note $r'\left(x\right)=r\left(z\right)$ and $r'\left(y\right)\leq r'\left(x\right)$ and
            $r\left(y\right)\geq r\left(x\right)$. All this meaning $\text{potential cost}\leq2+r'\left(x\right)+r'\left(z\right)$. Note by the AM-GM inequality, $\frac{a+b}{2}\geq\sqrt{ab}\Longrightarrow\frac{r\left(x\right)+r'\left(z\right)}{2}\leq\log\left(\frac{s\left(x\right)+s'\left(z\right)}{2}\right)\leq\log\left(\frac{s'\left(x\right)}{2}\right)=r'\left(x\right)-1$, leading to our desired result.
        \end{enumerate}
    \end{proof}
\end{claim}
%%%%%%%%%%%%%%%%%%%%%%%%%
% \begin{center}
% \hrulefill
% \end{center}

% \begin{claim}
%     If $G$ contains no negative cycles, then $\eta(H^B)\leq\frac{\Delta}{2}$.
% \end{claim}
% \begin{proof}
% Take an arbitrary vertex $v$ and look at the path $P_{H^B}(v)$ from $s$ to $v$. Let $u$ be the vertex on $P_{H^B}(v)$ immediately after $s$ (which may equal $v$), and let $P$ be subpath from $u$ onward to $v$ (simply remove $s$ and the edge $(s,u)$). Then
% \begin{align}
% \nonumber      \mathop{dist}_G(u,v) &\le w_H(P)\\
% \nonumber      {}&\le w_{H^B}(P) - |E^{\mathop{neg}}(H^B)\cap P|\cdot B\\
%     {}&= w_{H^B}(P) - \eta_{H^B}(v)\cdot B \label{eqn:eq1}\\      
%     {}&\le - \eta_{H^B}(v)\cdot B \label{eqn:eq2}
% \end{align}
% where \cref{eqn:eq1} uses that $w(s,u) = 0$, and \cref{eqn:eq2} uses that there is a path from $s$ to $v$ of length $0$ (namely the single edge) and thus the shortest path can be no longer.

% Now, by guarantees of LowDiamDecomp on weak diameter, we know $\mathop{dist}_G(v,u) \le dB = \Delta B/2$. Since $G$ contains no negative cycles, combining this with $\mathop{dist}_G(u,v) \le -\eta_{H^B}(v)\cdot B$ then implies $\Delta B / 2 - \eta_{H^B}(v)\cdot B \ge 0$. Rearranging gives $\eta_{H^B}(v) \le \Delta /2$. The claim holds since $v$ was arbitrary.
% \end{proof}

% \begin{center}
% \hrulefill
% \end{center}
%%%%%%%%%%%%%%%%%%%%%%%%%

\bibliographystyle{alpha}

\begin{thebibliography}{42}

% \bibitem{BNW22}
% Aaron Bernstein, Danupon Nanongkai, and Christian Wulff-Nilsen.
% \newblock Negative-weight
% single-source shortest paths in near-linear time. 
% \newblock {\em Proceedings of the 63rd Annual IEEE
% Symposium on Foundations of Computer Science (FOCS)}, 600--611, 2022.
% \bibitem{FF56}
% Lester R. Ford and Delbert R. Fulkerson.
% \newblock Maximal flow through a network.. 
% \newblock {\em . Canadian Journal
% of Mathematics}, 8(3):399404, 1956

\bibitem{tango}
Daniel D. Sleator and Robert E. Tarjan.
\newblock Self-adjusting binary search trees.
\newblock {\em Journal of the ACM}, 32(3):652--686, 1985.

\end{thebibliography}

\end{document}

\end{document}
