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 3

Huffman codes; introduction to dynamic programming.

- Tim RoughgardenProfessor

Computer Science

So, having out iterated through all of our algorithm design paradigms and

Â noticing that none of them seem to work very well for computing the maximum

Â weight independent set of a path graph, we're going to develop some ideas for a

Â new paradigm. And the key approach in this new paradigm

Â is to first reason about the structure of an optimal solution.

Â What I mean by this is we're going to seek out statements of the following

Â form. The optimal solution, whatever it may be,

Â has to possess a certain form. It has to have been built up from an

Â optimal solution to a sub-problem in a prescribed way.

Â So in fact, in much of our discussion of both divide and conquer and greedy

Â algorithms, this kind of reasoning was implicit.

Â With dynamic programming, we're going to make it systematic.

Â For example, implicit in the correctness of many of divide and conquer algorithm

Â is the fact that an optimal solution to the whole problem has to be expressible,

Â has to be constructable in a prescribed way from solutions to smaller

Â sub-problems. So, what's the motivation for doing this

Â thought experiment, trying to understand what the optimal solution could possibly

Â look like? Well, the plan is we're going to narrow

Â the possible candidates for the optimal solution down to a relatively small set

Â of candidates. With a small set, we can get away with

Â brute force search to pick the best one. So, one lesson you learn once you get

Â good at dynamic programming, is that it's not at all circular to reason about the

Â very object that you're trying to compute.

Â Remember, the goal here is to devise an algorithm to compute an optimal solution.

Â And now, I'm telling you to do a thought experiment as if you had already computed

Â it, as if I had handed it to you on a silver platter.

Â But that kind of daydreaming can be very productive.

Â And thinking hey, what if I did have an optimal solution?

Â What could I say about it? What would it look like?

Â Observations of that form can actually light up a trail to the computation of

Â that exact object and we'll see that in the next couple videos.

Â Alright so that's enough loft, lofty philosophy for now, lets get concrete.

Â Okay, so we've got this path graph, the vertices have weights.

Â We want the max weight independent set. Let's again do this thought experiment.

Â What if someone handed to us, what could we say about it's structure?

Â We'll be using the following notation when we reason about this maximum weight

Â independent set. S denotes the vertices in that optimal

Â solution, in that max weight independent set and we're going to let V sub n denote

Â the right most, the final vertex of the input graph.

Â So, here's a content-free statement. This last vertex of the path, V sub n.

Â Either it's an S or it isn't. So, that's going to give us two cases,

Â when we reason about the optimal solution.

Â Let's start with the case where Vn is excluded from the optimal solution

Â capital S. So, let's let G prime denote the path

Â graph you get by plucking off Vn, plucking off the right-most vertex from

Â the original graph G. So, let's make a couple of trivial

Â observations. So, first of all, this set, capital S,

Â it's an independent set in G. It doesn't include the last vertex.

Â So, we can regard this set as equally well as an independent set of the smaller

Â graph G prime. If it didn't contain consecutive vertices

Â in G, nor does it in G prime. But actually, we can say more.

Â Not only is S any old independent set in G prime.

Â It has to be an optimal, that is, maximum weight independent set in G prime.

Â Why? Well, if there was something better than

Â S in G prime, we could take that exact same independent set S star regarded as

Â an independent set in G and, of course, it would still be better than S in G.

Â But that contradicts our assumption that S was a max weight independent set in G.

Â So summarizing, if the max weight independent set S of the original path

Â graph G does not include the right-most vertex, it can be very simply described

Â in terms of an optimal solution to a smaller sub-problem.

Â It literally is a max weight independent set of G prime, the path graph with one

Â fewer vertex. Alright. So, case one is a warm up for

Â case two, which is similar but slightly more complicated.

Â Now, let's assume that the maximum independent S does, in fact, use the

Â right-most vertex, Vn. Now, by the definition of an independent

Â set, no two consecutive, no two adjacent

Â vertices can be chosen. So, by virtue of choosing, choosing V sub

Â n, the right-most vertex S, in this case, absolutely cannot include the penultimate

Â vertex V sub n - 1. So, we want to know by G double prime,

Â the path you get from G by plucking off both of the right-most two vertices.

Â So now, let's do our best to mimic the argument in case one.

Â In case one, we said well, S has to also be an independent set of G

Â prime. Now, here that doesn't make sense, right?

Â Here, S contains the last vertex so we can't talk about it even being a subset

Â of any smaller graph. However, if we think about S except for

Â the last vertex of V sub n, S with Vn removed is an independent set, in fact,

Â of G double prime, because remember, S can't contain the second to last vertex.

Â And once again, just like in case one we can say something stronger, S with Vn

Â removed is not any old independent set of G double prime, it actually must be an

Â optimal one. It must have maximum possible weight. The

Â reasoning is similar. Suppose S with Vn removed was not the best possible

Â independent set in G double prime. Then there's something else called an S

Â star which is even better, has even bigger weight.

Â How do we get a contradiction? Well, if we just add Vn to this even

Â bigger independent set S star that lies in G double prime, we get a bonafide

Â independent set of the entire graph G with overall weight even bigger than that

Â of S, but that contradicts the optimality of S.

Â So, for example you could imagine that this reported optimal solution capital S

Â had total weight 1,100 in two parts, it had 1,000 weight coming from vertices in

Â the G double prime, but it also had V sub n which had weight 100 on its own. And so

Â now, in the contradiction, you say, well, suppose there was an independent set that

Â had even more than 1,000 weight just in G double prime, say, 1,000 and 50.

Â Well then, we just add this last vertex V sub n to that, we get an independent set

Â with weight 1,150 and the original graph G.

Â But that contradicts the fact that S was supposed to be off more with weight

Â nearly 1,100. So, notice that the reason were using the

Â graph G double prime in this argument is to make sure that no matter what S star

Â is, no matter what this independent set of G

Â double prime is. We can just add V into it blively and not

Â worry about feasibility, right?

Â So, the right-most vertex S star could possibly possess is the third to last

Â vertex Vn - 2. So, there's no worries about feasibility

Â when we extend it by adding in the right-most vertex V sub n.

Â So, to make sure you don't lose the forest for the trees, let me just point,

Â let me remind you what our high level plan was, and let me point out that we

Â actually just executed that plan quite successfully in this problem.

Â The plan was to narrow down the candidates for what the optimal solution

Â could be. To reason about the form of the optimal

Â solution and argue that it has to look in a particular way.

Â What did we just prove on the previous slide?

Â We said that the optimal solution actually can only be one of two things.

Â Either it excludes the final vertex and it is nothing more than the max weight

Â independent set of G prime, or if it includes the right-most vertex than it

Â must be, a maximum weight independent set frp, G double prime augmented with this

Â last vertex V sub n. There's only two possibilities for what

Â the optimal solution could possibly look like, in terms of optimal solutions of

Â smaller sub problems. So, a corollary of this is that if a

Â little birdie told us which case we were in, whether or not V sub n, V sub n was

Â in the optimal solution, we could just complete this by recursing on the

Â appropriate sub-problem. The little birdie tells us the optimal

Â solution doesn't have Vn we just recurse on G prime.

Â If the little birdie tells us v sub n is in the optimal solution, we recurse on G

Â double prime and then add V sub n to the result.

Â Now, of course, there is no little birdie and we have no idea whether this

Â right-most vertex is in the optimal solution or not.

Â But hey, there is only two possibilities, right?

Â Here is an idea. Maybe it seems crazy, but why not try

Â both possibilities and just return whichever one is better?

Â Why do I suggest that maybe this is crazy? Well, if you stare at this and you

Â think about it and you think about the ramifications of trying both

Â possibilities as you recursed down the graph, this may start feeling a little

Â bit like brute force search. And, in fact it is.

Â This is just a recursive organization of a brute force search.

Â Nevertheless, as we'll see in the next video, if we're smart about eliminating

Â redundancy, we can actually implement this idea in a linear time.

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