8000 Prims mst + script · DontFearAlgorithms/Algorithms-1@9ba69f2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9ba69f2

Browse files
committed
Prims mst + script
1 parent c42ef6c commit 9ba69f2

File tree

2 files changed

+66
-13
lines changed

2 files changed

+66
-13
lines changed

slides/graphtheory/prims_mst.key

74.8 KB
Binary file not shown.

slides/graphtheory/prims_mst_script.txt

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -284,42 +284,95 @@ to the PQ.
284284
The very last thing is to make sure our tree spans the whole graph and return
285285
the edges comprising the MST along with the MST cost.
286286

287-
@103.
288-
So that's all for the lazy implementation, now let's talk about the eager
289-
version and what optimization it brings.
287+
@103
288+
So that's all for the lazy implementation, in the next video we'll be talking
289+
about the eager version and what optimizations we can do.
290+
291+
292+
293+
294+
295+
296+
-------------------------------------------------------------------------------
297+
298+
@105.
299+
Hello and welcome back, my name is William and today we're talking about the
300+
eager version of Prim's minimum spanning tree algorithm.
301+
302+
@106.
303+
In the previous video on Prim's algorithm I go over MST basics and the lazy
304+
implementation of Prim's. This video builds off concepts explained there, so
305+
please watch that video before this one, there should be a link in the
306+
description.
307+
308+
@107.
290309
The lazy implementation of Prim’s inserts E edges into the PQ. This results in
291310
each poll operation on the PQ to be O(log(E)).
292311
In the eager version we maintain the idea that instead of adding edges to the PQ
293312
which could later become outdated as the algorithm continues to visit nodes that
294313
we should instead track (node, edge) key-value pairs that can easily be updated
295314
and polled to determine the next best edge to add to the MST.
296315

297-
@104.
298-
for this all to make sense there's a key realization that needs to happen and
299-
that is that: for any MST with directed edges, each node is paired with
316+
@108.
317+
For this all to make sense there's a key realization that needs to happen and
318+
that is: for any MST with directed edges, each node is paired with
300319
exactly one of its incoming edges (except for the start node).
301320
One way to see this is on a MST with possibly multiple edges leaving a node, but
302321
only ever one edge entering a node.
303322

304-
@105.
323+
@109.
305324
Let's have a closer look at what I mean. Suppose we have this undirected graph.
306325

307-
@106.
326+
@110.
308327
The equivalent directed version looks like this.
309328

310-
@107.
329+
@111.
311330
A possible MST starting at node 0 looks like the following.
312331

313-
@108.
332+
@112.
314333
Now notice that on this directed MST each node is paired with exactly one edge,
315334
well except the starting node.
316335

317-
@109.
336+
@113.
318337
So in a sense, there seems to be a relationship we can take advantage of here
319338
which is that each node is paired with exactly one incoming edge.
320339

321-
@110.
322-
340+
@114.
341+
In the eager version we are trying to determine which of a node's incoming edges
342+
we should select to include in the MST.
343+
The main difference coming from the lazy version is that instead of adding edges
344+
to the PQ as we iterate over the edges of node we’re going to relax (update) the
345+
destination node’s most promising incoming edge.
346+
347+
@115.
348+
So you might be asking yourself the question how are we going to efficiently
349+
update and retrieve these (node, edge) pairs?
350+
Well one possible solution is to use an Indexed Priority Queue (IPQ) which can
351+
efficiently update and poll key-value pairs. Think of an indexed priority queue
352+
as the data structure you'd get if a hashtable and a priority queue had a baby
353+
together. It supports sorted key-value pair update and poll operations in
354+
logarithmic time.
355+
Using this new approach would reduce the overall time complexity from O(E*logE)
356+
to O(E*logV) since there can only be V (node, edge) pairs in the IPQ.
357+
358+
@116.
359+
If you're interested in learning more about what an indexed priority queue is
360+
and how it's implemented I highly recommend my data structures video on the
361+
subject. I will link it in the description if you need to catch up.
362+
363+
@117.
364+
The new implementation for the Eager version is slightly different, the
365+
algorithm goes as follows:
366+
Maintain an indexed priority queue of size V that sorts vertex edge pairs
367+
(v comma e) based on the minimum edge cost of e.
368+
Start the algorithm on any node s. Mark s as visited and relax all edges of s.
369+
Relaxing in this context refers to updating the entry for node v in the IPQ from
370+
(v comma old edge) to (v comma new edge) if the new edge has a better cost than
371+
the old edge.
372+
Then while the IPQ is not empty and a MST has not been formed, dequeue the next
373+
best (v, e) pair from the IPQ. Mark node v as visited and add edge e to the MST.
374+
Lastly, relax all edges of v while making sure not to relax any edge pointing to
375+
a node which has already been visited.
323376

324377

325378

0 commit comments

Comments
 (0)
0