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

438 ratings

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 4

Advanced dynamic programming: the knapsack problem, sequence alignment, and optimal binary search trees.

- Tim RoughgardenProfessor

Computer Science

So in these next two videos we'll give our second application of the dynamic

Â programming paradigm.

Â It's to a quite well known problem, it's called the knapsack problem.

Â And we'll show how following the exact same recipe that we used for

Â computing independent sets in path graphs

Â leads to the very well known dynamic programming solution to this problem.

Â So let's jump right into the definition of a knapsack problem.

Â So the input is given by n items.

Â So each item comes with a value,

Â V sub i, the more the better for us, and also a size, W sub i.

Â We're going to assume that both of these are non-negative, for

Â the sizes we're going to make additional assumption that their integral.

Â In addition to these two n numbers,

Â we are given one more which is called the capacity, capital W,

Â again we'll assume this is non-negative and an integer.

Â The role of these integrality assumptions will become clear in due time.

Â The knapsack problem,

Â the responsibility of an algorithm is to select a subset of the items.

Â What do we want?

Â We want as much value as possible, so

Â we want to maximize the sum of the values of the items that we select.

Â So what prevents us from picking everything?

Â Well, the sum of the sizes of the items that we pick have to total to at

Â most the capacity capital W.

Â Now I could tell you some cheesy story about a burglar breaking into a house

Â with a knapsack with a certain size capital W and

Â wants to make away with sort of the best loot possible.

Â But that would really be doing a disservice to this problem which is

Â actually quite fundamental.

Â This problem comes up quite a bit,

Â especially as a subroutine in some larger task.

Â Really, just whenever you have sort of a budget of a resource that you can use, and

Â you want to use it in the smartest way possible,

Â that's basically the knapsack problem.

Â So you can imagine how it would come up in a lot of contexts.

Â So let's now execute a recipe for

Â developing a dynamic programming algorithm.

Â Remember, the key to any dynamic programming solution is to figure out what

Â is the right set of subproblems.

Â And we're going to arrive at the sub problems for

Â the knapsack problem just as we did for max weight independent sets by doing

Â a thought experiment about the optimal solution, and

Â understanding what it has to look like in terms of solutions to smaller subproblems.

Â The bottom line of this thought experiment,

Â the deliverable will be a recurrence.

Â It will be a formula which tells us how the optimal value of

Â one subproblem depends on the value of smaller subproblems.

Â So to begin the thought experiment, fix an instance of knapsack and

Â let capital S denote an optimal solution, a max value solution.

Â We began our previous thought experiment with a content free statement that

Â the final vertex of a path is either in the optimal solution or it's not.

Â Now what is the analog of the right most vertex in this knapsack problem?

Â Well, unlike a path graph,

Â there's no intrinsic sequentiality to the items that we're given.

Â They're just an unordered set.

Â But it's actually useful to think of the items as sort literally ordered one,

Â two, three, all the way up to n, and

Â then the analog of the right most vertex is just the final item.

Â So the content free statement we're going to work with here is either the last item

Â belongs to the optimal solution capital S, or it doesn't.

Â We'll again start with the easy case when it doesn't.

Â What we argued in the path graph problem was that the max weight independent set in

Â the analogous case one has to be optimal if we just delete that rightmost edge from

Â the graph.

Â So here, the analogous claim is that this set S should still be optimal if we delete

Â the final item n from the knapsack instance.

Â The argument is the exact same near trivial contradiction.

Â If there was a different solution, s star,

Â amongst the first n minus 1 items with a weight even bigger than that of s,

Â we could regard this equally well as a superior knapsack feasible solution back

Â with all n of the items, but that contradicts the purported optimality of s.

Â So let's go through the slightly trickier case two together using a quiz.

Â So suppose the optimal knapsack solution does in

Â fact make use of this final item n.

Â Now we want to talk about this being somehow composed of an optimal

Â solution to a smaller subproblem.

Â So if we're going to delete the last item, then we can't talk about s,

Â because s has the last item, so

Â we need to remove the last item from s before we talk about its optimality.

Â That's analogous to back in the independent set problem,

Â we removed the right most vertex from the optimal solution before talking about its

Â optimality in a smaller subproblem.

Â So the question then is if we take capital S, the optimal solution,

Â we remove item n, in what sense is the residual solution optimal?

Â Put differently, for what kind of knapsack instance, if any,

Â is that an optimal solution for?

Â All right, so the correct answer is C.

Â So back in the independent set problem what we said is if we remove the right

Â most vertex, then what's left is optimal for the residual independent

Â set problem we get by plucking off the right most two vertices.

Â Here when we remove the nth item from the optimal solution S,

Â the claim is what we get is optimal for the knapsack problem involving the first

Â n-1 items and a residual knapsack capacity of W-w sub n.

Â So the original knapsack capacity with space reserved,

Â or deleted, for the nth item.

Â So before I give you a quick proof,

Â let me just briefly explain why a couple of the other answers are not correct.

Â So first of all, answer B, I hope you could rule out quickly.

Â It just doesn't type check.

Â So capital W, that's the knapsack capacity, so that's in units of size.

Â Little v sub n, that's the item's value.

Â So that's in dollars, so

Â it doesn't really make sense to talk the difference between those two.

Â They're just apples and oranges.

Â Part D, if you were worried about feasibility at any point.

Â So, if you take capital S and you remove the nth item, what have you done?

Â You have taken a set of items whose size was at most capital W by feasibility of s,

Â and you've removed an item with size wn from it.

Â So, what remains has total size at most capital W minus little w sub n.

Â So, S minus n wouldn't be feasible for

Â this reduced to this residual knapsack capacity W- w sub n.

Â A much more subtle point is part A, that's a very natural one to guess.

Â That turns out to not be correct.

Â So it turns out there might be smarter ways of using the first n-1 items,

Â than S minus item n, if you had a full knapsack capacity of W to work with.

Â So that's a subtler point.

Â And it's a good exercise for

Â you to actually convince yourself that A is wrong.

Â That there's no reason that when you take out item n from S and

Â you still keep using the original knapsack capacity that this has to be optimal.

Â That's not going to be true.

Â All right, so why is capital C correct?

Â Well, this is going to have the same spirit of case two of our weighted

Â independent set thought experiment.

Â So let me give you the proof.

Â The proof is going to be the usual contradiction analogous to case 2 of our

Â argument in the weighted independent set problem.

Â So suppose there was something better than S with n removed with the residual

Â capacity, W- wn.

Â Call that supposedly better solution S*, so what can we do to get a contradiction?

Â Well, let's just take S* which involves only the first n-1 items.

Â Let's add item n to it since S* has total size and most W-w sub n and

Â item n has size W sub n, the result has total size of W so

Â it's a feasible solution to take S* and extend it by item number n.

Â And if S* had more value than S with n removed,

Â then S* with n included has more value than S.

Â So for example, if S had total value 1,100,

Â 100 of which was coming from the nth item, then S with n removed had value 1000.

Â If S* was better, it had a value 1,050.

Â Well then we just put n back in and it has value 1150 which contradicts the purported

Â optimal value of S* which had total value merely 1100.

Â So notice what's going on here.

Â So in taking away W sub n for the knapsack capacity before we look at the residual

Â problem we're in effect reserving a buffer for item n if we ever need it.

Â That's how we know we're feasible when we stick n back into the solution S*.

Â That's analogous to deleting the penultimate vertex of the path,

Â again as a buffer to ensure feasibility when we include the nth vertex back in

Â independent set problem.

Â So what was the point of this whole thought experiment which we've now

Â completed?

Â Well, again the point was to say the optimal solution,

Â whatever it is, it has to have only one of two forms.

Â We've narrowed the list of candidates down to two possibilities.

Â Either you just inherit the optimal solution with one less item in the same

Â knapsack capacity, or you look at the optimal solution with one less item and

Â less knapsack capacity by W sub n, and you extend that by item n.

Â But those are the only two possibilities.

Â So again, if we only knew which of these two cases were true.

Â If we only knew whether or not item n was in the optimum solution or

Â not, in some sense we could recursively compute the rest of the solution.

Â So just as that was enough to get us going with a dynamic programming algorithm for

Â weighted independent sets, so it goes with knapsack,

Â as I'll show you on the next video.

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