@@ -284,42 +284,95 @@ to the PQ.
284
284
The very last thing is to make sure our tree spans the whole graph and return
285
285
the edges comprising the MST along with the MST cost.
286
286
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.
290
309
The lazy implementation of Prim’s inserts E edges into the PQ. This results in
291
310
each poll operation on the PQ to be O(log(E)).
292
311
In the eager version we maintain the idea that instead of adding edges to the PQ
293
312
which could later become outdated as the algorithm continues to visit nodes that
294
313
we should instead track (node, edge) key-value pairs that can easily be updated
295
314
and polled to determine the next best edge to add to the MST.
296
315
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
300
319
exactly one of its incoming edges (except for the start node).
301
320
One way to see this is on a MST with possibly multiple edges leaving a node, but
302
321
only ever one edge entering a node.
303
322
304
- @105 .
323
+ @109 .
305
324
Let's have a closer look at what I mean. Suppose we have this undirected graph.
306
325
307
- @106 .
326
+ @110 .
308
327
The equivalent directed version looks like this.
309
328
310
- @107 .
329
+ @111 .
311
330
A possible MST starting at node 0 looks like the following.
312
331
313
- @108 .
332
+ @112 .
314
333
Now notice that on this directed MST each node is paired with exactly one edge,
315
334
well except the starting node.
316
335
317
- @109 .
336
+ @113 .
318
337
So in a sense, there seems to be a relationship we can take advantage of here
319
338
which is that each node is paired with exactly one incoming edge.
320
339
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.
323
376
324
377
325
378
0 commit comments