\documentclass[t,12pt]{beamer}

\mode<presentation>
{
 % \usetheme{Antibes}
  \usetheme{Warsaw}
  \usefonttheme{serif}
}



\newcommand{\degree}{\ensuremath{^\circ}}
\definecolor{gray}{rgb}{0.8,.8,.8}


\title{Graphs III} 
\author{Minimum Spanning Trees (MST)}
\date{Laura Toma\\Algorithms (csci2200), Bowdoin College}


% If you wish to uncover everything in a step-wise fashion, uncomment
% the following command: 
%\beamerdefaultoverlayspecification{<+->}


\begin{document}

\begin{frame}
  \titlepage
\end{frame}


%\begin{frame}{Outline}
%  \tableofcontents
  % You might wish to add the option [pausesections]
%\end{frame}


\begin{frame}
  \frametitle{Minimum Spanning Tree (MST)}

  Problem: Given an \alert{undirected, weighted, connected} graph $G$,
  compute a spanning tree of minimum weight.

  
  \vfill 
  where 
  \begin{itemize} 
  \item Spanning tree: a subgraph of $G$ that is a tree and contains all vertices of $G$. 

  \item 
  The weight of a tree $T$ is the sum of the weights of its edges: $$
  w(T) = \sum_{(u,v) \in T} w_{u,v}$$ 
\end{itemize} 

Note: $G$ needs to be connected to admit a ST.  If not, first find its
CCs, and then find an MST for each component $\rightarrow$ minimum spanning forest (MSF).
\end{frame} 



\begin{frame}
  \frametitle{Minimum Spanning Tree (MST)}
  
  Two approaches, both greedy: 
  
  \pause
  \begin{itemize} 
  \item \alert{Kruskal's algorithm:}

    Start with an empty tree $T$. Consider the edges in $G$ in
    increasing order of weight.  Add edges to $T$ in order of weight
    unless doing so would create a cycle.

    \vfill
    \pause
  \item \alert{Prim's algorithm:}

    Start with an empty tree $T$. Greedily grow $T$ one edge at a
    time. At each step, add the edge of minimum weight that has
    exactly one endpoint in $T$.
  \end{itemize} 
\end{frame} 





\begin{frame}
  \frametitle{Prim's MST algorithm}
  
  Idea: 
\begin{itemize} 
\item Start with a tree $T$ containing an arbitrary vertex $r$ and  no edges
\item Grow $T$ by repeatedly adding minimum-weight edge connecting a
  vertex in the current $T$ with a vertex not in $T$
\end{itemize} 
\pause 
\vfill
Implementation: 
\begin{itemize} 
\item To find mimimum-weight edge connected to current $T$ we maintain
  a priority queue on vertices not in $T$.

\item The priority of a vertex is the weight of the mimimum-weight
  edge connecting $v$ to the tree.
\end{itemize} 
\end{frame} 






\begin{frame}
  \frametitle{Prim's MST algorithm}
  
  \begin{itemize}
  \item pick arbitrary vertex $r$
  \item Initialize: 
    
    For each $v \in V$, Insert(PQ, (v,$\infty$)). 
    
    Decrease-Key(PQ, r, 0).
  \item while PQ not empty do: 
    \begin{itemize}
    \item $u$ = Delete-Min(PQ)
    \item output the edge $(u, visit(u))$ as part of MST
    \item for each $(u,v) \in E$ do: 
      \begin{itemize}
      \item if $v \in PQ$ and $w_{u,v} < key(v)$ then
        
        ~~~~~~~~~~$visit(v) = u$
        
        ~~~~~~~~~~Decrease-Key(PQ, v, $w_{uv}$)
        
      \end{itemize} 
    \end{itemize} 
    \pause
    \vfill
    \alert{Analysis: $|V|$ Insert, $|V|$ Delete-Min, $|E|$ Decrease-Key}
    
    \pause
    \alert{$\rightarrow O(E \lg V)$ with a heap}
  \end{itemize} 
\end{frame} 



\begin{frame}
  \frametitle{Kruskal's MST algorithm}
  Idea: 
  \begin{itemize} 
  \item Start with $|V|$ trees, each consisting of one vertex and no edges. 
  \item Consider edges in $E$ in increasing order of weight; add an edge
    if it connects two trees
  \end{itemize} 
  \pause 
  \vfill
  \pause
  Implementation: 
  \begin{itemize} 
  \item sort $E$ by weight
    
  \item How to decide  if an edge $(u,v)$ creates a cycle, or connects two trees? 
    
    \pause
    Comes down to checking if vertices $u,v$ are in the same tree, or not. 
  \end{itemize} 
\end{frame} 



\begin{frame}
  \frametitle{Kruskal's MST algorithm}
  
  \begin{itemize}
  \item Initialize: $T$ consists of all vertices, and no edges
  \item Sort $E$ by weight
  \item For each edge $(u,v)$ in order do: 
    \begin{itemize}
    \item if $u,v$ in the same ``tree'' (i.e. connected in $T$): skip 
    \item else: add edge $(u,v)$ to $T$
    \end{itemize} 
  \end{itemize} 
  \pause
  How to check if $u,v$ are in the same CC of $T$? 
\end{frame} 


\begin{frame}
  \frametitle{Kruskal's MST algorithm}
  
  How to check if $u,v$ are in the same CC of $T$? 
  
  \pause 
  Ideas: \\
Could run BFS/DFS  on $T$ every time.... too slow. \\
Or...

\pause 
\vfill
We  need a data structure that supports: 
\begin{itemize} 
\item Make-Set($v$): create set containing $v$
\item Union-Set($u,v$): unite set containing $u$ and set containing $v$
\item Find-Set($v$): return unique representative for set containing $v$
\end{itemize} 
Called \alert{Union-Find} data structure
\end{frame}


\begin{frame}
  \frametitle{Kruskal's MST algorithm}
  
  \begin{itemize}
  \item Initialize: \\
    ~~~~~$T$ consists of all vertices, and no edges\\
    ~~~~~For each $v \in V$, Make-Set($v$)
  \item Sort $E$ by weight
  \item For each edge $(u,v)$ in order do: 
    \begin{itemize}
    \item if Find-Set($u$) $\neq$ Find-Set($v$): \\
      ~~~~~add edge $(u,v)$ to $T$\\
      ~~~~~Union-Set($u,v$)
    \end{itemize} 
  \end{itemize} 
\pause
\alert{Analysis: $E \lg E$ to sort; $|V|$ Make-Set,  $2|E|$ Find-Set,
  $|V|-1$  Union-Set }
\end{frame} 



\begin{frame}
  \frametitle{A Union-Find structure}
 \begin{itemize} 
\item Make-Set($v$): create set containing $v$
\item Union-Set($u,v$): unite set containing $u$ and set containing $v$
\item Find-Set($v$): return unique representative for set containing $v$
\end{itemize} 
\pause
\vfill
Simple solution: \\
\pause
Maintain elements in the same set in a linked list with each element
having a pointer to the first element in the list (unique representative)\\

\pause
\vfill
Find-Set($u$) runs in $O(1)$ time, but Union-Set($u,v$) needs $O(|V|)$
time. \\
\pause
$\Rightarrow$ Kruskal's algorithm runs in $O(E \lg V + V^2)$. Too
slow. 
\end{frame} 



\begin{frame}
  \frametitle{A Union-Find structure}
Supports: 
 \begin{itemize} 
\item Make-Set($v$), Union-Set($u,v$), Find-Set($v$)
\end{itemize} 
\vfill 
Refined solution: Maintain elements in the same set in a linked
list with each element having a pointer to the first element in the
list (unique representative), and \alert{in a union-set, always link
  the smaller list after the longer list}.

\pause
\vfill
Analysis: We'll count the total nb of pointers that are changed in \alert{all}
calls to Union-Set().   Assume we start with $|V|$ sets. 
\pause
How many times can a pointer of an element change?  
\pause
Every time an element changes its pointer, it belongs to a set of at
least \alert{double the size} than what it was before. 
\pause
Thus, the pointer of an element can change at  most $\lg V$ times. 
\pause
Overall,  $|V|-1$ Union-Set() calls will run in $O(V \lg V)$ time.
\end{frame} 




\begin{frame}
  \frametitle{ Union-Find  structure}

Actually, a much better bound for a Union-Find structure can be
obtained: 

\begin{itemize} 
\item use rooted trees (instead of lists)
\item the representative of the set containing $u$: the root of the
  tree that contains $u$
\item Find-Set($u$): go up the path from $u$ to its root
\item at Union-Set: connect the smaller tree as a child of the larger tree
\item at Find-set:  link all nodes on the path to the root as children
  of the root. 
\end{itemize} 
Can be shown $|V|$ operations run in  $O(V \alpha(V|)$ time, which is
practically $O(|V|)$ time. 
\end{frame} 


\begin{frame}
  \frametitle{MST algorithms}
Correctness: 
\begin{theorem} 
Let $V_1, V_2$ be a partition of $V$ into two disjoint sets, $V_1 \cup
V_2 = V$.  Consider all edges with one endpoint in $V_1$ and another
one in $V_2$.   Then there is a MST that includes the minimum-weight
such edge $e$.
\end{theorem}
\pause
\begin{proof} 
  Let $T$ be an MST of $G$.  % Note $T$ consists of parts that are in $V_1$ and
  % parts that are in $V_2$ and edges that cross $V_1$ and $V_2$.
  Assume by contradiction $T$ does not include $e$, then adding $e$ to
  $T$ creates a cycle. There must be another edge on this cycle that
  has one endpoint in $V_1$ and one in $V_2$.  It has weights $\ge e$.
  Remove it from $T$ and add $e$ instead; this gives a ST of weight
  $\le$ than before --- contradiction.
\end{proof} 
\end{frame} 



\begin{frame}
  \frametitle{MST algorithms}
\begin{theorem} 
Let $V_1, V_2$ be a partition of $V$ into two disjoint sets, $V_1 \cup
V_2 = V$.  Consider all edges with one endpoint in $V_1$ and another
one in $V_2$.   Then there is a MST that includes the minimum-weight
such edge $e$.
\end{theorem}

Correctness: 

Argue that Prim's and Kruskal's algorithms are correct by using the
theorem, and chosing the partition appropriately.

\end{frame} 

\end{document}


