\documentclass[11pt]{article}
\usepackage{amsmath,amssymb,amsthm}
\usepackage{xcolor}

\DeclareMathOperator*{\E}{\mathbb{E}}
\let\Pr\relax
\DeclareMathOperator*{\Pr}{\mathbb{P}}

\newcommand{\eps}{\epsilon}
\newcommand{\inprod}[1]{\left\langle #1 \right\rangle}
\newcommand{\R}{\mathbb{R}}
\newcommand{\twonorm}[1]{\|#1\|_2}
\newcommand{\floor}[1]{\lfloor #1 \rfloor}

\newcommand{\handout}[5]{
  \noindent
  \begin{center}
  \framebox{
    \vbox{
      \hbox to 5.78in { {\bf CS 388R: Randomized Algorithms, Fall 2019 } \hfill #2 }
      \vspace{4mm}
      \hbox to 5.78in { {\Large \hfill #5  \hfill} }
      \vspace{2mm}
      \hbox to 5.78in { {\em #3 \hfill #4} }
      \textcolor{red}{\textbf{NOTE:} THESE NOTES HAVE NOT BEEN EDITED OR CHECKED FOR CORRECTNESS}
    }
  }
  \end{center}
  \vspace*{4mm}
}

\newcommand{\lecture}[5]{\handout{XX}{#3}{#4}{Scribe: #5}{Lecture #1: #2}}

\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

\begin{document}

\lecture{25}{Random Walk on Undirected Graphs; Closest Pair in Plane}{November 21}{Prof.\ Eric Price}{Hongru Yang}
% Example:
%\lecture{1}{Coupon Collector; Balls and Bins}{August 29th, 2019}{Prof.\ Eric Price}{John Doe, Jane Roe}

\section{Markov Chain; Random Walk on Undirected Graphs (Continued)}
Hitting time:
\begin{align*}
    h_{uv} = \E[\text{time to reach } v \text{ starting at }u]
\end{align*}
Commute time:
\begin{align*}
    C_{uv} = h_{uv} + h_{vu} = 2m R_{\text{eff}}(u,v)
\end{align*}
Cover time:
\begin{align*}
    C_u(G) &= \E[\text{time to visit all vertices starting at }u] \\
    C(G) &= \max_u C_u(G) \leq 2m(n-1)
\end{align*}

\begin{theorem}
$mR_{\text{max}} \leq C(G) \leq O(1)mR_{\text{max}}\log n$, where $R_{\text{max}} = \max_{u,v} R_{\text{eff}}(u, v)$.
\end{theorem}
\begin{proof}
Lower bound: $C(G) = \max_u C_u(G) \geq \max_u \max_v h_{uv} \geq \max_{u,v} \frac{C_{uv}}{2} = m R_{\text{max}}$.

Upper bound:
Suppose starting at $u$, 
\begin{align*}
    \Pr[\text{haven't visited }v \text{ by time }t] \leq \frac{h_{uv}}{t} \leq \frac{C_{uv}}{t} = \frac{2m R_{\text{eff}}(u,v)}{t} \leq \frac{2m R_{\text{max}}}{t}
\end{align*}
If we take $t = 4mR_{\text{max}}$, by Markov's inequality, we have $\Pr[\text{haven't visited }v \text{ by time }t] \leq \frac{1}{2}$. However, observe that the above proof holds for any starting $u$. Thus, we have
\begin{align*}
    \Pr[\text{haven't visited }v \text{ between time }[t+1,2t]\text{ regardless of state at }t] \leq \frac{1}{2}
\end{align*}
which implies
\begin{align*}
    \Pr[v \text{ not reached by } t 2\log n] \leq \frac{1}{2^{2\log n}} &= \frac{1}{n^2} \\
    \Pr[\text{not cover the entire graph by } t2\log n] &\leq \frac{1}{n^2}
\end{align*}
\end{proof}

\subsection*{Application}
Check $s-t$ connectivity on an undirected graph in $O(\log n)$ space. $2mn$ steps per $(s,t)$ pair. $O(\log n)$ space to tell where I am and keep track of how many steps performed.

\section{Closest Pair in Plane}
Suppose we have $n$ points in plane, $P = \{p_1, p_2, \ldots, p_n\}$ and we want to find $CP(P) = \min_{i \neq j} \twonorm{p_i - p_j}$. Naively we can do this in $O(n^2)$. We are going to show a deterministic algorithm that runs in $O(n \log n)$ time and a randomized algorithm that runs in expectation $O(n)$ time. 

\subsection*{Deterministic Closest Pair in Plane}
\begin{enumerate}
    \item Recursively split all the points by its $x$ coordinate. Then $P = L \cup R$ and $|L| = |R| = \frac{n}{2}$. Solve $CP(L), CP(R)$ and let $\sigma = \min(CP(L), CP(R))$.
    \item Find the minimum distance $\delta$ of pairs across boundary $L \leftrightarrow R$.
    \item Return $\min(\delta, \sigma)$.
\end{enumerate}{}

To do step 2 in $O(n)$ time, first notice that we only need to go through all the points that are within $\sigma$ distance to the left and right of the splitting boundary. Further, we can divide the region into squares of size $\sigma \times \sigma$ by $y$ axis. Inside each square, there can be at most $4$ points. Therefore, for a given point that is to the left of the boundary, we only need to look at the 3 adjacent box to the right of the boundary and there are at most $8$ points.

To achieve above, we use $\floor{\frac{x}{\sigma}}, \floor{\frac{y}{\sigma}}$ as key to store points into a hash table. So, we can query all the points in a square in $O(1)$ time. 

\subsection*{Randomized Closest Pair in Plane}
Suppose we have correct $\sigma$ beforehand, we can split the plane into $\sigma \times \sigma$ small squares and all squares have at most 4 points inside. Then the closest pair lies in adjacent cells. We can then scan through all points and for each point we check all adjacent cells and then add the point into the hash table.

New goal: given a guess $\sigma$, is $\sigma_{\text{true}} > \sigma, \sigma_{\text{true}} = \sigma, \sigma_{\text{true}} < \sigma$?

We can run the algorithm and if we have more than 5 points in a square then $\sigma_{\text{true}}<\sigma$.

New algorithm:

\begin{enumerate}
    \item Randomly assign order to all the points and we denote the first $i$ points in this order $P_i$. 
    \item Scan through all the points in this order. For each point $p_i$,
    \begin{itemize}
        \item Find $CP(P_i)$.
        \item If $CP(P_i) < \sigma$, rebuild the hash table of $P_i$ with $\sigma = CP(P_i)$.
        \item Else add $p_i$ to the hash table. 
    \end{itemize}
    \item Return $\sigma$
\end{enumerate}

Notice that we can compute $CP(P_i)$ in constant time since all the points in the hash table are at least $\sigma$ distance apart (and thus each square has at most 4 points) and we can query all the points that are in adjacent cells of $p_i$ in constant time. 

The running time of this algorithm in expectation is $\sum_{i=1}^n 1 + \Pr[\text{need to rebuild for }p_i]i = 3n$ since $\Pr[\text{need to rebuild the hash table for }p_i] = \Pr[\text{minimum distance among }P_i\text{ uses }p_i] = \frac{i-1}{{i \choose 2}} = \frac{2}{i}$. 

\section{Introduction to Point Location in Regions Split by Lines in General Location}
If we have a set of $n$ lines $L$ in general location, we will have ${n \choose 2}+1 = \Theta(n^2)$ regions. We can prove this by observing that whenever we add a new line we will create total number of lines more regions. 

\begin{definition}
The \textbf{triangulation} of $L$ is defined by adding line segment between intersections of lines in $L$ to split multi-sided regions so that each divided region is a triangle. (We assume parallel lines intersecting each other at infinity.)
\end{definition}

It can be shown that there are $O(n^2)$ triangles in a triangulation. 

We assign labels to all the regions and we want to output label of a given point.

Our goal is to create a data structure with $O(n^2)$ time and space complexity beforehand so that we can do fast query later.

\subsection*{A Related Problem: R-ary Tree}
R-ary can be created by first randomly sample $R$ points from a set of points associated with one node. Then these points define a splitting of all the points. Create children of the node based on the splitting. 

Then, if for a given node we have $r$ branches,
\begin{align*}
    \E[\text{size of a given branch}] = \frac{n}{r}
\end{align*}
By Chernoff's bound this implies,
\begin{align*}
    \Pr[\text{size of a given branch} \geq \frac{n}{r} \log r] \leq e^{-\log r} = \frac{1}{r}
\end{align*}
which implies w.p. $3/4$, the maximum branch size is at most $\frac{n\log r}{r}$.

Thus the expected query time can be bounded by $Q(n)\leq O(r) + Q(\frac{n \log r}{r}) \leq O(1) + Q(n/10) = O(\log n)$ if we take $r = 10$.


% \section{Math things}
% Here is an equation:
% \begin{equation*}
% a^2 + b^2 = c^2 ,
% \end{equation*}
% or
% $$
% a^2 + b^2 = c^2 .
% $$

% Here's the same equation but that can be referenced:
% \begin{equation}
% a^2 + b^2 + c^2 \label{eqn:pythagoras}
% \end{equation}
% Equation~\ref{eqn:pythagoras} has been known for a long
% time. Sometimes you might want to align equations:
% \begin{align*}
% (x+y)^2 - (x-y)^2 &= (x^2 + 2xy + y^2) - (x^2 - 2xy - y^2) \\
% {}&= 4xy
% \end{align*}
% or align them with numbers to be referenced:
% \begin{align}
% (x+y)^2 - (x-y)^2 &= (x^2 + 2xy + y^2) - (x^2 - 2xy - y^2) \label{eqn:1}\\
% {}&= 4xy \label{eqn:2}
% \end{align}
% or where only some of them can be easily referenced:
% \begin{align}
% \nonumber (x+y)^2 - (x-y)^2 &= (x^2 + 2xy + y^2) - (x^2 - 2xy - y^2) \\
% {}&= 4xy \label{eqn:3}
% \end{align}

% Here are some other math goodies: $\binom{n}{k}$, $\frac{n}{k}$, $x\gg
% y$, $x\ll y$, $x\lesssim y$, $x\gtrsim y$, $\inprod{x,y}$, $x\in\R^n$,
% $x\in S$, $S\ni x$, $x\notin S$, $x\neq y$, $x\oplus y$, $x\pm y$,
% $\sqrt{x}$, $x\equiv y$, $S\cap T$, $\S\cup T$, $S\subset T$,
% $S\subseteq T$, $S\supset T$, $\gamma$, $\Gamma$, $\R$, $\mathbb{F}$
% $$\sum_{i=1}^n i,\ \sum_{\substack{i=1\\i\ odd}}^n i, \prod_{i=1}^n i$$

% \section{Main Section}

% We begin by describing the problem \ldots.
% Make sure to use sections and subsections.

% \subsection{Blah blah blah}
% Here is a subsection.

% \subsubsection{Blah blah blah}
% Here is a subsubsection. You can use these as well.

% \subsection{Using Boldface}
% Make sure to use \textbf{lots} of {\bf boldface}.

% \paragraph{Question:}
% How would you use boldface?

% \paragraph{Example:}
% This is an example showing how to use boldface to help organize your
% lectures.


% \paragraph{Some Formatting.}
% Here is some formatting that you can use in your notes:
% \begin{itemize}
% \item {\em Item One} -- This is the first item.
% \item {\em Item Two} -- This is the second item.
% \item \dots and here are other items.
% \end{itemize}

% If you need to number things, you can use this style:
% \begin{enumerate}
% \item {\em Item One} -- Again, this is the first item.
% \item {\em Item Two} -- Again, this is the second item.
% \item \dots and here are other items.
% \end{enumerate}

% \paragraph{Bibliography.}
% Please give real bibliographical citations for the papers that we
% mention in class. See below for how to include a bibliography section.
% If you use BibTeX, integrate the .bbl file into your .tex source. You
% should reference papers like this: ``The tug of war sketch originates
% in a paper by Alon, Matias and Szegedy \cite{AMS99}.''  In general,
% the name of the authors should appear in text at most once (for the
% first citation); further citations look like: ``Our proof follows that
% of \cite{AMS99}''.

% Take a look at previous lectures (TeX files are available) to see the
% details. A excellent source for bibliographical citations is
% DBLP. Just Google DBLP and an author's name.



% \bibliographystyle{alpha}

% \begin{thebibliography}{42}

% \bibitem[AMS99]{AMS99}
% Noga~Alon, Yossi~Matias, Mario~Szegedy.
% \newblock The Space Complexity of Approximating the Frequency Moments.
% \newblock {\em J. Comput. Syst. Sci.}, 58(1):137--147, 1999.

% \end{thebibliography}

\end{document}