/
minimumspanningtrees.tex
284 lines (252 loc) · 9.71 KB
/
minimumspanningtrees.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
%========== Minimum Spanning Trees ==========%
\chapter{Minimum Spanning Trees}
\label{ch:minimumspanningtrees}
\textbf{Pensum} 23 \cite{clrs} \\\\
\textbf{Assignments} 13-3 \\\\
\textbf{Algorithms} Kruskal's algorithm, Prim's algorithm \\\\
\textbf{Keywords} Greedy, optimal substructure, light edges,
binary- and fibonacci-heap
\vspace{1in}
\noindent A minimum spanning tree $T$ is a is a tree or graph whose edges are
a subset $T \subseteq E$ of a weighted graph $G = (V, E)$, with weight
function $w$, such that the weight of an edge $(u, v) \in E$ is determined by
$w$, and the weight of the tree $T$ is minimized.
The total weight of a graph $T$ is given by the sum of all of its edge weights.
\begin{align}
w(T) = \sum_{(u, v) \in T}w(u, v)
\end{align}
The term \textit{spanning} refers to the fact that the tree $T$ spans the
entire set of vertices $V$, and the term \textit{minimum} refers to that the
tree $T$ is of minimum weight.
\section{Definitions}
These are the definitions specific to the minimum spanning tree problem.
\begin{description}
\item A \textbf{cut} $(S, V-S)$ of an undirected graph $G = (V, E)$ is a
partition of $V$.
\item A \textbf{crossing} is an edge $(u, v) \in E$ that \textit{crosses}
the cut $(S, V - S)$ --- that is, the edges end-points are in $S$ and $S - V$,
respectively.
\item An cut \textbf{respects} a set $A$ of edges if no edges in $A$
crosses the cut.
\item A \textbf{light edge} is an edge that crosses a cut and is of
minimum weight (see theorem 23.1\cite[p. 626]{clrs}).
\end{description}
\section{Optimal Substructure}
The proof of optimal substructure for minimum spanning trees is given by
\begin{theorem} \cite[23.1, p. 626]{clrs} \\
\label{thm:23.1}
\textnormal
{
Let $G = (V, E)$ be a connected undirected graph with a real-valued weight
function $w$ defined on $E$. Let $A \subset E$ that is included in some
minimum spanning tree for $G$, let $(S, V - S)$ be any cut of $G$ that
respects $A$, and let $(u, v)$ be a light edge crossing the cut. Then, the
edge $(u, v)$ is safe for $A$.
}
\end{theorem}
\begin{proof}
Let $T$ be a MST that includes $A$, and assume that $T$ does not contain
the light edge $(u, v)$ --- if it does, we are done. We construct another
MST $T'$ that includes $A \cup \{(u, v)\}$. The edge $(u, v)$ forms a
cycle with a path $p$ from $u$ to $v$ in $T$. Since $u \in S$ and
$v \in V - S$, at least one edge in $T$ lies on the path $p$ and also
crosses the cut. Let $(x, y)$ be any such edge that is not in $A$, since
the cut respects $A$. Since $(x, y)$ is on the unique simple path from $u$
to $v$ in $T$, removing $(x, y)$ breaks $T$ into two subtrees. Adding
$(u, v)$ reconnects them to form a new spanning tree $T' = T - \{(x, y)\}
\cup \{(u, v)\}$. And since both edges crosses the cut, but $(u, v)$ is a
light edge $w(u, v) \leq (x, y)$.
\begin{align}
w(T') = w(T) - w(x, y) + w(u, v) \leq w(T)
\end{align}
But $T$ is a MST, that is $w(T) \leq w(T')$, then so must $T'$ be.
\end{proof}
\section{Overlapping Subproblems}
The overlapping subproblems property follows directly from theorem
\ref{thm:23.1}, that states if we remove any edge from a minimum spanning
tree, we are left with subtrees $T_1$ and $T_2$, each a subset of a
minimum spanning tree $T$.
\newpage
\section{Generic Minimum Spanning Tree}
% p. 626, CLRS
The algorithm given below for finding the minimum spanning tree is a
generalization, meaning that it is somewhat loosely defined. As such, this
is merely a template for building an actual algorithm.
\begin{algorithm}
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}
\SetKwFunction{GenericMST}{GenericMST}
\Input{A graph $G = (V, E)$ and a weight function $w$ defined on $E$.}
\Output{A minimum spanning tree of $G$.}
\BlankLine
\GenericMST($G$, $w$) \\
\Begin
{
$A = \emptyset$ \\
\While{$A$ \textnormal{does not form a spanning tree}}
{
find an edge $(u, v)$ that is safe for $A$ \\
$A = A \cup \{(u, v)\}$
}
}
\caption{GenericMST}
\label{alg:generic-mst}
\end{algorithm}
\subsection{Loop Invariant}
We state the loop invariant of algorithm \ref{alg:generic-mst}; prior to each
iteration, $A$ is a subset of some minimum spanning tree.
\\\\
\noindent \textbf{Initialization} \\
After line 3, where we initialize the set $A$ to the empty set, $A$ trivially
satisfies the loop invariant, as the empty set is a subset of any set.
\\\\
\noindent \textbf{Maintenance} \\
Lines 4-7 maintains the loop invariant, because we only add \textit{safe}
edges to $A$. That is, edges $(u, v)$ where $A \cup \{(u, v)\}$ also
satisfies the loop invariant.
\\\\
\noindent \textbf{Termination} \\
Since all edges added to $A$ are in a minimum spanning tree, $A$ must be a
minimum spanning tree of $G$.
\newpage
\section{Kruskal's Algorithm}
% p. 631
The algorithm follows the greedy strategy by sorting the edges into
nondecreasing order of their weights and then simply adding each edge to
the forest if it does not already belong to it.
\\\\
\begin{algorithm}[H]
\caption{Kruskal's algorithm}
\label{alg:kruskal-mst}
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}
\SetKwFunction{KruskalMST}{KruskalMST}
\SetKwFunction{MakeSet}{MakeSet}
\SetKwFunction{FindSet}{FindSet}
\SetKwFunction{Union}{Union}
\Input{A graph $G$ and a weight function $w$.}
\Output{A minimum spanning tree.}
\BlankLine
\KruskalMST($G$, $w$) \\
\Begin
{
% initialization O(V)
$A = \emptyset$ \\
\For{\textnormal{each vertex } $v \in G.V$}
{
\MakeSet($v$)
}
Sort the edges of $G.E$ into nondecreasing order by weight $w$ \\
% maintenance
\For{\textnormal{each edge } $(u, v) \in G.E$}
{
\If{\FindSet$(u)$ $\neq$ \FindSet$(v)$}
{
$A = A \cup \{(u, v)\}$ \\
\Union($u$, $v$)
}
}
\Return $A$
}
\end{algorithm}
\begin{description}
\item \texttt{MakeSet} creates a set whose only member and representative is $x$ --- $\Theta(1)$.
\item \texttt{FindSet} returns the representative of the set containing $x$ --- $\Theta(1)$.
\item \texttt{Union} creates the union set --- $\Theta(1)$.
\end{description}
%\newpage
\subsection{Analysis}
%The running-time of \ref{alg:kruskal-mst} depends on the implementation of
%the disjoint-set data structure. Assuming the use of the fastest
%implementation known (disjoint-set-forest, see section
%21.3\cite[p. 568]{clrs}), the running-time is as follows.
%
The initialization occurs on lines 3--7. Line 3 takes constant time
$\Theta(1)$, lines 4--6 performs $|V|$ \texttt{MakeSet} operations. We can
sort the edges $G.E$ in $O(E \lg E)$-time.
The loop on lines 8--13 performs $O(E)$ \texttt{FindSet}- and \texttt{Union}
operations giving us grand a total of $O((V+E) \alpha(V))$, where $\alpha$
is a slowly growing function (see section 21.4\cite[p. 573]{clrs}).
\newpage
\section{Prim's Algorithm}
% p. 634
This algorithm relies on a priority queue (see section
\ref{ch:heaps|sub:priorityqueues} on page \pageref{ch:heaps|sub:priorityqueues}
for more on this topic). It works by building a min-heap priority queue of the
vertices, and then continually extracting the minimum off of the heap. On each
extraction, the algorithm goes through the adjacency list and updates the key
and parent of the vertices still in the priority queue, which sets us up for
the next iteration.
\\\\
\begin{algorithm}[H]
\caption{Prim's algorithm}
\label{alg:prim-mst}
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}
\SetKwFunction{PrimMST}{PrimMST}
\SetKwFunction{ExtractMin}{ExtractMin}
\SetKw{Nil}{NIL}
\Input{A graph $G$, a weight function $w$ and an arbitrary root $r$.}
\Output{In-place minimized spanning tree of $G$.}
\BlankLine
\PrimMST($G$, $w$, $r$) \\
\Begin
{
% initialization O(V)
\For{$u \in G.V$}
{
$u.key = \infty$ \\
$u.\pi = $ \Nil
}
$r.key = 0$ \\
$Q = G.V$ \\
% ... O(?)
\While{$Q \neq \emptyset$}
{
$u = \ExtractMin(Q)$ \\
\For{$v \in G.Adj[u]$}
{
\If{$v \in Q$ \textnormal{and} $w(u, v) < v.key$}
{
$v.\pi = u$ \\
$v.key = w(u, v)$
}
}
}
}
\end{algorithm}
\subsection{Analysis}
Assuming the use of the algorithm described in
\ref{ch:heaps|sub:priorityqueues}, then the initialization part on lines 3--8
contributes $O(V)$ to the running-time.
The loop on lines 9--17 are run exactly $|V|$ times. Since each
\texttt{ExtractMin} costs $O(\lg V)$, this loop contributes $O(V \lg V)$. The
loop on lines 11--16 runs exactly $|E|$ times. We maintain a priority queue
membership bit, such that the check is constant time $\Theta(1)$. The key
update is an implicit \texttt{DecreaseKey}, taking $O(\lg V)$, and thus we
have $O(V \lg V + E \lg V) = O(E \lg V)$.
\subsection{Proof}
By the generic minimum spanning tree algorithm \ref{alg:generic-mst}, Prim's
algorithm build on its principles, and as such implicitly maintains the set
$A$ from the generic algorithm as
\begin{align}
A = \{ (v, v.\pi) : v \in V - \{r\} - Q \}
\end{align}
When the algorithm terminates, the priority queue has been exhausted and the
minimum spanning tree $A$ for $G$ is thus
\begin{align}
A = \{ (v, v.\pi) : v \in V - \{r\} \}
\end{align}
The algorithm maintains a three-part loop invariant:
\begin{enumerate}
\item $A = \{ (v, v.\pi) : v \in V - \{r\} - Q \}$.
\item The vertices already placed into the MST are those in $V - Q$.
\item For all vertices $v \in Q$, if $v.\pi \neq $ \texttt{nil}, then
$v.key < \infty$ and $v.key$ is the weight of a light edge $(v, v.\pi)$
connecting $v$ to some vertex already in the MST.
\end{enumerate}
Line 10 identifies a vertex $u \in Q$ incident on a light edge that crosses
the cut $(V - Q, Q)$ --- except the first, due to line 7. Removing $u$ from
$Q$ adds it to the set $V - Q$, thus $(u, u.\pi)$ becomes a member of $A$.
The inner loop maintains the third invariant by updating the $key$ and $\pi$
attributes of every vertex adjacent to $u$ that is not in the tree.