\documentclass{projector}

\style{hidden=90,highlight.shaded=true}
\squeeze

\usepackage{arev}



\newtheoremframe{theorem}{Theorem}
\newtheoremframe{lemma}{Lemma}
\newtheoremframe{definition}{Definition}

\newcommand{\CondGraphic}[2][1]{%
   \centerbox{\graphicbox[scale=#1]{#2}}%
}

\newcommand{\deq}{\mathrel{\mathop:}=}

\newcommand{\cF}{\mathcal{F}}
\newcommand{\cI}{\mathcal{I}}



\newcommand{\MakeSet}{\mathit{MakeSet}}
\newcommand{\Union}{\mathit{Union}}
\newcommand{\Find}{\mathit{Find}}

\newcommand{\Op}{\mathit{Op}}


\begin{document}

\begin{slide}  

\vfill


\begin{center} 
Honors Algorithms \\ G22.3520-001 Fall 2007 \\ \ \\
Lecture 13 \\
\end{center}



\vfill


\end{slide}


\begin{slide} 
\Stripe{Union/Find with ``up trees''}



\pause
Each set is implemented  as a tree

\pause
Every node in the tree, other than the root,
has a pointer ``up'' to its parent

\pause
The representative of a set is the root of its tree

\pause
Find: follow pointers to the root

\pause
Union:
Merge one root into the other root



\end{slide}


\begin{slide}
Example of Union:
\pause
\begin{center}
\CondGraphic[0.80]{fig1}
\end{center}

\pause
Assume $n$ items and $m$ operations

\pause
Worst case: $m n$ --- trees may degenerate into lists

\pause
Two simple ideas: {\alert size balancing} and {\alert path compression} 
\end{slide}


\begin{slide}
\banner{Size balancing}


\begin{highlight}[Size balancing rule]
In a Union operation, always merge the smaller tree
into the larger tree
\end{highlight}

\pause 

\begin{lemma}[1]
If $T$ is a tree created by balanced merges,
and $T$ has size $n$ and height $h$,
then $n \ge 2^h$
\end{lemma}

\end{slide}

\begin{slide} 
{\em Proof:} induction on $n$.


\pause
Assume $T$ was obtained by merging $T_1$ into $T_2$
\begin{center}
\CondGraphic[0.75]{fig2}
\end{center}
where
$n_1 \deq \mathit{Size}(T_1) \le n_2 \deq \mathit{Size}(T_2)$

\pause
Let 
$h_i \deq \mathit{Height}(T_i)$ for $i=1,2$

\pause
By induction, $n_1 \ge 2^{h_1}$ and $n_2 \ge 2^{h_2}$

\pause
If $h_1 \ge h_2$, then $h = h_1 + 1$ and
\[
n = n_1 + n_2 \ge 2 n_1 \ge 2 \cdot 2^{h_1} = 2^h
\]
\pause
If $h_1 < h_2$, then $h = h_2$ and
\[
n = n_1 + n_2 \ge n_2 \ge 2^{h_2} = 2^h  \ \ \ \text{QED}
\]



\end{slide}

\begin{slide}
\banner{Path compression}

\begin{highlight}[Path compression rule]
After each Find operation, make all nodes visited
point to the root of the tree
\end{highlight}

\pause
\begin{center}
\CondGraphic[0.9]{fig3}
\end{center}


\end{slide}

\begin{slide}
\banner{Running time analysis}


\pause
For $g \ge 0$, define
\[
\left.F(g) \deq 2^{2^{2^{\text{\rotatebox{30}{$\cdots$}}^2}}} \right\} 
\ \ g \ \text{2's}
\]

\pause
Formally, $F(0) \deq 1$ and $F(g+1) \deq 2^{F(g)}$

\pause
Define
$\log^* r  \deq \text{least $g$ such that $F(g) \ge r$}$

\pause
\begin{theorem}
With size balancing and path compression,
any sequence of $m$ union/find operations on $n$ items
takes time $O((m + n) \log^* n)$
\end{theorem}

\end{slide}

\begin{slide}


\begin{center}
\CondGraphic[0.5]{fig5} \\[0.75em]
\pause $R_g \deq (\log^*)^{-1}(g) = \{ r : \log^* r = g \}$ \\
\pause $\log^*r \le g \Leftrightarrow r \le F(g)$ \\
\pause $R_0 = \{0, 1\}$, $R_{g} = \{ F(g-1) + 1, \ldots, F(g) \}$ for $g >0$
\end{center}




\end{slide}

\begin{slide}
Let $\Op_1, \ldots, \Op_m$ be a sequence of union/find
operations

\pause
Consider the forest of trees {\alert $\cF$} that results after executing
$\Op_1, \ldots, \Op_m$ with size balancing, but {\alert no}
path compression

\pause
Define the {\alert rank} of a node $v$
to be its height in $\cF$


\begin{center}
\CondGraphic[0.75]{fig4}
\ \ \centerbox{\parbox{0.5\textwidth}{rank is a static quantity --
it does not change over time}}
\end{center}

\end{slide}




\begin{slide}
\begin{lemma}[2]
For every 
$r \ge 0$, there are
at most $n/2^r$ nodes of rank $r$
\end{lemma}


\pause
Proof:
\begin{itemize}
\pitem
By Lemma~1, any node of rank $r$ is the root of a subtree in $\cF$
of size $\ge 2^r$

\pitem
Any two distinct nodes of rank $r$ are roots of {\alert disjoint} subtrees
in $\cF$

\pitem
Therefore, there can be at most $n/2^r$ nodes of rank $r$

\pitem
QED
\end{itemize}


\end{slide}

\begin{slide}

\begin{lemma}[3]
Suppose that at some time during the execution
of $\Op_1, \ldots, \Op_m$ with compression,
$v$ is a (strict) descendent of $w$.
Then $\mathit{Rank}(v) < \mathit{Rank}(w)$
\end{lemma}


\pause
Proof:
\begin{itemize}
\pitem
Key observations:
\pitem
path compression only eliminates descendency relations --
it never creates any new ones

\pitem
with no path compression, union/find operations
never destroy descendency relations
\end{itemize}

\end{slide}

\begin{slide}
Prove by induction on $i$:
\begin{itemize}
\pitem
for all $v, w$, if $v$ is a descendent of $w$
after executing $\Op_1, \ldots, \Op_i$ with compression,
then $v$ is a descendent of $w$ after executing
$\Op_1, \ldots, \Op_i$ without compression
\end{itemize}


\pause
Thus, 
if $v$ is a descendent of $w$ at some point in time
during the execution of $\Op_1, \ldots, \Op_m$ with compression,
then  $v$ is a descendent of $w$ in $\cF$,
and hence $\mathit{Rank}(v) < \mathit{Rank}(w)$  \pause -- QED


\pause
\begin{definition}
For a node $v$, we define its {\alert group}
as $G(v) \deq \log^* \mathit{Rank}(v)$
\end{definition}

\pause
Clearly, $G(v) \le \log^* n$

\end{slide}


\begin{slide}
{\em Proof of Theorem}

\pause
Union operations take $O(1)$, so we can focus on find
operations

\pause
Let $\cI$ be the set of indices $i$ such that $\Op_i$ is a
find operation

\pause
Consider a fixed $i \in \cI$, with $\Op_i = \text{``$\Find(v)$''}$

\pause
Consider the path from $v$ to the root: 
\[
 v = {\hideAt{\p}\underbrace{{\showAt{\p}v_1, v_2, \ldots, v_{k-2}}}_{\text{moved nodes}}}, v_{k-1}, v_k = \text{root}
\]
\pause
\pause
By Lemma~3, we have
\begin{gather*}
\mathit{Rank}(v_1) <  \mathit{Rank}(v_2) < \cdots < \mathit{Rank}(v_k) \\
\pause G(v_1) \le G(v_2) \le \cdots \le G(v_k)
\end{gather*}


\end{slide}


\begin{slide}
Let $X_i =  \{ v_1, \ldots, v_k \}$

\pause
$C \deq \sum_{i \in \cI} |X_i|$ is the cost of all the find
operations

\pause
Let's split $X_i$ into 3 sets:
\begin{itemize}
\pitem
$Y_i \deq \{ v_j : j < k-1 \ \text{and} \ G(v_j) = G(v_{j+1}) \}$

\pitem
$Z_i \deq \{ v_j : j < k-1 \ \text{and} \ G(v_j) < G(v_{j+1})  \}$

\pitem
$W_i \deq \{ v_j : j \ge k-1 \}$

\end{itemize}

\pause
We have
\begin{itemize}
\pitem
$|Z_i| \le G(v_k) \le \log^* n$ 

\pitem
$|W_i| \le 2$
\end{itemize}

\end{slide}

\begin{slide} 
So we have
\begin{align*}
C  & =  \sum_{i\in\cI} ( |Y_i| + |Z_i| + |W_i| ) \\
 & \pause \le  \sum_{i\in \cI} |Y_i| +  m \log^* n + 2 m
\end{align*}

\pause
{\bf Claim:} $C' \deq \sum_i |Y_i| \le  n \log^* n$


\pause
Idea:
Consider a fixed node $v$
\begin{itemize}
\pitem
Each time $v$ moves during a path compression,
$v$'s new parent has a higher rank than $v$'s old parent

\pitem
If $G(v) = g$,
then
after $|R_g|-1$ moves, $v$
must acquire a parent whose group is $> g$

\end{itemize}


\end{slide}


\begin{slide}
For $g \ge 0$, let $V_g \deq \{ v : G(v) = g\}$

\pause
We have
\begin{align*}
C' & \le \sum_{g = 0}^{\log^* n}  |V_g| \cdot (|R_g| - 1) \\ 
 &  \le n + \sum_{g = 2}^{\log^* n}  |V_g| |R_g| \\ 
\end{align*}

\pause
To prove the claim, it will suffice to show that
\[
|V_g| |R_g | \le n
\]
for $g > 0$
(we may assume $n > 1$ and so $\log^*n > 0$)
\end{slide}

\begin{slide}
For $g > 0$, we have
\begin{align*}
\pause R_g & \refresh = \{ F(g-1) + 1, \ldots, F(g) \} \\
\pause |V_g| & \refresh \le \sum_{r \in R_g} n/2^r \ \ \text{(by Lemma~2)} \\
  & \pause =  \frac{n}{2^{F(g-1)+1}} \sum_{j=0}^{F(g)-F(g-1)-1} 1/2^j  \\
  & \pause \le \frac{n}{2^{F(g-1)}} \pause = \frac{n}{F(g)} 
\end{align*}

\pause
Therefore,
\[
\pause  |V_g| |R_g|  \refresh \le \frac{n}{F(g)} \cdot |R_g|
   \pause \le \frac{n}{F(g)} \cdot F(g) \pause = n.
\]

\pause
{\Large QED}


\end{slide}

\end{document}
