\documentclass[t,aspectratio=1610]{beamer}
\input{algo-lecture}
\usepackage{pifont}
%======================== Document Info =======================
\title{Minimal Spanning Trees}
\author{Antonio Carzaniga}
\institute{Faculty of Informatics\\Universit\`a della Svizzera italiana}
\date{May 12, 2025}
\newcommand{\insertcopyright}{\copyright\ 2007\quad Antonio Carzaniga}

\begin{document}
\begin{frame}% Titleslide
  \maketitle
\end{frame}

\begin{frame}\frametitle{Outline}
  \begin{itemize}
  \item MST problem

    \bigskip
  \item Generic algorithm

    \bigskip
  \item Prim and Kruskal

  \end{itemize}
\end{frame}

\begin{frame}\frametitle{Minimum Spanning Tree}
  \begin{itemize}
  \item Given a weighted graph $G=(V,E)$
    \begin{itemize}
      \smallskip
    \item with ``weight'' function $w:E\to\mathbb{R}$
    \end{itemize}

    \bigskip
    \pause
  \item Find an \emph{acyclic} subset $T\subseteq E$
    \begin{itemize}
      \smallskip
    \item a \alert{tree}
    \end{itemize}

    \bigskip
    \pause
  \item $T$ ``spans'' the entire graph $G$ (i.e., touches all vertexes)
    \begin{itemize}
      \smallskip
    \item a \alert{spanning tree}
    \end{itemize}

    \bigskip
    \pause
  \item $T$'s total weight of the tree is minimal
    $$w(T)=\sum_{(u,v)\in T}{w(u,v)}$$

    \begin{itemize}
    \item a \alert{minimum-weight spanning tree}, or ``minimum
      spanning tree''
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}\frametitle{Example}
  \begin{center}
    \begin{pspicture}(0,0)(6,4)
      \cnode(0,0){.3}{n1}\rput(0,0){$a$}
      \cnode(2,0){.3}{n2}\rput(2,0){$b$}
      \cnode(4,0){.3}{n3}\rput(4,0){$c$}
      \cnode(6,0){.3}{n4}\rput(6,0){$d$}
      \cnode(0,2){.3}{n5}\rput(0,2){$e$}
      \cnode(2,2){.3}{n6}\rput(2,2){$f$}
      \cnode(4,2){.3}{n7}\rput(4,2){$g$}
      \cnode(6,2){.3}{n8}\rput(6,2){$h$}
      \cnode(0,4){.3}{n9}\rput(0,4){$i$}
      \cnode(2,4){.3}{n10}\rput(2,4){$j$}
      \cnode(4,4){.3}{n11}\rput(4,4){$k$}
      \cnode(6,4){.3}{n12}\rput(6,4){$m$}
      % 
      \ncline{n2}{n7}\aput[2pt](.5){\small\sf 3}
      \ncline{n3}{n4}\aput[2pt](.5){\small\sf 2}
      \ncline{n3}{n7}\aput[2pt](.5){\small\sf 3}
      \ncline{n6}{n9}\aput[2pt](.5){\small\sf 1}
      \ncline{n7}{n10}\aput[2pt](.5){\small\sf 2}
      \ncline{n8}{n11}\aput[2pt](.5){\small\sf 3}
      \ncline{n8}{n12}\aput[2pt](.5){\small\sf 3}
      \ncline{n1}{n5}\aput[2pt](.5){\small\sf 1}
      \ncline{n1}{n6}\aput[2pt](.5){\small\sf 2}
      \ncline{n6}{n2}\aput[2pt](.5){\small\sf 1}
      \ncline{n2}{n3}\aput[2pt](.5){\small\sf 1}
      \ncline{n7}{n4}\aput[2pt](.5){\small\sf 1}
      \ncline{n5}{n9}\aput[2pt](.5){\small\sf 1}
      \ncline{n6}{n7}\aput[2pt](.5){\small\sf 3}
      \ncline{n7}{n8}\aput[2pt](.5){\small\sf 1}
      \ncline{n7}{n11}\aput[2pt](.5){\small\sf 3}
      \ncline{n11}{n12}\aput[2pt](.5){\small\sf 1}
      \ncline{n9}{n10}\aput[2pt](.5){\small\sf 3}
      \ncline{n4}{n8}\aput[2pt](.5){\small\sf 2}
      % 
      \only<2->{%
        \ncline[linewidth=3pt,linecolor=blue]{n1}{n5}
        \ncline[linewidth=3pt,linecolor=blue]{n5}{n9}
        \ncline[linewidth=3pt,linecolor=blue]{n9}{n6}
        \ncline[linewidth=3pt,linecolor=blue]{n6}{n2}
        \ncline[linewidth=3pt,linecolor=blue]{n2}{n3}
        \ncline[linewidth=3pt,linecolor=blue]{n3}{n4}
        \ncline[linewidth=3pt,linecolor=blue]{n7}{n10}
        \ncline[linewidth=3pt,linecolor=blue]{n4}{n7}
        \ncline[linewidth=3pt,linecolor=blue]{n7}{n8}
        \ncline[linewidth=3pt,linecolor=blue]{n11}{n12}}%
      \only<2-3>{%
        \ncline[linewidth=3pt,linecolor=blue]{n8}{n11}}%
      \only<4>{%
        \ncline[linewidth=3pt,linecolor=blue]{n8}{n12}}%
    \end{pspicture}
  \end{center}

  \bigskip
  \begin{itemize}
  \item<3-> There may be more than one minimum spanning tree
  \end{itemize}
\end{frame}

\begin{frame}\frametitle{General Idea}
  \begin{itemize}
    \pause
  \item Build $T$ by adding one edge at a time

    \bigskip
    \pause
  \item Each time, add $e\in E$ such that

    \begin{itemize}
      \smallskip
    \item $e$ is the \alert{lightest edge}

      \smallskip
    \item $e$ \alert{does not create a cycle}

    \end{itemize}

    \bigskip
  \item Stop when we have covered all vertexes

    \bigskip
    \pause
  \item This is a \emph{``greedy'' algorithm}

    \bigskip
    \pause
  \item Does it work?
  \end{itemize}
\end{frame}

\begin{frame}\frametitle{Refined Idea}
  \begin{center}
    \algobox{%
    \begin{codebox}
        \Procname{$\proc{Generic-MST}(G,w)$}
        \li $A\gets\emptyset$
        \li \While $A$ is not a spanning tree
        \li \Do find a \emph{safe} edge $e=(u,v)$ 
        \li     $A\gets A\cup \{e\}$ 
            \End
    \end{codebox}}
  \end{center}

  \begin{itemize}
    \pause
    \bigskip
  \item \alert{Invariant:} $A$ is a subset of a minimum spanning tree

    \bigskip
    \pause
  \item A \alert{safe edge} is an edge that maintains the invariant

    \begin{itemize}
      \medskip
    \item $e$ is such that, if $A$ is a subset of a minimum spanning
      tree, then $A\cup\{e\}$ is also a subset of a minimum spanning tree

      \medskip
      \pause
    \item more or less the \emph{definition} of a greedy algorithm
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}\frametitle{Preliminary Definitions}
  \begin{itemize}
  \item A \alert{cut} $(S,V-S)$ of an undirected graph $G=(V,E)$ is a
    \emph{partition} of $V$
    \pause
  \begin{center}
    \psset{unit=.9}
    \begin{pspicture}(0,0)(6,4.3)\footnotesize
      \cnode(0,0){.3}{n1}\rput(0,0){$a$}
      \cnode(2,0){.3}{n2}\rput(2,0){$b$}
      \cnode(4,0){.3}{n3}\rput(4,0){$c$}
      \cnode(6,0){.3}{n4}\rput(6,0){$d$}
      \cnode(0,2){.3}{n5}\rput(0,2){$e$}
      \cnode(2,2){.3}{n6}\rput(2,2){$f$}
      \cnode(4,2){.3}{n7}\rput(4,2){$g$}
      \cnode(6,2){.3}{n8}\rput(6,2){$h$}
      \cnode(0,4){.3}{n9}\rput(0,4){$i$}
      \cnode(2,4){.3}{n10}\rput(2,4){$j$}
      \cnode(4,4){.3}{n11}\rput(4,4){$k$}
      \cnode(6,4){.3}{n12}\rput(6,4){$m$}
      \ncline{n2}{n7}
      \ncline{n3}{n4}
      \ncline{n3}{n7}
      \ncline{n6}{n9}
      \ncline{n7}{n10}
      \ncline{n8}{n11}
      \ncline{n8}{n12}
      \ncline{n1}{n5}
      \ncline{n1}{n6}
      \ncline{n6}{n2}
      \ncline{n2}{n3}
      \ncline{n7}{n4}
      \ncline{n5}{n9}
      \ncline{n6}{n7}
      \ncline{n7}{n8}
      \ncline{n7}{n11}
      \ncline{n11}{n12}
      \ncline{n9}{n10}
      \ncline{n4}{n8}
      \pause
      \pscircle[linewidth=2pt,linecolor=lightgray](n1){.3}
      \pscircle[linewidth=2pt,linecolor=black](n2){.3}
      \pscircle[linewidth=2pt,linecolor=black](n3){.3}
      \pscircle[linewidth=2pt,linecolor=black](n4){.3}
      \pscircle[linewidth=2pt,linecolor=lightgray](n5){.3}
      \pscircle[linewidth=2pt,linecolor=black](n6){.3}
      \pscircle[linewidth=2pt,linecolor=lightgray](n7){.3}
      \pscircle[linewidth=2pt,linecolor=lightgray](n8){.3}
      \pscircle[linewidth=2pt,linecolor=lightgray](n9){.3}
      \pscircle[linewidth=2pt,linecolor=lightgray](n10){.3}
      \pscircle[linewidth=2pt,linecolor=black](n11){.3}
      \pscircle[linewidth=2pt,linecolor=lightgray](n12){.3}
      %
      \pause
      \pscurve[linecolor=red,linewidth=2pt](1,-0.3)(1.8,3)(4,1)(6.3,1)
      \pscurve[linecolor=red,linewidth=2pt](5,4.3)(4,3)(3,4.3)
%       \ncline{n2}{n7}\aput[2pt](.5){\small\sf 3}
%       \ncline{n3}{n4}\aput[2pt](.5){\small\sf 2}
%       \ncline{n3}{n7}\aput[2pt](.5){\small\sf 3}
%       \ncline{n6}{n9}\aput[2pt](.5){\small\sf 1}
%       \ncline{n7}{n10}\aput[2pt](.5){\small\sf 2}
%       \ncline{n8}{n11}\aput[2pt](.5){\small\sf 3}
%       \ncline{n8}{n12}\aput[2pt](.5){\small\sf 3}
%       \ncline{n1}{n5}\aput[2pt](.5){\small\sf 1}
%       \ncline{n1}{n6}\aput[2pt](.5){\small\sf 2}
%       \ncline{n6}{n2}\aput[2pt](.5){\small\sf 1}
%       \ncline{n2}{n3}\aput[2pt](.5){\small\sf 1}
%       \ncline{n7}{n4}\aput[2pt](.5){\small\sf 1}
%       \ncline{n5}{n9}\aput[2pt](.5){\small\sf 1}
%       \ncline{n6}{n7}\aput[2pt](.5){\small\sf 3}
%       \ncline{n7}{n8}\aput[2pt](.5){\small\sf 1}
%       \ncline{n7}{n11}\aput[2pt](.5){\small\sf 3}
%       \ncline{n11}{n12}\aput[2pt](.5){\small\sf 1}
%       \ncline{n9}{n10}\aput[2pt](.5){\small\sf 3}
%       \ncline{n4}{n8}\aput[2pt](.5){\small\sf 2}
    \end{pspicture}
  \end{center}

  \bigskip
    \pause
  \item An edge $e=(u,v)$ \emph{crosses} the cut $(S,V-S)$ if $u\in S$
    and $v\in V-S$, or vice-versa

  \bigskip
    \pause
  \item A cut $(S,V-S)$ \emph{respects} a set of edges $A$ if no edge
    in $A$ crosses the cut
  \end{itemize}
\end{frame}

\begin{frame}\frametitle{Finding a Safe Edge}
  \begin{center}
    \algobox{%
    \begin{codebox}
        \Procname{$\proc{Generic-MST}(G,w)$}
        \li $A\gets\emptyset$
        \li \While $A$ is not a spanning tree
        \li \Do
        \psframebox[linewidth=0pt,fillcolor=yellow,fillstyle=solid]{%
          find a \emph{safe} edge $e=(u,v)$}
        \li     $A\gets A\cup \{e\}$ 
            \End
    \end{codebox}}
  \end{center}

    \pause
    \bigskip
  \begin{itemize}
  \item Let $(S,V-S)$ be a cut of $G$ that respects $A$

    \bigskip
    \pause
  \item A minimum-weight edge $e$ that crosses $(S,V-S)$ is a safe edge
  \end{itemize}
\end{frame}

\begin{frame}\frametitle{Example}
  \begin{itemize}
  \item Let $(S,V-S)$ be a cut of $G$ that respects $A$

    \medskip
  \item A minimum-weight edge $e$ that crosses $(S,V-S)$ is a safe
    edge
  \end{itemize}
  \pause
  \begin{center}
    \psset{unit=.9}
    \begin{pspicture}(0,0)(6,4.3)\footnotesize
      \cnode(0,0){.3}{n1}\rput(0,0){$a$}
      \cnode(2,0){.3}{n2}\rput(2,0){$b$}
      \cnode(4,0){.3}{n3}\rput(4,0){$c$}
      \cnode(6,0){.3}{n4}\rput(6,0){$d$}
      \cnode(0,2){.3}{n5}\rput(0,2){$e$}
      \cnode(2,2){.3}{n6}\rput(2,2){$f$}
      \cnode(4,2){.3}{n7}\rput(4,2){$g$}
      \cnode(6,2){.3}{n8}\rput(6,2){$h$}
      \cnode(0,4){.3}{n9}\rput(0,4){$i$}
      \cnode(2,4){.3}{n10}\rput(2,4){$j$}
      \cnode(4,4){.3}{n11}\rput(4,4){$k$}
      \cnode(6,4){.3}{n12}\rput(6,4){$m$}
      \ncline{n2}{n7}\aput[2pt](.5){\small\sf 3}
      \ncline{n3}{n4}\aput[2pt](.5){\small\sf 2}
      \ncline{n3}{n7}\aput[2pt](.5){\small\sf 3}
      \ncline{n6}{n9}\aput[2pt](.5){\small\sf 1}
      \ncline{n7}{n10}\aput[2pt](.5){\small\sf 2}
      \ncline{n8}{n11}\aput[2pt](.5){\small\sf 3}
      \ncline{n8}{n12}\aput[2pt](.5){\small\sf 3}
      \ncline{n1}{n5}\aput[2pt](.5){\small\sf 1}
      \ncline{n1}{n6}\aput[2pt](.5){\small\sf 2}
      \ncline{n6}{n2}\aput[2pt](.5){\small\sf 1}
      \ncline{n2}{n3}\aput[2pt](.5){\small\sf 1}
      \ncline{n7}{n4}\aput[2pt](.5){\small\sf 1}
      \ncline{n5}{n9}\aput[2pt](.5){\small\sf 1}
      \ncline{n6}{n7}\aput[2pt](.5){\small\sf 3}
      \ncline{n7}{n8}\aput[2pt](.5){\small\sf 1}
      \ncline{n7}{n11}\aput[2pt](.5){\small\sf 3}
      \ncline{n11}{n12}\aput[2pt](.5){\small\sf 1}
      \ncline{n9}{n10}\aput[2pt](.5){\small\sf 3}
      \ncline{n4}{n8}\aput[2pt](.5){\small\sf 2}
      \only<3->{%
        \ncline[linecolor=blue,linewidth=3pt]{n1}{n5}
        \ncline[linecolor=blue,linewidth=3pt]{n5}{n9}
        \ncline[linecolor=blue,linewidth=3pt]{n2}{n3}
        \ncline[linecolor=blue,linewidth=3pt]{n8}{n12}
        \ncline[linecolor=blue,linewidth=3pt]{n11}{n12}}%
      \only<4-5>{%
        \pscurve[linecolor=red,linewidth=2pt](6.3,1.2)(5,2)(4,2.5)(3,2)(2,1.2)(1,-.3)}%
      \only<5->{%
        \ncline[linecolor=blue,linewidth=3pt]{n2}{n6}}%
      \only<7-8>{%
        \pscurve[linecolor=red,linewidth=2pt](1.2,4.3)(2,3.2)(2.8,4.3)}%
      \only<8>{%
        \ncline[linecolor=blue,linewidth=3pt]{n7}{n10}}%
    \end{pspicture}
  \end{center}
  \begin{itemize}
  \item<3-> $A=\{(a,e),(e,i),(b,c),(k,m),(m,h)\}$

    \medskip
    \pause
  \item<4-> Choosing the cut $(\{a,e,f,h,i,j,k,m\},\{b,c,d,g\})$

    \medskip
    \pause
  \item<5-> $(f,b)$ is a safe edge\uncover<6->{; $(g,h)$ is a safe
      edge, too}
  \end{itemize}
\end{frame}

\begin{frame}\frametitle{Concrete Algorithms}
  \begin{itemize}
    \pause
  \item Kruskal's algorithm (1956)

    \begin{itemize}
      \medskip
    \item based on the generic minimum-spanning-tree algorithm

      \medskip
    \item incrementally builds a \alert{forest} $A$

    \end{itemize}

    \bigskip
    \pause
  \item Prim's algorithm (1957)

    \begin{itemize}
      \medskip
    \item based on the generic minimum-spanning-tree algorithm

      \medskip
    \item incrementally builds a \alert{single tree} $A$
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}\frametitle{Disjoint-Set Data Structure}
  \begin{itemize}
  \item \alert{$\id{Make-Set}(x)$} creates a set containing $x$

    \pause
    \bigskip
  \item \alert{$\id{Find-Set}(x)$} returns the \emph{representative}
    of the set containing $x$

    \begin{itemize}
      \medskip
    \item $x,y\in S\Rightarrow \id{Find-Set}(x)=\id{Find-Set}(y)$

      \medskip
    \item $x\in S_1\wedge y\in S_2\wedge S_1\ne S_2\Rightarrow \id{Find-Set}(x)\ne\id{Find-Set}(y)$
    \end{itemize}

    \pause
    \bigskip
  \item \alert{$\id{Union}(x,y)$} joins the sets containing $x$ and $y$
  \end{itemize}
\end{frame}
\begin{frame}\frametitle{Kruskal's Algorithm}
  \centerline{%
    \algobox{\footnotesize%
      \begin{codebox}
        \Procname{$\proc{MST-Kruskal}(G,w)$}
        \li $A\gets\emptyset$
        \li \For each vertex $v\in V(G)$ 
        \li \Do $\proc{Make-Set}(v)$  \>\>\>\>\>\>\Comment \emph{disjoint-set} data structure
            \End
        \li sort $E$ in non-decreasing order by weight $w$
        \li \For each edge $(u,v)\in E$, taken in non-decreasing order by $w$
        \li \Do \If $\proc{Find-Set}(u)\ne\proc{Find-Set}(v)$
        \li     \Then $A\gets A\cup\{(u,v)\}$
        \li           $\proc{Union}(u,v)$
                \End
            \End
      \end{codebox}}}

  \pause
  \begin{center}
    \psset{unit=.8}
    \begin{pspicture}(0,0)(6,4.3)\footnotesize
      \cnode(0,0){.3}{n1}\rput(0,0){$a$}
      \cnode(2,0){.3}{n2}\rput(2,0){$b$}
      \cnode(4,0){.3}{n3}\rput(4,0){$c$}
      \cnode(6,0){.3}{n4}\rput(6,0){$d$}
      \cnode(0,2){.3}{n5}\rput(0,2){$e$}
      \cnode(2,2){.3}{n6}\rput(2,2){$f$}
      \cnode(4,2){.3}{n7}\rput(4,2){$g$}
      \cnode(6,2){.3}{n8}\rput(6,2){$h$}
      \cnode(0,4){.3}{n9}\rput(0,4){$i$}
      \cnode(2,4){.3}{n10}\rput(2,4){$j$}
      \cnode(4,4){.3}{n11}\rput(4,4){$k$}
      \cnode(6,4){.3}{n12}\rput(6,4){$m$}
      \ncline{n2}{n7}\aput[2pt](.5){\small\sf 3}
      \ncline{n3}{n4}\aput[2pt](.5){\small\sf 2}
      \ncline{n3}{n7}\aput[2pt](.5){\small\sf 3}
      \ncline{n6}{n9}\aput[2pt](.5){\small\sf 1}
      \ncline{n7}{n10}\aput[2pt](.5){\small\sf 2}
      \ncline{n8}{n11}\aput[2pt](.5){\small\sf 3}
      \ncline{n8}{n12}\aput[2pt](.5){\small\sf 3}
      \ncline{n1}{n5}\aput[2pt](.5){\small\sf 1}
      \ncline{n1}{n6}\aput[2pt](.5){\small\sf 2}
      \ncline{n6}{n2}\aput[2pt](.5){\small\sf 1}
      \ncline{n2}{n3}\aput[2pt](.5){\small\sf 1}
      \ncline{n7}{n4}\aput[2pt](.5){\small\sf 1}
      \ncline{n5}{n9}\aput[2pt](.5){\small\sf 1}
      \ncline{n6}{n7}\aput[2pt](.5){\small\sf 3}
      \ncline{n7}{n8}\aput[2pt](.5){\small\sf 1}
      \ncline{n7}{n11}\aput[2pt](.5){\small\sf 3}
      \ncline{n11}{n12}\aput[2pt](.5){\small\sf 1}
      \ncline{n9}{n10}\aput[2pt](.5){\small\sf 3}
      \ncline{n4}{n8}\aput[2pt](.5){\small\sf 2}
      \pause
      \only<+>{\ncline[linecolor=green,linewidth=3pt]{n6}{n9}}
      \only<+->{\ncline[linecolor=blue,linewidth=3pt]{n6}{n9}}
      \only<+>{\ncline[linecolor=green,linewidth=3pt]{n1}{n5}}
      \only<+->{\ncline[linecolor=blue,linewidth=3pt]{n1}{n5}}
      \only<+>{\ncline[linecolor=green,linewidth=3pt]{n6}{n2}}
      \only<+->{\ncline[linecolor=blue,linewidth=3pt]{n6}{n2}}
      \only<+>{\ncline[linecolor=green,linewidth=3pt]{n7}{n8}}
      \only<+->{\ncline[linecolor=blue,linewidth=3pt]{n7}{n8}}
      \only<+>{\ncline[linecolor=green,linewidth=3pt]{n11}{n12}}
      \only<+->{\ncline[linecolor=blue,linewidth=3pt]{n11}{n12}}
      \only<+>{\ncline[linecolor=green,linewidth=3pt]{n2}{n3}}
      \only<+->{\ncline[linecolor=blue,linewidth=3pt]{n2}{n3}}
      \only<+>{\ncline[linecolor=green,linewidth=3pt]{n7}{n4}}
      \only<+->{\ncline[linecolor=blue,linewidth=3pt]{n7}{n4}}
      \only<+>{\ncline[linecolor=green,linewidth=3pt]{n5}{n9}}
      \only<+->{\ncline[linecolor=blue,linewidth=3pt]{n5}{n9}}
      \only<+>{\ncline[linecolor=green,linewidth=3pt]{n1}{n6}}
      \only<+->{}
      \only<+>{\ncline[linecolor=green,linewidth=3pt]{n4}{n8}}
      \only<+->{}
      \only<+>{\ncline[linecolor=green,linewidth=3pt]{n7}{n10}}
      \only<+->{\ncline[linecolor=blue,linewidth=3pt]{n7}{n10}}
      \only<+>{\ncline[linecolor=green,linewidth=3pt]{n3}{n4}}
      \only<+->{\ncline[linecolor=blue,linewidth=3pt]{n3}{n4}}
      \only<+>{\ncline[linecolor=green,linewidth=3pt]{n2}{n7}}
      \only<+->{}
      \only<+>{\ncline[linecolor=green,linewidth=3pt]{n3}{n7}}
      \only<+->{}
      \only<+>{\ncline[linecolor=green,linewidth=3pt]{n8}{n11}}
      \only<+->{\ncline[linecolor=blue,linewidth=3pt]{n8}{n11}}
      \only<+>{\ncline[linecolor=green,linewidth=3pt]{n8}{n12}}
      \only<+->{}
      \only<+>{\ncline[linecolor=green,linewidth=3pt]{n6}{n7}}
      \only<+->{}
      \only<+>{\ncline[linecolor=green,linewidth=3pt]{n7}{n11}}
      \only<+->{}
      \only<+>{\ncline[linecolor=green,linewidth=3pt]{n9}{n10}}
      \only<+->{}
    \end{pspicture}
  \end{center}
\end{frame}
\begin{frame}\frametitle{Complexity of \proc{MST-Kruskal}}
  \centerline{%
    \algobox{\footnotesize%
      \begin{codebox}
        \Procname{$\proc{MST-Kruskal}(G,w)$}
        \li $A\gets\emptyset$
        \li \For each vertex $v\in V(G)$ 
        \li \Do $\proc{Make-Set}(v)$  \>\>\>\>\>\>\Comment \emph{disjoint-set} data structure
            \End
        \li sort $E$ in non-decreasing order by weight $w$
        \li \For each edge $(u,v)\in E$, taken in non-decreasing order by $w$
        \li \Do \If $\proc{Find-Set}(u)\ne\proc{Find-Set}(v)$
        \li     \Then $A\gets A\cup\{(u,v)\}$
        \li           $\proc{Union}(u,v)$
                \End
            \End
      \end{codebox}}}

  \pause
    \medskip
  \begin{itemize}
  \item $|V|$ times \proc{Make-Set} (loop of line~2--3)

    \pause
    \medskip
  \item $O(|E|\log{|E|})$ for sorting $E$ (line~4)

    \pause
    \medskip
  \item $2|E|$ times \proc{Find-Set}

    \pause
    \medskip
  \item $O(|E|)$ times \proc{Union}
  \end{itemize}
\end{frame}
\begin{frame}\frametitle{Prim's Algorithm}
  \begin{itemize}
  \item Build builds $T$ incrementally
    \begin{itemize}
      \medskip
    \item in each iteration, add a node $v$ to $T$ through an edge
      that connecs $v$ with $T$
    \end{itemize}

    \pause
    \bigskip
  \item Variables storing values known at each iteration
    \begin{itemize}
      \pause
      \bigskip
    \item $T$, tree being built

      \pause
      \bigskip
    \item $W[v]$ best known cost/weight of connecting $v$ with $T$

      \pause
      \bigskip
    \item $P[v]$, node $u\in T$ such that the edge $(u,v)$ is the
      least-cost edge connecting $v$ with $T$
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}[c]\frametitle{Prim's Algorithm}
  \begin{center}
    \algobox{%
      \begin{codebox}
        \Procname{$\proc{MST-Prim}(G, u, w)$}
        \li $T\gets(\emptyset,\emptyset)$
        \li \For each vertex $v\in V(G)$
        \li \Do $W[v]\gets\infty$
        \li     $P[v]\gets \const{nil}$
            \End
        \li $W[u]\gets 0$
        \li \While $V(T)\ne V(G)$
        \li \Do find $u\not\in V(T)$ such that $W[u]$ is minimal
        \li     $T\gets T\cup \{u\}$ \Comment add $u$ to $T$
        \li     \For all $v\in \id{Adj}(u)\setminus V(T)$
        \li     \Do \If $w(u,v) < W[v]$
        \li         \Then $W[v]\gets w(u,v)$
        \li               $P[v]\gets u$
                    \End
                \End
            \End
      \end{codebox}}
  \end{center}
\end{frame}

\begin{frame}[c]\frametitle{Prim's Algorithm}
  \begin{tabular}[m]{c}
  \algobox{%
    \small%
    \begin{codebox}
      \Procname{$\proc{MST-Prim}(G, u, w)$}
      \li $T\gets(\emptyset,\emptyset)$
      \li \For each vertex $v\in V(G)$
      \li \Do $W[v]\gets\infty$
      \li     $P[v]\gets \const{nil}$
          \End
      \li $W[u]\gets 0$
      \li \While $V(T)\ne V(G)$
      \li \Do find $u\not\in V(T)$ such that $W[u]$ is minimal
      \li     $T\gets T\cup \{u\}$ \Comment add $u$ to $T$
      \li     \For all $v\in \id{Adj}(u)\setminus V(T)$
      \li     \Do \If $w(u,v) < W[v]$
      \li         \Then $W[v]\gets w(u,v)$
      \li               $P[v]\gets u$
                  \End
              \End
          \End
   \end{codebox}}
  \end{tabular}
  \hfill
 \begin{tabular}[m]{c}
 \psset{unit=0.9}\small
 \begin{pspicture}(-0.5,-5)(6.5,2.5)
   \cnode(0,0){.3}{Na}\rput(0,0){$a$}
   \cnode(2,0){.3}{Nb}\rput(2,0){$b$}
   \cnode(4,0){.3}{Nc}\rput(4,0){$c$}
   \cnode(6,0){.3}{Nd}\rput(6,0){$d$}
   \cnode(0,2){.3}{Ne}\rput(0,2){$e$}
   \cnode(2,2){.3}{Nf}\rput(2,2){$f$}
   \cnode(4,2){.3}{Ng}\rput(4,2){$g$}
   \cnode(6,2){.3}{Nh}\rput(6,2){$h$}
   \ncline{Ne}{Nf}\aput[2pt](.5){\small\sf 1}
   \ncline{Na}{Nb}\aput[2pt](.5){\small\sf 4}
   \ncline{Nb}{Ng}\aput[2pt](.5){\small\sf 3}
   \ncline{Nc}{Nd}\aput[2pt](.5){\small\sf 2}
   \ncline{Nc}{Ng}\aput[2pt](.5){\small\sf 3}
   \ncline{Na}{Ne}\aput[2pt](.5){\small\sf 1}
   \ncline{Na}{Nf}\aput[2pt](.5){\small\sf 2}
   \ncline{Nf}{Nb}\aput[2pt](.5){\small\sf 1}
   \ncline{Nb}{Nc}\aput[2pt](.5){\small\sf 1}
   \ncline{Ng}{Nd}\aput[2pt](.5){\small\sf 1}
   \ncline{Ne}{n9}\aput[2pt](.5){\small\sf 1}
   \ncline{Nf}{Ng}\aput[2pt](.5){\small\sf 3}
   \ncline{Ng}{Nh}\aput[2pt](.5){\small\sf 1}
   \ncline{Nd}{Nh}\aput[2pt](.5){\small\sf 2}
   \only<+>{%
     \rput[t](3,-.7){%
       \begin{tabular}{cccc}
         $v$&$W$&$P$&\hbox to 1em{\hfil$T$\hfil}\\
         \hline
         $a$&$0$&&\\
         $b$&$\infty$&&\\
         $c$&$\infty$&&\\
         $d$&$\infty$&&\\
         $e$&$\infty$&&\\
         $f$&$\infty$&&\\
         $g$&$\infty$&&\\
         $h$&$\infty$&&\\
         \hline
       \end{tabular}}}
   \only<+>{%
     \rput[t](3,-.7){%
       \begin{tabular}{cccc}
         $v$&$W$&$P$&\hbox to 1em{\hfil$T$\hfil}\\
         \hline
         $a$&0&&\ding{51}\\
         $b$&$\infty$&&\\
         $c$&$\infty$&&\\
         $d$&$\infty$&&\\
         $e$&$\infty$&&\\
         $f$&$\infty$&&\\
         $g$&$\infty$&&\\
         $h$&$\infty$&&\\
         \hline
       \end{tabular}}
     \pscircle[linecolor=blue,linewidth=2pt](Na){.4}}%
   \only<.->{\pscircle[linecolor=black,linewidth=2pt](Na){.3}}%
   \only<+>{%
     \rput[t](3,-.7){%
       \begin{tabular}{cccc}
         $v$&$W$&$P$&\hbox to 1em{\hfil$T$\hfil}\\
         \hline
         $a$&0&&\ding{51}\\
         $b$&4&$a$&\\
         $c$&$\infty$&&\\
         $d$&$\infty$&&\\
         $e$&1&$a$&\\
         $f$&2&$a$&\\
         $g$&$\infty$&&\\
         $h$&$\infty$&&\\
         \hline
       \end{tabular}}
     \pscircle[linecolor=blue,linewidth=2pt](Na){.4}}%
   \only<.->{\pscircle[linecolor=black,linewidth=2pt](Na){.3}}%
   \only<.>{\ncline[linecolor=blue,linewidth=3pt,linestyle=dashed]{Na}{Ne}
     \rput[t](3,-.7){%
       \begin{tabular}{cccc}
         $v$&$W$&$P$&\hbox to 1em{\hfil$T$\hfil}\\
         \hline
         $a$&0&&\ding{51}\\
         $b$&4&$a$&\\
         $c$&$\infty$&&\\
         $d$&$\infty$&&\\
         $e$&1&$a$&\\
         $f$&2&$a$&\\
         $g$&$\infty$&&\\
         $h$&$\infty$&&\\
         \hline
       \end{tabular}}
     \pscircle[linecolor=lightgray,linewidth=2pt](Ne){.3}}%
   \only<.>{\ncline[linecolor=blue,linewidth=3pt,linestyle=dashed]{Na}{Nf}
     \pscircle[linecolor=lightgray,linewidth=2pt](Nf){.3}}%
   \only<.>{\ncline[linecolor=blue,linewidth=3pt,linestyle=dashed]{Na}{Nb}
     \pscircle[linecolor=lightgray,linewidth=2pt](Nb){.3}}%
   \only<+>{%
     \rput[t](3,-.7){%
       \begin{tabular}{cccc}
         $v$&$W$&$P$&\hbox to 1em{\hfil$T$\hfil}\\
         \hline
         $a$&0&&\ding{51}\\
         $b$&4&$a$&\\
         $c$&$\infty$&&\\
         $d$&$\infty$&&\\
         $e$&1&$a$&\ding{51}\\
         $f$&2&$a$&\\
         $g$&$\infty$&&\\
         $h$&$\infty$&&\\
         \hline
       \end{tabular}}
     \pscircle[linecolor=blue,linewidth=2pt](Ne){.4}}%
   \only<.->{\pscircle[linecolor=black,linewidth=2pt](Ne){.3}}%
   \only<.->{\ncline[linecolor=blue,linewidth=3pt]{Na}{Ne}}%
   \only<.>{\ncline[linecolor=blue,linewidth=3pt,linestyle=dashed]{Na}{Nb}
     \pscircle[linecolor=lightgray,linewidth=2pt](Nb){.3}}%
   \only<.>{\ncline[linecolor=blue,linewidth=3pt,linestyle=dashed]{Na}{Nf}
     \pscircle[linecolor=lightgray,linewidth=2pt](Nf){.3}}%
   % 
   \only<+>{%
     \rput[t](3,-.7){%
       \begin{tabular}{cccc}
         $v$&$W$&$P$&\hbox to 1em{\hfil$T$\hfil}\\
         \hline
         $a$&0&&\ding{51}\\
         $b$&4&$a$&\\
         $c$&$\infty$&&\\
         $d$&$\infty$&&\\
         $e$&1&$a$&\ding{51}\\
         $f$&1&$e$&\\
         $g$&$\infty$&&\\
         $h$&$\infty$&&\\
         \hline
       \end{tabular}}
     \pscircle[linecolor=blue,linewidth=2pt](Ne){.4}}%
   \only<.->{\pscircle[linecolor=black,linewidth=2pt](Ne){.3}}%
   \only<.>{\ncline[linecolor=blue,linewidth=3pt,linestyle=dashed]{Ne}{Nf}
     \pscircle[linecolor=lightgray,linewidth=2pt](Nf){.3}}%
   \only<.>{\ncline[linecolor=blue,linewidth=3pt,linestyle=dashed]{Na}{Nb}
     \pscircle[linecolor=lightgray,linewidth=2pt](Nb){.3}}%
   % 
   \only<+>{%
     \rput[t](3,-.7){%
       \begin{tabular}{cccc}
         $v$&$W$&$P$&\hbox to 1em{\hfil$T$\hfil}\\
         \hline
         $a$&0&&\ding{51}\\
         $b$&4&$a$&\\
         $c$&$\infty$&&\\
         $d$&$\infty$&&\\
         $e$&1&$a$&\ding{51}\\
         $f$&1&$e$&\ding{51}\\
         $g$&$\infty$&&\\
         $h$&$\infty$&&\\
         \hline
       \end{tabular}}
     \pscircle[linecolor=blue,linewidth=2pt](Nf){.4}}%
   \only<.->{\pscircle[linecolor=black,linewidth=2pt](Nf){.3}}%
   \only<.->{\ncline[linecolor=blue,linewidth=3pt]{Ne}{Nf}}%
   \only<.>{\ncline[linecolor=blue,linewidth=3pt,linestyle=dashed]{Na}{Nb}
     \pscircle[linecolor=lightgray,linewidth=2pt](Nb){.3}}%
   \only<+>{%
     \rput[t](3,-.7){%
       \begin{tabular}{cccc}
         $v$&$W$&$P$&\hbox to 1em{\hfil$T$\hfil}\\
         \hline
         $a$&0&&\ding{51}\\
         $b$&1&$f$&\\
         $c$&$\infty$&&\\
         $d$&$\infty$&&\\
         $e$&1&$a$&\ding{51}\\
         $f$&1&$e$&\ding{51}\\
         $g$&3&$f$&\\
         $h$&$\infty$&&\\
         \hline
       \end{tabular}}
     \pscircle[linecolor=blue,linewidth=2pt](Nf){.4}}%
   \only<.->{\pscircle[linecolor=black,linewidth=2pt](Nf){.3}}%
   \only<.>{\ncline[linecolor=blue,linewidth=3pt,linestyle=dashed]{Nf}{Nb}
     \pscircle[linecolor=lightgray,linewidth=2pt](Nb){.3}}%
   \only<.>{\ncline[linecolor=blue,linewidth=3pt,linestyle=dashed]{Nf}{Ng}
     \pscircle[linecolor=lightgray,linewidth=2pt](Ng){.3}}%
   % 
   \only<+>{%
     \rput[t](3,-.7){%
       \begin{tabular}{cccc}
         $v$&$W$&$P$&\hbox to 1em{\hfil$T$\hfil}\\
         \hline
         $a$&0&&\ding{51}\\
         $b$&1&$f$&\ding{51}\\
         $c$&$\infty$&&\\
         $d$&$\infty$&&\\
         $e$&1&$a$&\ding{51}\\
         $f$&1&$e$&\ding{51}\\
         $g$&3&$f$&\\
         $h$&$\infty$&&\\
         \hline
       \end{tabular}}
     \pscircle[linecolor=blue,linewidth=2pt](Nb){.4}}%
   \only<.->{\pscircle[linecolor=black,linewidth=2pt](Nb){.3}}%
   \only<.->{\ncline[linecolor=blue,linewidth=3pt]{Nf}{Nb}}%
   \only<.>{\ncline[linecolor=blue,linewidth=3pt,linestyle=dashed]{Nf}{Ng}
     \pscircle[linecolor=lightgray,linewidth=2pt](Ng){.3}}%
   % 
   \only<+>{%
     \rput[t](3,-.7){%
       \begin{tabular}{cccc}
         $v$&$W$&$P$&\hbox to 1em{\hfil$T$\hfil}\\
         \hline
         $a$&0&&\ding{51}\\
         $b$&1&$f$&\ding{51}\\
         $c$&1&$b$&\\
         $d$&$\infty$&&\\
         $e$&1&$a$&\ding{51}\\
         $f$&1&$e$&\ding{51}\\
         $g$&3&$f$&\\
         $h$&$\infty$&&\\
         \hline
       \end{tabular}}
     \pscircle[linecolor=blue,linewidth=2pt](Nb){.4}}%
   \only<.->{\pscircle[linecolor=black,linewidth=2pt](Nb){.3}}%
   \only<.>{\ncline[linecolor=blue,linewidth=3pt,linestyle=dashed]{Nf}{Ng}
     \pscircle[linecolor=lightgray,linewidth=2pt](Ng){.3}}%
   \only<.>{\ncline[linecolor=blue,linewidth=3pt,linestyle=dashed]{Nb}{Nc}
     \pscircle[linecolor=lightgray,linewidth=2pt](Nc){.3}}%
   % 
   \only<+>{%
     \rput[t](3,-.7){%
       \begin{tabular}{cccc}
         $v$&$W$&$P$&\hbox to 1em{\hfil$T$\hfil}\\
         \hline
         $a$&0&&\ding{51}\\
         $b$&1&$f$&\ding{51}\\
         $c$&1&$b$&\ding{51}\\
         $d$&$\infty$&&\\
         $e$&1&$a$&\ding{51}\\
         $f$&1&$e$&\ding{51}\\
         $g$&3&$f$&\\
         $h$&$\infty$&&\\
         \hline
       \end{tabular}}
     \pscircle[linecolor=blue,linewidth=2pt](Nc){.4}}%
   \only<.->{\pscircle[linecolor=black,linewidth=2pt](Nc){.3}}%
   \only<.->{\ncline[linecolor=blue,linewidth=3pt]{Nb}{Nc}}%
   \only<.>{\ncline[linecolor=blue,linewidth=3pt,linestyle=dashed]{Nf}{Ng}
     \pscircle[linecolor=lightgray,linewidth=2pt](Ng){.3}}%
   % 
   \only<+>{%
     \rput[t](3,-.7){%
       \begin{tabular}{cccc}
         $v$&$W$&$P$&\hbox to 1em{\hfil$T$\hfil}\\
         \hline
         $a$&0&&\ding{51}\\
         $b$&1&$f$&\ding{51}\\
         $c$&1&$b$&\ding{51}\\
         $d$&2&$c$&\\
         $e$&1&$a$&\ding{51}\\
         $f$&1&$e$&\ding{51}\\
         $g$&3&$f$&\\
         $h$&$\infty$&&\\
         \hline
       \end{tabular}}
     \pscircle[linecolor=blue,linewidth=2pt](Nc){.4}}%
   \only<.->{\pscircle[linecolor=black,linewidth=2pt](Nc){.3}}%
   \only<.->{\ncline[linecolor=blue,linewidth=3pt]{Nb}{Nc}}%
   \only<.>{\ncline[linecolor=blue,linewidth=3pt,linestyle=dashed]{Nc}{Nd}
     \pscircle[linecolor=lightgray,linewidth=2pt](Nd){.3}}%
   \only<.>{\ncline[linecolor=blue,linewidth=3pt,linestyle=dashed]{Nf}{Ng}
     \pscircle[linecolor=lightgray,linewidth=2pt](Ng){.3}}%
   % 
   \only<+>{%
     \rput[t](3,-.7){%
       \begin{tabular}{cccc}
         $v$&$W$&$P$&\hbox to 1em{\hfil$T$\hfil}\\
         \hline
         $a$&0&&\ding{51}\\
         $b$&1&$f$&\ding{51}\\
         $c$&1&$b$&\ding{51}\\
         $d$&2&$c$&\ding{51}\\
         $e$&1&$a$&\ding{51}\\
         $f$&1&$e$&\ding{51}\\
         $g$&3&$f$&\\
         $h$&$\infty$&&\\
         \hline
       \end{tabular}}
     \pscircle[linecolor=blue,linewidth=2pt](Nd){.4}}%
   \only<.->{\pscircle[linecolor=black,linewidth=2pt](Nd){.3}}%
   \only<.->{\ncline[linecolor=blue,linewidth=3pt]{Nc}{Nd}}%
   \only<.>{\ncline[linecolor=blue,linewidth=3pt,linestyle=dashed]{Nf}{Ng}
     \pscircle[linecolor=lightgray,linewidth=2pt](Ng){.3}}%
   \only<+>{%
     \rput[t](3,-.7){%
       \begin{tabular}{cccc}
         $v$&$W$&$P$&\hbox to 1em{\hfil$T$\hfil}\\
         \hline
         $a$&0&&\ding{51}\\
         $b$&1&$f$&\ding{51}\\
         $c$&1&$b$&\ding{51}\\
         $d$&2&$c$&\ding{51}\\
         $e$&1&$a$&\ding{51}\\
         $f$&1&$e$&\ding{51}\\
         $g$&1&$d$&\\
         $h$&2&$d$&\\
         \hline
       \end{tabular}}
     \pscircle[linecolor=blue,linewidth=2pt](Nd){.4}}%
   \only<.->{\pscircle[linecolor=black,linewidth=2pt](Nd){.3}}%
   \only<.>{\ncline[linecolor=blue,linewidth=3pt,linestyle=dashed]{Nd}{Ng}
     \pscircle[linecolor=lightgray,linewidth=2pt](Ng){.3}}%
   \only<.>{\ncline[linecolor=blue,linewidth=3pt,linestyle=dashed]{Nd}{Nh}
     \pscircle[linecolor=lightgray,linewidth=2pt](Nh){.3}}%
   \only<+>{%
     \rput[t](3,-.7){%
       \begin{tabular}{cccc}
         $v$&$W$&$P$&\hbox to 1em{\hfil$T$\hfil}\\
         \hline
         $a$&0&&\ding{51}\\
         $b$&1&$f$&\ding{51}\\
         $c$&1&$b$&\ding{51}\\
         $d$&2&$c$&\ding{51}\\
         $e$&1&$a$&\ding{51}\\
         $f$&1&$e$&\ding{51}\\
         $g$&1&$d$&\ding{51}\\
         $h$&2&$d$&\\
         \hline
       \end{tabular}}
     \pscircle[linecolor=blue,linewidth=2pt](Ng){.4}}%
   \only<.->{\pscircle[linecolor=black,linewidth=2pt](Ng){.3}}%
   \only<.->{\ncline[linecolor=blue,linewidth=3pt]{Nd}{Ng}}%
   \only<.>{\ncline[linecolor=blue,linewidth=3pt,linestyle=dashed]{Nd}{Nh}
     \pscircle[linecolor=lightgray,linewidth=2pt](Nh){.3}}%
   \only<+>{%
     \rput[t](3,-.7){%
       \begin{tabular}{cccc}
         $v$&$W$&$P$&\hbox to 1em{\hfil$T$\hfil}\\
         \hline
         $a$&0&&\ding{51}\\
         $b$&1&$f$&\ding{51}\\
         $c$&1&$b$&\ding{51}\\
         $d$&2&$c$&\ding{51}\\
         $e$&1&$a$&\ding{51}\\
         $f$&1&$e$&\ding{51}\\
         $g$&1&$d$&\ding{51}\\
         $h$&1&$g$&\\
         \hline
       \end{tabular}}
     \pscircle[linecolor=blue,linewidth=2pt](Ng){.4}}%
   \only<.->{\pscircle[linecolor=black,linewidth=2pt](Ng){.3}}%
   \only<.>{\ncline[linecolor=blue,linewidth=3pt,linestyle=dashed]{Ng}{Nh}
     \pscircle[linecolor=lightgray,linewidth=2pt](Nh){.3}}%
   \only<+>{%
     \rput[t](3,-.7){%
       \begin{tabular}{cccc}
         $v$&$W$&$P$&\hbox to 1em{\hfil$T$\hfil}\\
         \hline
         $a$&0&&\ding{51}\\
         $b$&1&$f$&\ding{51}\\
         $c$&1&$b$&\ding{51}\\
         $d$&2&$c$&\ding{51}\\
         $e$&1&$a$&\ding{51}\\
         $f$&1&$e$&\ding{51}\\
         $g$&1&$d$&\ding{51}\\
         $h$&1&$g$&\ding{51}\\
         \hline
       \end{tabular}}
     \pscircle[linecolor=blue,linewidth=2pt](Nh){.4}}%
   \only<.->{\pscircle[linecolor=black,linewidth=2pt](Nh){.3}}%
   \only<.->{
     \rput[t](3,-.7){%
       \begin{tabular}{cccc}
         $v$&$W$&$P$&\hbox to 1em{\hfil$T$\hfil}\\
         \hline
         $a$&0&&\ding{51}\\
         $b$&1&$f$&\ding{51}\\
         $c$&1&$b$&\ding{51}\\
         $d$&2&$c$&\ding{51}\\
         $e$&1&$a$&\ding{51}\\
         $f$&1&$e$&\ding{51}\\
         $g$&1&$d$&\ding{51}\\
         $h$&1&$g$&\ding{51}\\
         \hline
       \end{tabular}}
     \ncline[linecolor=blue,linewidth=3pt]{Ng}{Nh}}%
   \pause
 \end{pspicture}
 \end{tabular}
\end{frame}

\end{document}

%%% Local Variables: 
%%% mode: latex
%%% TeX-PDF-from-DVI: "Dvips"
%%% TeX-fold-macro-spec-list: nil
%%% TeX-fold-env-spec-list: (("[PS-PICTURE]" ("pspicture")) 
%%%                          ("[CODEBOX]" ("codebox")))
%%% outline-regexp: "[\t ]*\\\\documentclass\\|[\t ]*\\\\begin *{frame}\\|[\t ]*\\\\begin *{document}\\|[\t ]*\\\\end *{document}\\|%===\\|[\t ]*\\\\part"
%%% End: 
