
\documentclass[11pt]{article}
\usepackage{latexsym}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{epsfig}
\usepackage{subfig}

\newcommand{\handout}[7]{
  \noindent
  \begin{center}
  \framebox{
    \vbox{
      \hbox to 5.78in { {\bf 6.851: Advanced Data Structures } \hfill #2 }
      \vspace{4mm}
      \hbox to 5.78in { {\Large \hfill #7  \hfill} }
      \vspace{2mm}
      \hbox to 5.78in { {\em #3 \hfill #4}}
      \vspace {1mm}
      \hbox to 5.78in {{\em \hfill #5}}
      \vspace{1mm}
      \hbox to 5.78in {{\em \hfill #6}}
    }
  }
  \end{center}
  \vspace*{4mm}
}

\newcommand{\lecture}[6]{\handout{#1}{#2}{#3}{Scribes: #4}{#5}{#6}{Lecture #1}}

\newtheorem{theorem}{Theorem}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{observation}[theorem]{Observation}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{claim}[theorem]{Claim}
\newtheorem{fact}[theorem]{Fact}
\newtheorem{assumption}[theorem]{Assumption}

% 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
%\renewcommand{\baselinestretch}{1.25}

\begin{document}

\lecture{9 --- March 15, 2012}{Spring 2012}{Prof.\ Erik Demaine}{Tim Kaler(2012)}{Jenny Li(2012)}{Elena Tatarchenko(2012)}

\section{Overview}
This is the last lecture on memory hierarchies. Today's lecture is a crossover between cache-oblivious data structures and geometric data structures. 

First, we describe an optimal cache-oblivious sorting algorithm called Lazy Funnelsort. We'll then see how to combine Lazy Funnelsort with the sweepline geometric technique to solve batched geometric problems. Using this sweepline method, we discuss how to solve batched orthogonal 2D range searching. Finally, we'll discuss online orthogonal 2D range searching, including a linear-space cache-oblivious data structure for 2-sided range series, as well as saving a log log factor from the normal 4-sided range search.

\section{Lazy Funnelsort}
\begin{figure}[h!]
  \begin{center}
    \scalebox{0.9}{\includegraphics{funnel.png}}
  \end{center}
  \caption{Memory layout for K-funnel sort.}
  \label{fig:funnel}
\end{figure}
A funnel merges several sorted lists into one sorted list in an output buffer. Suppose we'd like to merge $K$ sorted lists of total size $K^3$. We can merge them in $O(\frac{K^3}{B} \lg_{M/B} (\frac{K}{B}) + K)$ memory transfers. \emph{Note: The $+ K$ term may be important in certain implementations, but generally the first term will dominate.}

A $K$-funnel is a complete binary tree with $K$ leaves. Each of the subtrees in a $K$-funnel is recursively a $\sqrt{K}$-funnel. The edges of a $\sqrt{K}$-funnel point to buffers, each of which is size $K^{3/2}$. Because there are $\sqrt{K}$ buffers, the total size of the buffers in the $K$-funnel is $K^2$. See Figure \ref{fig:funnel} for a visual representation. 

When the $k$-funnel is initialized, its buffers are all empty. At the very bottom, the leaves point to input lists. 

The actual funnelsort algorithm is an $N^{1/3}$-way mergesort with $N^{1/3}$-funnel merger.
\emph{Note: We can only afford $N^{1/3}$-funnel as a merger because of the $K^3$ multiplier from above.}

\subsection{Filling buffers}
When filling a buffer, we'd like the end result to be the output buffer being completely full.

\begin{figure}[h!]
  \begin{center}
    \scalebox{1.0}{\includegraphics{childnodes.png}}
  \end{center}
  \caption{ Filling a buffer.}
  \label{fig:filling}
\end{figure}

In order to fill a buffer, we must look to the two child buffers to supply the data (See Figure \ref{fig:filling}). We will merge the elements of the child buffers into the parent buffer, as long as both child buffers still contain items. We merge by picking and putting in the smaller element of the two child buffers (regular binary merge). Whenever one of the child buffers becomes empty, recursively fill it. We are only considering two input buffers and one resulting, merged buffer at a time. As described above, each leaf in the funnelsort tree has an input list which supplies the original data.


\subsection{Distribution Sweeping via Lazy Funnelsort}

The idea of distribution sweeping is that we can use funnelsort to sort, but it can also do a divide and conquer on the key value.

We can actually augment the binary merge of the filling algorithm to maintain auxilliary information about the coordinates. For example, we can, given a point, use this funnelsort to figure out its nearest neighbor.


\section{Orthogonal 2D Range Searching}
Given $N$ points and some rectangles, we'd like to return which points are in which rectangles.

\subsection{Batched}
The batched problem involves getting all $N$ points and $N$ rectangles first. We have all the information upfront, and we're also given a batch of queries and want to solve them all.

The optimal time is going to be $O(\frac{N}{B} \lg_{M/B} (\frac{N}{B}) + \frac{out}{B})$

\subsubsection{Algorithm}
First, count the number of rectangles containing each point (We need this to figure out what our buffer size needs to be in the funnel):
\begin{enumerate}
\item Sort the points and rectangle corners by $x$ using lazy funnelsort.
\item Divide and conquer on $x$, where we mergesort by $y$ and perform an upward sweepline algorithm. We have slabs $L$ and $R$, as seen in Figure \ref{fig:slabs}. 

\begin{figure}[h!]
  \begin{center}
    \scalebox{0.6}{\includegraphics{slabs.png}}
  \end{center}
  \caption{Left and Right slabs containing points and rectangles.}
  \label{fig:slabs}
\end{figure}

\item The problem of which points are in which rectangles are from when the rectangle completely spans one of the slabs, the rectangles in green in Figure \ref{fig:outerslabs}, but not when the points of the rectangle are within that slab. 
\begin{figure}[h!]
  \begin{center}
    \scalebox{0.6}{\includegraphics{outerslabs.png}}
  \end{center}
  \caption{ Slabs that also contain slabs that span either L or R.}
  \label{fig:outerslabs}
\end{figure}
\item Maintain the number of active rectangles (sliced by sweep line) that have a left corner in $L$ and that completely span $R$. Increment $C_L$ when we encounter a lower-left corner of a rectangle, and decrement when we encounter an upper-left corner.
\item Symmetrically maintain $C_R$ on rectangles which span slab $L$.
\item When we encounter a point in $L$, add $C_R$ to its counter. Similarly, add $C_L$ to the counter of any point in $R$.
\end{enumerate}

At this point, we can create buffers of the correct size. However, this is not optimal. The optimal solution is to carve the binary tree into linear size subtrees, and make each run in linear time.

\subsection{Online Orthogonal 2D range search 
 ~\cite{cite:lars2006} ~\cite{cite:lars2005}}

It is possible to preprocess a set of $N$ points in order to support
range queries while incurring only $O(\log_{B} N + \frac{out}{B})$ external
memory transfers.

We will consider three different kinds of range queries: 

\begin{itemize}
\item 2-sided range queries : $(\leq x_{q}, \leq y_{q})$ 
\item 3-sided range queries : $([x_{q_{min}}, x_{q_{max}}], \leq y_{q})$
\item 4-sided range queries : $([x_{q_{min}}, x_{q_{max}} ], [y_{q_{min}}, y_{q_{max}} ])$
\end{itemize}

\begin{figure}[h!]
  \begin{center}
    \scalebox{0.6}{\includegraphics{234sidedrangequeries.png}}
  \end{center}
  \caption{Depicts the three different types of range queries.}
  \label{fig:outerslabs}
\end{figure}

Only a small amount of extra space is needed to perform cache oblivious range
queries. The below table compares the space required to perform range queries
in the random access model, and the cache oblivious model.
 
\begin{center}
\begin{tabular}{|p{2.0cm}|p{2.0cm}|p{4.0cm}|}
  \hline
  Query Type & RAM & Cache Oblivious \\\hline
   2-sided& $O(N)$  & $O(N)$ \\ \hline
   3-sided& $O(N)$  & $O(N \lg N)$ \\ \hline
   4-sided& $O(N \frac{\lg N}{\lg \lg N})$  & $O(N \frac{\lg^{2}N}{\lg \lg N})$ 
    \\ \hline
%  \multicolumn{2}{l}{*This is the OFM Problem.}\\
\end{tabular}
\end{center}

\subsubsection{2-sided range queries}

First we will show how to construct a data structure that is
capable of performing 2-sided range queries while using only
linear space. Using this data structure, it will be possible
to construct data structures for the 3-sided and 4-sided cases
which have the desired space bounds.

At a high level, our data structure consists of a vEB BST containing
our $N$ points sorted on their $y$ coordinate. Each point in the tree
contains a pointer into a single array which has $O(N)$ size. This
array contains one or more copies of each of the $N$ points, and
is organized in a special way. To report the points satisfying a 
2-sided range query $(\leq x_{q}, \leq y_{q})$ we find the point in the tree 
and follow a pointer into the array, and then scan the array until
we encounter a point whose $x$ coordinate is greater than $x_{q}$. The
array's structure guarantees that we will only scan $O(out)$ points
and that the distinct points scanned are precisely those satisfying
the range query $(\leq x_{q}, \leq y_{q})$.

\begin{figure}[h!]
  \begin{center}
    \scalebox{0.6}{\includegraphics{highlevel2sidedrangequerystructure.png}}
  \end{center}
  \caption{A high level description of the data structure which
supports 2 sided range queries using only linear space.}
  \label{fig:outerslabs}
\end{figure}

We will proceed to describe the structure of the array and prove the
following claims:  

1. The high level procedure we described
 will find all points in $(\leq x_{q}, \leq y_{q})$.

2. The number of scanned points is $O(out)$.

3. The array has size $O(N)$.

\subsubsection*{First Attempt}
We will now outline two attempts, one unsuccessful, and one successful. We will begin by describing a simple structure for the array which
will satisfy claims 1 and 2, but fail to have linear size (claim 3). This
attempt will introduce concepts which will be useful in our second 
(and successful) attempt.

\begin{definition}
A range $(\leq x_{q}, \leq y_{q})$ is \textbf{dense} in an array $S$ if
\begin{equation*}
|\{(x,y) \in S : x<x_{q} \}| \leq 2 \cdot \text{\# points in } (\leq x_{q}, \leq y_{q})
\end{equation*}
\end{definition}

\begin{definition}
A range is \textbf{sparse} with respect to $S$ if it is not dense in $S$.
\end{definition}

Note that if a range is dense in an array $S$ and $S$ is sorted on $x$, then
we can report all points within that range by scanning through $S$. 
Since the range is dense in $S$, we will
scan no more than twice the number of points reported. 

Our strategy will be to construct an array $S_{0}S_{1}\ldots S_{k}$ so that 
for any query $(\leq x_{q}, \leq y_{q})$ there exists an $i$ for which that
query is dense in $S_{i}$. 

Consider the following structure:
 
Let $S_{0} = \text{ all points (sorted by  $x$  coordinate) } $

Let $y_{i} = \text{largest $y$ where some query $(\leq x_{q}, \leq y)$
 is sparse in $S_{i-1}$}$. 

Let $S_{i} = S_{i-1} \cap (*, \leq y_{i})$.

Then let our array be $S_{0}S_{1}\ldots S_{i}$.

Now consider the query $(\leq x_{q}, \leq y_{q})$. There is some $i$ for which
$y_{i} < y_{q}$, and for this $i$ the query will be dense in $S_{i-1}$. For
otherwise, $y_{q}$ would be a larger value of $y$ for which some query 
$(\leq x_{q}, \leq y)$ is sparse in $S_{i-1}$, contradicting the definition
of $y_{i}$. Now we can construct our vEB BST and have each node point to the
start of the subarray for which the corresponding query is dense. This 
data structure will allow us to find all points in a range while scanning only
$O(out)$ points (satisfying claims 1 and 2). However, the array 
$S_{0}S_{1}\ldots S_{k}$ may not have $O(N)$ size. Figure 7 shows an
example in which the array's size will be quadratic in $N$.

\begin{figure}[h!]
  \begin{center}
    \scalebox{0.6}{\includegraphics{quadraticexample.png}}
  \end{center}
  \caption{ An example showing how our first attempt may construct
an array with size quadratic in $N$.}
  \label{fig:outerslabs}
\end{figure}

\subsubsection*{Second (correct) Attempt}

Let $x_{i} =
\text{max $x$ coordinate where $(\leq x, \leq y_{i})$ is sparse in $S_{i-1}$}$.

Let $y_{i} = \text{largest $y$ where some query $(\leq x_{q}, \leq y)$
 is sparse in $S_{i-1}$}$. 

Let $P_{i-1} = S_{i-1} \cap (\leq x_{i} , *)$.

Let $S_{i} = S_{i-1} \cap ((*, \leq y_{i}) \cup (\geq x_{i}, *))$. 
(Note that this definition differs from that in our first attempt.)

Our array will now be $P_{0}P_{1}\ldots P_{i-1}S_{i} \ldots S_{k}$ 
(where $S_{j}$ has $O(1)$ size for $j$ between $i$ and $k$.) 

First, we show that the array is $O(N)$ in size. Notice that
$|P_{i-1} \cap S_{i}| \leq \frac{1}{2} |P_{i-1}|$ since
$(\leq x_{i}, \leq y_{i})$ is sparse in $S_{i-1}$. Charge
storing $P_{i-1}$ to $(P_{i-1} - S_{i})$. This results
in each point only being charged once by a factor of
$\frac{1}{1 - \frac{1}{2}} = 2$. Which implies that the total
space used is $\leq 2N$.

Next, notice that when an element is repeated in the array its
$x$ coordinate must be less than the $x$ coordinate of the
last seen point in the query. Therefore, by focusing on a
monotone sequence of $x$ coordinates we can avoid duplicates.

Finally, the total time spent scanning the array will be
 $O(out)$ because each point is repeated only $O(1)$ times. 
Therefore, this data structure can support $O(\log_{B} N + \frac{out}{B})$ 
2-sided range queries while using only $O(N)$ space.

\subsubsection{3-sided range queries}

Maintain a vEB search tree in which the leaves are points keyed
by $x$. Each internal node stores two 2-sided range query data structures
containing the points in that node's subtree. Since each point appears in
the 2-sided data structures of $O(\log N)$ internal nodes, the resulting
structure uses $O(N \log N)$ space. 

To perform the 3-sided range query $([x_{q_{min}},x_{q_{max}}], \leq y_{q})$,
we first find the least common ancestor of $x_{q_{min}}$ and $x_{q_{max}}$ 
in the tree. We then perform the query $(\geq x_{q_{min}}, \leq y_{q})$ in
the left child's structure 
and the query $(\leq x_{q_{max}}, \leq y_{q})$ in the right
child's structure. The union of the points reported will be the correct
answer to the 3-sided range query. 

OPEN: 3-sided range queries with $O(\log_{B} N + \frac{out}{B})$ queries
and $O(N)$ space.

\subsubsection{4-sided range queries}

Notice that we can easily achieve $O(\log_{B} N + \frac{out}{B})$ queries
if we allow ourselves to use $O(N \log^{2} N)$ space by constructing a vEB
tree keyed on $y$ which contains 3-sided range query structures.

However, it is possible to use only $O(N \frac{\log^{2} N}{\log \log N})$ space 
by storing each point in only $O(\frac{\log N}{\log \log N})$ 3-sided range
query structures.

Conceptually, we contract each $\frac{1}{2} \lg \lg N$ height subtrees
into $\sqrt{\lg N}$ degree nodes. This results in a tree of height
$O(\frac{\lg N}{\lg \lg N})$.

Now, as before, we store two 3-sided range query structures in each internal
node containing the points in their subtree. Furthermore, we store $\lg N$ static
search trees keyed by $x$ on points in each interval of the node's 
$\sqrt{\lg N}$ children.

To perform the query $([x_{q_{min}}, x_{q_{max}}], [y_{q_{min}}, y_{q_{max}}])$ 
we first find the least common ancestor of $y_{q_{min}}$ and $y_{q_{max}}$ in 
the tree. 
Then we perform the query $([x_{q_{min}}, q_{q_{max}}], \geq y_{q_{min}})$
 in the child node containing $y_{q_{min}}$, and the query
$([x_{q_{min}}, q_{q_{max}}], \leq y_{q_{min}})$ in the child containing 
$y_{q_{max}}$. Finally, use the search tree keyed on $x$ associated with
the interval between the child containing $y_{q_{min}}$ and the child 
containing $y_{q_{max}}$ to perform the query$([x_{q_{min}}, x_{q_{max}}], *)$
for all the in-between children at once. This data
structure supports $O(\log_{B} N + \frac{out}{B})$ 4-sided range
queries, while using only $O(N \frac{\log^{2}N}{\log \log N})$ space.

%\bibliography{mybib}
\bibliographystyle{alpha}

\begin{thebibliography}{77}

\bibitem{bf}
Gerth St\o lting Brodal and Rolf Fagerberg. Cache oblivious distribution
sweeping. \emph{In Proceedings of the 29th International Colloquium on Au-
tomata, Languages, and Programming}, volume 2380 of Lecture Notes in
Computer Science, pages 426-438, Malaga, Spain, July 2002.

\bibitem{cite:lars2006}
Lars Arge and Norbert Zeh. 2006. 
\emph{Simple and semi-dynamic structures for cache-oblivious planar orthogonal 
range searching.} In Proceedings of the twenty-second annual symposium on 
Computational geometry (SCG '06). ACM, New York, NY, USA, 158-166. 

\bibitem{cite:lars2005}
Lars Arge, Gerth Stølting Brodal, Rolf Fagerberg, and Morten Laustsen. 2005. 
\emph{Cache-oblivious planar orthogonal range searching and counting.} 
In Proceedings of the twenty-first annual symposium on Computational geometry 
(SCG '05). ACM, New York, NY, USA, 160-169. 


\end{thebibliography}
\end{document}

