-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathMinimumSpanningTrees.html
267 lines (244 loc) · 9.44 KB
/
MinimumSpanningTrees.html
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
<html>
<!-- THIS FILE WAS GENERATED BY A SCRIPT: DO NOT EDIT IT! -->
<head>
<link href="style.css" rel="stylesheet" type="text/css"/>
<title>
Design and Analyis of Algorithms: Minimum Spanning Trees
</title>
</head>
<body>
<div id="header">
<div id="logo">
<img src="graphics/Julia.png">
</div>
<div id="user-tools">
<a href="index.html">Home</a>
<a href="about.html">About</a>
<a href="feedback.html">Feedback</a>
</div>
</div>
<h1>
Design and Analyis of Algorithms: Minimum Spanning Trees
</h1>
<div style="text-align:center">
<p>
<img src="">
</p>
</div>
<h2>
Overview
</h2>
<p>
<b>Tree</b>: A connected graph with no cycles.
<br>
Given a graph G, any tree that includes all of the vertices
of G is called a <i>spanning tree</i>. The lowest-weight tree
that does that is a <i>minimum spanning tree</i>.
<br>
<br>
These are used to solve problems such as:
</p>
<ul>
<li>Lowest cost way to bring a package between two
cities.
</li>
<li>Most efficient way to connect two components on a
circuit board.
</li>
<li>Most efficient way to connect two strangers in a
social network.
</li>
</ul>
<p>
This problem can be solved using a greedy algorithm.
<br>
<br>
It runs on a weighted graph. (On an unweighted graph, all
spanning trees are minimal: tree property E = V - 1.)
</p>
<h2>
The Generic MST Algorithm
</h2>
<p>
Generic-MST(<i>G</i>, <i>w</i>)
<br>
<i>A</i> = ∅
<br>
<b>while</b> <i>A</i> does not form a spanning tree
<br>
fine an edge (<i>u</i>, <i>v</i>) that is
safe for <i>A</i>
<br>
<i>A</i> =
<i>A</i> ∪ {(<i>u</i>, <i>v</i>)}
<br>
return <i>A</i>
<br>
</p>
<p>
<b>Loop invariants:</b>
<br>
</p>
<h3>
Finding a "safe" edge
</h3>
<p>
How do we find a safe edge?
<br>
One must exist, since we are working with a connected graph,
and <i>A</i> is at all times a part of an MST.
<br>
<b>Definition</b>:
a cut (<i>S</i>, <i>V</i> - <i>S</i>) of an
undirected graph <i>G</i> = (<i>V</i>, <i>E</i>) is a
partition of <i>V</i>.
<br>
<b>Definition</b>:
an edge (<i>u</i>, <i>v</i>) crosses the cut (<i>S</i>,
<i>V</i> - <i>S</i>) if one of its
endpoints is in <i>S</i> and the other in <i>V</i>.
<br>
<b>Definition</b>:
a cut <b>respects</b> a set <i>A</i> of edges
if no edge in <i>A</i> crosses
the cut.
<br>
<b>Definition</b>:
An edge is a <b>light edge</b> crossing a cut if its weight is
minimum among all edges crossing the cut. Uniqueness is not
required.
</p>
<h2>
<a id="Kruskal">
Kruskal's Algorithm
</a>
</h2>
<p>
<img src="graphics/MinSpanTree1.png">
<br>
<br>
We add edges in increasing-cost order, so long as the edges
don't create a cycle (are "safe").
<br>
Steps:
</p>
<ul>
<li>Include the edge weighted 1.
</li>
<li>Include both edges weighted 2.
</li>
<li>Include the edge weighted 3.
</li>
<li>Include the upper
and lower edges weighted 4.
(Not the middle one!)
</li>
<li>Include the lone edge weighted 5.
</li>
<li>Include the edge weighted 6.
</li>
</ul>
<p>
<img src="graphics/MinSpanTree2.png">
<br>
<br>
<b>Proof</b>: Is Kruskal's algorithm guaranteed to always
find the minimum spanning tree?
<br>
Yes, it is. Let's prove it.
<br>
We suppose that graph <i>G</i> has <i>n</i> vertices.
Then our algorithm will create a tree <i>T</i>
with edges <i>e<sub>1</sub></i>, <i>e<sub>2</sub></i>,
... <i>e<sub>n - 1</sub></i>, where
<i>w(e<sub>1</sub>) ≤ w(e<sub>2</sub>)
≤ ... w(e<sub>n - 1</sub>)</i>.
<br>
Suppose that there is a tree <i>T*</i> with a lesser
weight.
<br>
Let <i>e<sub>k</sub></i> be the first edge in <i>T</i>
that is not in <i>T*</i>.
<br>
Now we insert <i>e<sub>k</sub></i> in <i>T*</i>. This will
produce a cycle in <i>T*</i>, by the nature of trees.
There must be some edge <i>e<sup>*</sup></i> that is in
<i>T*</i> but not in <i>T</i> (otherwise <i>T</i> would
have a cycle).
<br>
<img src="graphics/Kruskal.png">
<br>
But the weight of <i>e<sub>k</sub></i> must be less than
the weight of <i>e<sup>*</sup></i>, because after we had
inserted <i>e<sub>1</sub></i> through
<i>e<sub>k - 1</sub></i>, we could have next chosen
<i>e<sup>*</sup></i>... but we did not. Instead we chose
<i>e<sub>k</sub></i>.
<br>
So <i>T*</i> does not have a lesser weight after all.
</p>
<h4>
How to find safe edges
</h4>
<ul>
<li>Place each vertex in its own set. (We now have a
<i>forest</i> of one-vertex, unconnected trees.)
</li>
<li>We examine edges (<i>u, v</i>) in non-decreasing order.
</li>
<li>If <i>u</i> and <i>v</i> are not in the same set (tree), add the
edge. (It is safe.)
</li>
<li>If <i>u</i> and <i>v</i> are in the same set, adding the edge
would create a cycle (by the nature of trees, E = V
- 1).
</li>
</ul>
<h2>
Prim's Algorithm
</h2>
<p>
The big difference from Kruskal: in Kruskal, we have a set of
edges, perhaps with no connections, that <i>eventually</i>
turns into a tree.
<br>
With Prim, we continually have an ever-growing tree. In fact,
it is always an MST of a sub-graph of our graph.
</p>
<h2>
Source Code
</h2>
<p>
<a href="https://github.com/gcallah/algorithms/tree/master/Python/MinimumSpanningTrees">Python</a><br>
</p>
<h2>
For Further Study
</h2>
<ul>
<li>
<a
href="https://en.wikipedia.org/wiki/Minimum_spanning_tree">
Minimum Spanning Tree (Wikipedia)
</a>
</li>
<li>
<a href="https://www.youtube.com/watch?v=YyLaRffCdk4">
Prim's Algorithm (Khan Academy)
</a>
</li>
</ul>
<h2>
Homework
</h2>
</body>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-97026578-2', 'auto');
ga('send', 'pageview');
</script>
</html>