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

270 ratings

Stanford University

270 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

Hey, so guess what? We just designed our first dynamic

Â programming algorithm. That linear time algorithm for computing

Â the max weight independence set in a path graph is indeed an instantiation of the

Â general dynamic programming paradigm. Now I've deferred articulating the

Â general principles of that paradigm until now because I think they are best

Â understood through concrete examples. Now that we have one to relate them to,

Â let me tell you about these guiding principles.

Â We will in the coming lectures see many more examples.

Â So the key that unlocks the potential of the dynamic programming paradigm for

Â solving a problem is to identify a suitable collection of sub-problems.

Â And these, sub-problems have to satisfy a number of properties.

Â In our algorithm for computing max weight independent sets and path graphs, we had

Â N plus one sub problems, one for each prefix of the graph.

Â So formally, our Ithi sub problem in our algorithm, it was to compute the max

Â weight independent set of G sub I, of the path graph consisting only of the first I

Â vertices. So the first property that you want your

Â collection of subproblems to possess is it shouldn't be too big.

Â It shouldn't have too many different subproblems.

Â The reason being is, in the best case scenario, you're going to be spending

Â constant time solving each of those subproblems, so the number of subproblems

Â is a lower bound than the running time of your algorithm.

Â Now, in the Maximum Independent Set example, we did great.

Â We had merely a linear number of subproblems, and we did indeed get away

Â with a mere constant work for each of those subproblems, giving us our linear

Â running time bound overall. The second property you want and this

Â one's really the kicker, is there should be a notion of smaller subproblems and

Â larger subproblems. In the context of independence sets of

Â path graphs this was really easy to understand.

Â The subproblems were prefixes of the original graph and the more vertices you

Â had, the bigger the subproblem. So in general, in dynamic programming,

Â you systematically solve all of the subproblems beginning with the smallest

Â ones and moving on to larger and larger subproblems.

Â And for this to work, it better be the case that, at a given subproblem.

Â Given the solutions to all of the smaller sub problems it's easier to confer what

Â the solution to the current sub problem is.

Â That is solutions to previous sub problems are sufficient to quickly and

Â correctly compute the solution to the current sub problem.

Â The way this relationship between larger and smaller subproblems is usually

Â expressed is via recurrence and it states what the optimal solution to a given

Â subproblem is as a function of the optimal solutions to smaller subproblems.

Â And this is exactly how things played out in our independent set algorithm.

Â We did indeed have a recurrence. It just said, that the optimal value, the

Â maxwood independence head value for G sub I.

Â Was the better of two candidates. And we justified this using our thought

Â experiment. Either you just inherit the maximum

Â independence set value from the preceding sub problem from the I-1 sub problem.

Â Or you take the optimal solution from two sub problems back from GI-2.

Â And you extend it by the current vertex, V sub I.

Â That is, you add the [INAUDIBLE] vertices weight to the weight of the optimal

Â solution from two sub problems back. So this is a pattern we're going to see

Â over and over again. We'll define subproblems for various

Â computational problems. And we'll use re, recurrence to express

Â how the optimal solution of a given subproblem depends only on the solutions

Â to smaller subproblems. So just like in our independent set

Â example once you have such a recurrence it naturally leads to a table filling

Â algorithm where each entry in your table corresponds to the optimal solution to

Â one sub-problem and you use your recurrence to just fill it in moving from

Â the smaller sub-problems to the larger ones.

Â So the third property, you probably won't have to worry about much.

Â Usually this just takes care of itself. But needless to say, after you've done

Â the work of solving all of your sub problems, you better be able to answer

Â the original question. This property is usually automatically

Â satisfied because in most cases, not all, but in most cases the original problem is

Â simply the biggest of all of your subproblems.

Â Notice this is exactly how things worked in the independent sets.

Â Our biggest subproblem G sub N was just the original graph.

Â So once we fill up the whole table, boom.

Â Waiting for us in the final entry was the desired solution to the original problem.

Â So I realize, you know this is a little abstract at the moment.

Â We've only have one concrete example to relate to these abstract concepts.

Â I encourage you to revisit this again after we see more examples and we will

Â see many more examples. Something that all of the forthcoming

Â examples should make clear is the power and flexibility of a dynamic programming

Â paradigm. This is really just a technique that you

Â have got to know. Now when you're trying to devise your own

Â dynamic programming algorithms, the key, the heart of the matter is to figure out

Â what the right sub problems are. If you nail the sub problems usually

Â everything else falls into place in a fairly formulaic way.

Â Now if you've got a black belt in dynamic programming you might be able to just

Â stare at a problem. And intuitively know what the right

Â collection of subproblems are. And then, boom, you're off to the races.

Â But, of course, you know? For white belts in dynamic programming,

Â there's still a lot of training to be done.

Â So rather, in the forthcoming examples. Rather than just plucking the subproblems

Â from the sky. We're going to go through the same kind

Â of process that we did for independent sets.

Â And try to figure out how you would ever come up with these subproblems in the

Â first place? By reasoning about the structure of

Â optimal solutions. That's a process you should be able to

Â mimic in your own attempts at applying this paradigm to problems that come up in

Â your own projects. So, perhaps you were hoping that once you

Â saw the ingredients of dynamic programming, all would become clearer why

Â on earth it's called dynamic programming and probably it's not.

Â So, this is an anachronistic use of the word

Â programming. It doesn't mean coding in the way I'm

Â sure almost all of you think of it. It's the same anachronism in phrases like

Â mathematical or linear programming. It more refers to a planning process, but

Â you know for the full story let's go ahead and turn to Richard Bellman

Â himself. He's more or less the inventor of dynamic

Â programming, you will see his Bellman-Ford Algorithm a little bit later

Â in the course. So he answers this question in his

Â autobiography and he's says, he talks about when he invented it in the 1950's

Â and he says those were not good years for mathematical research.

Â He was working at a place called Rand, he says we had a very interesting gentleman

Â in Washington named Wilson who was the Secretary of Defense.

Â And he actually had a pathological fear and hatred of the word research.

Â I'm not using the term lightly. I'm using it precisely.

Â His face would suffuse, he would turn red, and he would get violent if people

Â used the term research in his presence. You can imagine how we felt then about

Â the term mathematical. So the Rand Corporation was employed by

Â the Air Force, and the Air Force had Wilson as its boss, essentially.

Â Hence, I felt I had to do something to shield Wilson and the Air Force from the

Â fact that I was really doing mathematics inside the Rand Corporation.

Â What title, what name could I choose? In the first place I was interested in

Â planning and decision making, but planning, it's not a good word for

Â various reasons. I decided therefore to use the word,

Â Programming. Dynamic has a very interesting property

Â as an adjective, in that it's poss, impossible to use the word dynamic in a

Â pejorative sense. Try thinking of some combination that

Â will possibly give it a pejorative meaning.

Â It's impossible. Thus I thought dynamic programming was a

Â good name. It was something not even a congressman

Â could object to so I used it as an umbrella for my activities.

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