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

440 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

At the beginning of the course, we talked about the sequence alignment problem,

Â a problem which is fundamental to modern computational genomics.

Â And we talked about the need for an efficient algorithm for solving that

Â problem, for finding the best alignment of two strings.

Â I'm pleased to report that at this point we're well prepared to give such an

Â algorithm. Indeed, such an efficient solution will

Â readily fall out of the dynamic programming recipe that we now have quite

Â a bit of practice with. So let me briefly jog your memory about

Â the sequence alignment problem. So the goal here is to compute a

Â similarity measure between strings, a similarity measure defined as the total

Â penalty of the best alignment, also known as the Needleman-Wunsch score.

Â So for example. if you've given as input the strings A G,

Â G, G C T at A G, G C A, a natural candidate alignment would be to stack

Â them on top of the other, inserting a gap in the shortest string after the two Gs,

Â that some sense represent the missing G. This is a pretty good alignment that

Â suffers from merely two flaws. So first of all, we did resort to adding

Â a gap in the second string. Second of all, there is a mismatch in the

Â final column. The A and the T get mismatched.

Â In general we evaluate the alignment by summing up the penalties of all the flaws

Â and the sum penalty per gap and the sum penalty per mismatch.

Â So, a bit more precisely, as input in this computational problem, we're given

Â two strings. I'm going to call them capital X and

Â capital Y. I'm going to use little x and little y to

Â denote the individual characters of these strings.

Â Let's say the first string capital X has linked M and the second string Y has

Â linked N. In addition to the two input strings we

Â assume we're given as input the values for the various types of penalties.

Â So that we know exactly how much it costs each time we insert a gap.

Â And for each possible mismatch, we need to know exactly what's the cost of that

Â mismatch. In principle you could be given a penalty

Â for matching a letter with itself, but typically that's going to be a penalty of

Â zero. The space of feasible solutions are just

Â the ways of inserting gaps into the two strings so that the results have equal

Â length. I should emphasize that you're allowed to

Â insert gaps into both of the strings. In the example, we only inserted into one

Â of the two strings, but in general, you might have an input

Â where one string is seven characters longer than the other,

Â and it might turn out that in the optimal alignment, the best thing to do is insert

Â three gaps at various places in the longer string,

Â and ten gaps at various places into the shorter string.

Â And the goal, of course is just to compute amongst all of the exponentially

Â many alignments, the one that minimizes the total penalty, where total penalty is

Â just the sum of individual penalties for the inserted gaps and the various

Â mismatches. So let's not be unduly intimidated by how

Â fundamental this problem is, and let's just apply the dynamic recipe,

Â the programing recipe that we have been using all along.

Â Now remember, the really key insight in any dynamic programing solution is

Â figuring out what's the right collection of sub problems.

Â And if you're feeling like your up to the black belt level in dynamic programing,

Â you might just want to try to guess what are the right collection of sub problems

Â for sequence alignment? But I don't expect you to be able to do

Â that at this point. And so, as usual we're going to derive

Â the correct collection of sub-problems. And we're going to do it by reasoning

Â about the structure of an optimal solution,

Â narrowing it down to a small number of candidates composed in various ways from

Â solutions to smaller sub-problems. Once we've figured out the small number

Â of possibilities for what the optimal solution could look like in terms of

Â solutions to smaller sub problems, we'll be able to drive a recurrence which in

Â effect just does brute force search through the small number of candidates.

Â And from the recurrence we'll be able to back out.

Â We'll be able to reverse engineer what are the various sub problems that we

Â actually care about and that we have to solve.

Â So let's do a thought experiment. What does the optimal solution have to

Â look like? And again, remember, this is exactly what

Â it is that we're trying to compute but that's not going to stop us from

Â reasoning about it. If someone handed to us on a silver

Â platter the optimal solution, what would it have to look like?

Â So consider any old pair of strings, capital X and capital Y, and an optimal

Â alignment of them. Let's visualize this optimal alignment as

Â follows. Let's write down the string X plus

Â whatever gaps get inserted into it on top, and right beneath it we'll write

Â down the string Y, with whatever gaps are inserted into it.

Â These two things have exactly the same length.

Â So to figure out the various cases of the structure for this optimal solution,

Â let's reason by analogy with the problems we've already solved.

Â So back when we were looking at independent sets of line graphs, our case

Â analysis was well, either the final vertex, the rightmost vertex of the path,

Â is in the optimal solution or it's not. In the knapsack problem, we said well

Â either the last item is in the optimal solution or it's not.

Â So we always looked at sort of the last part of the optimal solution, in some

Â sense the rightmost position. And happily, staring at this alignment,

Â we see we can once again focus just on the action in the right most, in the

Â final position. So now I have a question for you.

Â So in the independent set problem, there were two cases.

Â The last vertex was either in the optimal solution or it's not.

Â In the knapsack problem, there were also two cases, the final item was either in

Â the optimal solution or it's not. So my question for you is, in the

Â sequence alignment problem: when we focus on what's going on in the final position

Â of the optimal alignment, how many relevant cases do we have to study?

Â So the answer I'm looking for is B, three relevant possiblities for the contents of

Â the final position. Let me explain my reasoning, let's start

Â with the upper parts of the final position.

Â observe that if that's a character of the string capital X, it can only be the very

Â last character. It can only be little X sub N.

Â That's because that's where this string ends.

Â Now we don't know that little X sub N is in the final position, there might be a

Â gap. Similarly, in the bottom part of this

Â final position, there's two possibilities.

Â There's a gap, or, if it's a character of y, it has to

Â be the final character, little y, sub n. So that one seems to suggest four

Â possibilities, two options for the top,

Â two options for the bottom. But the hint of talking about relevant

Â possibilities is that it's totally pointless to have two, a gap in both the

Â top and the bottom. Why?

Â Well the penalty for gaps is non-negative, so if we just deleted both

Â of those gaps we'd get an even better alignment of X and Y.

Â And in studying an optimal solution, we can therefore assume we never have two

Â gaps in a common position. So that leaves exactly three cases.

Â It could be there's no gaps at all, that in fact this alignment matches the

Â character, little x of m, with little y sub n.

Â Or it could match the final character of capital X, with a gap.

Â Or it could match the final character of capital Y with a gap.

Â So the hope behind this case analysis is that we're going to be able to boil down

Â the possibilities for the optimal solution to merely three candidates,

Â one candidate for each of the three possibilities for the contents of the

Â final position. That would be analogous to what we did in

Â both the independent set and knapsack problems, where we boiled the optimal

Â solution down to just one of two candidates, corresponding to whether

Â either the final vertex or the final item, as the case may be, was in the

Â optimal solution. Another way of thinking about this is

Â we'd like to make precise the idea that if we just knew what was going on in the

Â final position, if only a little birdy would tell us which of the three cases

Â that we're in, then we'd be done by just solving the some smaller sub-problem

Â recursively. So let's now state for each of the three

Â possible scenarios for the final position,

Â what is the corresponding candidate for the optimal solution, the way in which,

Â it must necessary be composed with an optimal solution to a smaller sub

Â problem. So who are going to be the protagonists

Â in our smaller sub-problem? Well, the smaller sub-problem's going to

Â involve everything except the stuff in the final position.

Â So it's going to involve the string's X and Y, possibly with one character

Â remaining. So let's let x prime be x, with its final

Â character peeled off. Y prime's going to be y, with its final

Â character peeled off. So let me just remind you of how I

Â numbered the three cases. So case one is when the final position

Â contains the final characters of both of the two strings, that is, when there's no

Â gaps. Case two is when x, little x of n gets

Â matched with the gap and case three is when little y of n gets matched with the

Â gap. Alright, so let's suppose that case one

Â holds. This means that the contents of the final

Â position, includes both of the characters little x sub m and little y sub n.

Â So now what we're going to do is we want to look at a smaller sub problem.

Â And we want to look at the sub problem induced by the contents of all of the

Â rest of the positions. We're going to call that the induced

Â alignment. Since we started with an alignment, two

Â things that had to equal length, and we peeled off the final position of both, we

Â have another thing that has equal link so we're justified in calling it an

Â alignment. Now what is it an alignment of?

Â Well if we're in case one, that means what's missing from the induced alignment

Â are the final characters. little X of M and little Y's of N,

Â which means the induced alignment is a bona fide alignment of X prime and Y

Â prime. And certainly, what we're hoping is true,

Â is that the induced alignment is in fact, an optimal alignment of these smaller

Â strings x prime and y prime. This would say that when we're in case

Â one, the optimal solution to the original problem is built up in a simple way from

Â an optimal solution to a smaller sub problem.

Â We're of course hoping that something analogous happens in cases two and three.

Â The only change is going to be that the protagonists of the sub-problem will be a

Â little bit different. In case two, the thing which is missing

Â from the induced alignment is the final character of x.

Â So, it's going to be the induced alignment of x prime and y.

Â Similarly, in case three, the induced alignment is going to be an alignment of

Â x and y prime. So, this is an insertion, this is a

Â claim, it's not completely obvious, though the proof isn't hard, as I will

Â show you on the next slide. But assuming for the moment that this

Â assertion is true, it fulfills the hope we had earlier.

Â It says that indeed, the optimal solution can only be one of three candidates, that

Â one for each of the possibilities for the contents of the final position.

Â Alternatively it says, that if we only knew which of the three cases we were in,

Â we'd be done, we can recurse, we could look up a solution to a smaller sub

Â problem and we could extend it in an easy way to a optimal solution for the

Â original problem. So lets now move onto the proof of this

Â assertion. Why is it true that an optimal solution

Â must be built up from an optimal solution to the relevant smaller sub-problem?

Â Well all of the cases are pretty much the same argument so I'm just going to do

Â case one, the other cases are basically the same.

Â I invite you to fill in the details. So it's going to be the same type of

Â simple proof by contradiction that we used earlier, when reasoning about the

Â structure of optimal solutions for the independent set in knapsack problems.

Â We're going to assume the contrary, we're going to assume that the induced solution

Â to the smaller subproblem is not optimal, and from the fact that there is a better

Â solution for the subproblem, we will extract a better solution for the

Â original problem, contradicting the purported optimality of the solution that

Â we started with. So when we're dealing with case one, the

Â induced alignment is of the strings X prime and Y prime, X and Y with the final

Â character peeled off. And so for the contradiction, let's

Â assume that this induced alignment, it has some penalty, say capital P. Let's

Â assume it's not actually an optimal alignment of X prime and Y prime.

Â That is, suppose if we started from scratch we'd come up with some superior

Â alignment of X prime and Y prime, with total penalty P star, strictly smaller

Â than P. But if that were the case, it would be a

Â simple matter to lift this purportedly better alignment of x prime and y prime

Â to an alignment of the original strings x and y.

Â Namely we just reuse the exact same alignment of x prime and y prime, and

Â then in the final position, we just match x m with y n.

Â So what is the total penalty of this extended alignment of all of x and y?

Â Well, it's just the penalty incurred in everything but the final position, and

Â that's just the old penalty p star, plus the new penalty incurred in the final

Â position. And that's just the penalty corresponding

Â to the match of the characters x m and y n.

Â P star being less than p, of course p star plus alpha x m y n is

Â less than p plus alpha x m y n. But this second term is simply the total

Â penalty incurred by our original alignment of X and Y, right?

Â That alignment incurred penalty capital P, just in the induced alignment of X

Â prime Y prime, and it's total penalty was just that plus the penalty in the final

Â position, which is this alpha xn, yn. But that furnishes the contradiction that

Â we suppose that we started with an optimal alignment of X and Y,

Â yet here is a better one. So with that contradiction, it completes

Â the proof of the optimal substructure claim.

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