The primary topics in this part of the specialization are: greedy algorithms (scheduling, minimum spanning trees, clustering, Huffman codes) and dynamic programming (knapsack, sequence alignment, optimal search trees).

Loading...

From the course by Stanford University

Greedy Algorithms, Minimum Spanning Trees, and Dynamic Programming

292 ratings

Stanford University

292 ratings

Course 3 of 4 in the Specialization Algorithms

The primary topics in this part of the specialization are: greedy algorithms (scheduling, minimum spanning trees, clustering, Huffman codes) and dynamic programming (knapsack, sequence alignment, optimal search trees).

From the lesson

Week 1

Two motivating applications; selected review; introduction to greedy algorithms; a scheduling application; Prim's MST algorithm.

- Tim RoughgardenProfessor

Computer Science

Okay, so it's time to discuss our first minimum spanning tree algorithm namely

Â Prim's algorithm. Definitely a candidate for the greatest

Â hits compilation. And again remember even though it's

Â called Prim's algorithm, it was actually discovered earlier by Jarnik.

Â So how's it work? Well before showing you any pseudo code,

Â let's first illustrate it on an example. As we go through the example, I hope that

Â the similarities to Dijkstra's shortest path algorithm will be evident.

Â I'm going to work with the same example graph from the previous video with four

Â vertices and five edges. The plan is to grow a tree one edge at a

Â time. And we're going to keep growing this tree

Â like a mold. We're going to start from just a seed

Â vertex. And then we're going to suck up one new

Â vertex with each iteration of the algorithm.

Â So, this is similar to Dijkstra's Algorithm.

Â In Dijkstra's Algorithm, it was clear where we should grow the initial mold

Â from, because we were given a source vertex, that they're trying to compute

Â the shortest paths out of. We have no source vertex in the minimum

Â spanning tree problem, but it turns out that we can just pick an arbitrary vertex

Â to start. Doesn't matter which one, which is cool.

Â So the plan is in E generation we're going to add one edge and span one new

Â vertex adjacent to the ones we're already spanning.

Â Now as a greedy algorithm Prim is simply going to select the cheapest edge that

Â allows it to span one additional new vertex.

Â Now the start of the algorithm here we're not really spamming anything.

Â We are sort of thinking of ourselves as growing from and currently spanning the

Â vertex in the upper right. So what are the edges in which we can

Â span an adjacent vertex? Well, there is two inches.

Â There is the top inch that costs one then we'll span an addition in the upper left

Â vertex or the is the edge with cost two on the right.

Â If we include that, we'll be able to span the vertex in the bottom right.

Â So we're not going to be greedy, we're just going to choose the cheaper edge,

Â the edge of cost one. Now, the vertices that our tree thus far

Â spans are the top two vertices. So, in the next iteration, we want to add

Â one more edge [COUGH] to span one additional new vertex.

Â And now we see that there are three edges sticking out of what we're spanning thus

Â far that will allow us to span a new edge.

Â There's the edges that have cost two, three, and four.

Â The two and the three will allow us to span the vertex in the bottom right.

Â If we pick the four, that will allow us to span the vertex in the bottom left.

Â Yeah, and we're going to be greedy, so of these three candid edges, we're going to

Â pick the cheapest one which is the edge of cost two.

Â So now the mold that we've been growing is in effect, covers all of the verticies

Â except for the one in the bottom left. So now in the final iteration we want to

Â include one more edge so that we span that final remaining vertex.

Â The one in the bottom left. Note that there's there was this edge of

Â cause three that we never added. But it got sucked up into the tree that

Â we grew anyways. So we're going to go ahead and ignore

Â that. Adding the three wouldn't allow us to

Â span any more vertices. In fact, it would create a loop which we

Â don't want. So we're going to say, okay.

Â We'll have the two edges that would allow us to span an extra vertex.

Â There's the four and there's the five. We're going to be greedy,

Â we're going to pick the four. And once we have the edges of the cost

Â one and two and three and four we have a spanning tree there's no loops there's a

Â path from any vertex to any other vertex along the pink edges, the cost is seven

Â you might recall from the previous video this is indeed the minimum cost spanning

Â tree of this graph. Of course, the fact that we have this

Â simple procedure that works correctly in this toy example, which is four vertices

Â and five edges, really means nothing. I mean you shouldn't draw any immediate

Â conclusions that this is a good algorithm in general even though that is going to

Â be the case. So let's next go and actually define the

Â algorithm generally. So if you have a general graph, what does

Â it mean to start somewhere and grow a mold, span one more vertex each

Â iteration, always proceeding greedily until you are done.

Â So lets spell out the pseudo code on the next slide.

Â So here is Prim's minimum spanning tree algorithm.

Â We're going to start with just two lines of initialization.

Â We're going to maintain a set of vertices, capital X.

Â This is meant to the be the vertices that we span so far.

Â Again, we need some seed vertex from which to start the process.

Â It doesn't matter where, which one we pick.

Â We're going to get the same tree no matter what, so just call it little s.

Â That's an arbitrary vertex from which we start growth.

Â The other thing we're maintaining is, of course, the tree.

Â So that's initially going to be empty. We're going to add one edge to it in each

Â iteration. An invarient that we are going to

Â maintain throughout the algorithm is that the edges that currently reside in the

Â set capital T span the verticies that currently reside in the set capital X.

Â Then we're going to have our main while loop.

Â this is the workhorse of the algorithm. And it's very similar to the one in

Â Dijkstra's algorithm. Namely, each iteration is responsible for

Â picking one edge crossing the current frontier.

Â advancing to include one new vertex. And again, it's going to be greed.

Â The criterion's going to be different, in fact, simpler, than with Dijkstra's

Â Algorithm instead of looking at links. We're just going to say, what's the

Â cheapest edge that allows us to span a new vertex?

Â So the loop's going to keep going, as long as there are vertices that we don't

Â yet span. And then what we do is we search to the

Â edges that allow us to span a new vertex. So which edges are those?

Â Well we want there to be one endpoint in the set X of vertices we already have our

Â tree spanning and we want the other end point to be non-redundant, so we want it

Â to be outside of X. So if we have an edge that crosses the

Â frontier in this sense, one endpoint in X, in endpoint outside that's how we

Â increase the number of spanned vertices by one in an iteration.

Â So if E is the cheapest edge amongst all of those that cross the front here with

Â one end point on either side, that's the one we're going to add to our tree so far

Â capital T in this iteration, it's end point that's not already in capital X,

Â that's going to be the very text that we add to X in this iteration.

Â And again the semantics of an iteration is that we're trying to increase the

Â number of spanned vertices while paying as little as possible, that's the sense

Â in which a prim's algorithm is a greedy algorithm.

Â So as usual with a greedy algorithm, this seems natural enough, but it's not at all

Â clear that it's correct, that it always computes in minimum spanning tree.

Â In fact, if you think about it's not even obvious, it actually computes a spannin

Â tree at all, minimum or otherwise, but it is correct.

Â Let's make that statement precise on the next slide.

Â So the key claim is that Prim's Algorithm is correct.

Â Given any connected input graph, it is guaranteed to output a spanning tree with

Â minimum possible cost. So before we delve into any details, let

Â me just finish this video by telling you about the proof plan.

Â We're going to prove this theorem in two parts.

Â First, we're going to establish that it outputs some spanning tree.

Â Maybe, maybe not minimum. Even that's non trivial.

Â Then we'll worry about arguing that the spanning tree output actually is one of

Â minimum cost. Both parts of the proof are interesting.

Â For part one to argue that we output some spanning tree, we're going to review some

Â preliminaries about graphs and about cuts and about spanning trees and graphs.

Â For part two to argue optimality, we're going to rely on a very neat property of

Â spanning trees, minimum spanning trees called the cut property.

Â I'm happy to report so that the work that we do here and in both parts will bear

Â further fruit later we're going to reuse these ingredients when we prove the

Â correctness of another MST algorithm named McCrustgrals algorithm.

Â For those of you who would much rather talk about running time than correctness

Â don't worry your time will come after we wrap up this correctness proof I'll

Â address how do you implement prim's algorithm quickly in particular using

Â heaps we'll get the running time down to the near linear bound of O of M log n.

Â Coursera provides universal access to the worldâ€™s best education,
partnering with top universities and organizations to offer courses online.