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

433 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 2

Kruskal's MST algorithm and applications to clustering; advanced union-find (optional).

- Tim RoughgardenProfessor

Computer Science

So, by now we've spent quite a bit of time talking about the minimum cost

Â spanning tree problem. There's a number of motivations why we do

Â that. the first, it's just a uniquely great

Â problem for the study of greedy algorithms.

Â You can propose a bunch of different greedy algorithms, and quite unusually,

Â it's all of them seem to work. So you get correct greedy algorithms, but

Â it's quite subtle what's driving their correctness.

Â You also get a lot of practice, arguing about graphs, and arguing about exchange

Â arguments, and so on. The second reason, it's been worthwhile

Â spending time on these algorithms. It has given us some more practice with data

Â structures and how to use them to speed up algorithms,

Â namely heaps, to speed up Prim's algorithm,

Â to union-find data structure to speed up Kruskal's algorithm.

Â The third reason that we're talking about them is they have applications in their

Â own rights. That's the subject of this video and the

Â next and we're going to focus on applications to clustering problems.

Â So let me begin by just talking about the goal of clustering informally.

Â And then, I'll let you pin me down to a precise objective function on the next

Â slide. So in a clustering problem, the input is

Â n points that we think of as being embedded in space.

Â And it's actually quite rare that in the underlying problem that we care about is

Â it actually intrinsically geometric? Is it actually intrinsically points in

Â space? Usually, we're representing something we

Â care about. Maybe it's web pages.

Â Maybe it's images. Maybe it's a database as points in space.

Â And given a bunch of objects, we want to cluster them into, in some sense coherent

Â groups. For those of you coming from a machine

Â learning background, you'll often hear this problem referred to as unsupervised

Â learning, meaning the data is unlabeled. We're looking for just patterns and data

Â where the data is not annotated. This obviously is a fairly wishy-washy

Â description of a problem, so let's be a little bit more precise.

Â We're going to assume that part of the input is what we're going to call a

Â similarity measure. Meaning for any two objects, we're

Â going to have a function giving us a number indicating how similar or really

Â rather how dissimilar they are to each other.

Â In keeping with the geometric metaphor, we're going to refer to this function as

Â a distance function. One thing that's cool is we don't need to

Â impose many assumptions on this distance function.

Â The one thing we're going to assume is that it's symmetric,

Â meaning the distance from p to q is the same as the distance from q to p.

Â So what are some examples? Well, if you want to really go with the

Â geometric metaphor, if you're representing these points as in a space

Â RM for some dimension M, you can just use the Euclidean distance or if you prefer

Â some other norm, like say, l1 or l-infinity.

Â In many application domains, there are widely accepted similarity or distance

Â measures. one example would be for sequences, as we

Â discussed in introductory lecture the penalty of the best alignment between two

Â genome fragments. So now that we have this distance

Â function, what would it mean to have coherent groups?

Â Well, things which have small distance from each other which are similar, they

Â should generally be in the same group, and things that are very dissimilar that

Â have large distance between them, you would expect to mostly be in different

Â groups. So how can we evaluate how good a

Â clustering is, how well it's doing with the job of putting nearby points together

Â and dissimilar points in different groups?

Â Well, to be honest there's many ways of approaching and formalizing that problem.

Â The approach we're going to take is an optimization-based approach.

Â We're going to positive and objective function on clusterings and then seek out

Â the clustering that optimizes that objective function.

Â I want to warn you that's not the only way to approach the problem.

Â There are other interesting approaches, but optimization is a natural one.

Â Furthermore, just like in our scheduling application, there's more than one

Â objective function that people study and that is well-motivated.

Â So one very popular objective function would be, say the k-means objective,

Â which I encourage you to look up and read more about.

Â For this lecture, I'm just going to adopt one specific objective function.

Â It's natural enough, but it's by no means the only one or even the primary one.

Â But it'll serve the purpose of studying a natural greedy algorithm related to

Â minimum spanning tree algorithms which is optimal in a precise sense.

Â So let me develop the terminology needed to state the objective function and the

Â optimization problem precisely. One issue that always comes up in

Â clustering problems is how many clusters are you going to use?

Â So to keep things simple in this video, we're going to assume that part of the

Â input k indicates how many clusters you're supposed to use.

Â So we're going to assume you know a priori how many clusters you want.

Â So in some application domains, this is a totally reasonably assumption.

Â You know, you might know for example that you want exactly two clusters, the k = 2.

Â in some domains you may have you know, good domain knowledge from past

Â experience that you know how many clusters you expect to need that's, all

Â fine and good. Also you know, in practice, if you don't

Â really know what k is supposed to be, you can go ahead and run the algorithm we're

Â going to talk about for a bunch of different values of k and then you

Â symmetric or just eyeball it to figure out which is the best solution.

Â So the objective function we're going to look at is defined in terms of separated

Â pairs of points. That is points that are assigned to

Â distinct clusters. Now you know, if you have more than one

Â cluster, inevitably there's going to be some pairs of points.

Â Some points are going to be one groups, other points are going to be in the

Â different group. So separated points are inevitable and

Â the most alarming separated points are the ones that are the most similar, the

Â ones that have the smallest distance. If points are separated, we want them to

Â be for apart, so we're particularly concerned with

Â nearby points that are separated. So that's going to be our objective

Â function value, called the spacing of a clustering.

Â It's the distance between the closest together pair of separated points.

Â Now what do we want from the spacing of a clustering?

Â Well, we want all of the separated points to be as far apart as possible.

Â That is, we want the spacing to be big. The bigger the better.

Â So that naturally motivates the formal problem statement.

Â You're given as inputs, the, distance measure.

Â You're told the distance between each pair of points.

Â You're also told the desired number of clusters.

Â Amongst all ways of clustering the points into k clusters, find the clustering

Â which makes the spacing as big as possible.

Â So let's develop a greedy algorithm that seeks to make the spacing as big as

Â possible. And to facilitate the discussion, I'll

Â use an example point set with just six black points up here in the upper right

Â part of the slide. Now, the good idea behind this greedy

Â algorithm is to not worry about the constraints that, at the end of the day,

Â we can only output k different clusters. We're actually going to be infeasible,

Â we'll have too many clusters throughout the algorithm.

Â Only at the conclusion of the algorithm will we be down to k clusters, which will

Â be our final infeasible solution. So that frees us up to initialize the

Â procedure where the degenerate solution, where each point is in its own cluster.

Â So in our example point set, we have these six pink isolated clusters.

Â In general, you're going to have n clusters and we've got to get down to k.

Â Now, let's remember what the spacing objective is.

Â In the spacing objective, you go over all of the separated pairs of points.

Â So for this degenerate solution, it's all pairs of points.

Â And you look at the most alarming separated pair, that is those, that are

Â the, the closest to each other. So the spacing is the distance between

Â the closest pair of separated points. Now, in a greedy algorithm, you want to

Â increase your objective function as much as possible.

Â But actually, things are pretty cut and dried in this scenario.

Â Suppose I give you a clustering and you want to make the spacing bigger,

Â the only way you can do that is by taking the currently closest pair of separated

Â points and making them not separated any more.

Â That is putting them in the same cluster. So, it's in some sense obvious what you

Â want to do to make the objective function go up at all.

Â You gotta look at the pair of points that is defining the objective,

Â the closest pair of separated points, and you have to fuse them.

Â You have to fuse their clusters so that they're no longer separated.

Â In this example, it looks to me like the closest pair of points, which of course

Â are separated, is this pair in the upper right.

Â So, if we want to make the spacing bigger, then we fuse them into a common

Â cluster. [SOUND] So we started with six clusters.

Â Now, we're down to five. So now we reevaluate the spacing again of

Â this new clustering. We ask, what is the closest pair of

Â separated points? So that would seem to me to be this pair

Â in the bottom right. [SOUND] And again, the only way we can

Â increase the spacing by merging clustering is to merge these two isolated

Â clusters into one. Now, we do it again. We say, which pair

Â of points determines the current spacing, which the currently separated pair of

Â points that are nearest to each other? That to me would look like this pair

Â that's on the rightmost part of the picture.

Â The only way to merge two clusters and make the spacing actually go up is to

Â merge the clusters containing the pairs of points determining the current

Â spacing. So in this case, two different clusters

Â with two points, each would collapse into a single cluster with four points.

Â Let's assume that we wanted three clusters, clusters anyways, which is

Â where we are. So at this point, the greedy algorithm is

Â going to halt. So let me now spell out the pseudocode of

Â the greedy algorithm more generally, but it's exactly what you'd expect given

Â the discussion so far. All right.

Â So, I'd like you to stare at this pseudocode for ten seconds or however

Â long you need and try to relate this to an algorithm that we've seen in the

Â course. In particular, an algorithm that we've

Â seen quite recently. I hope it reminds you strongly of an

Â algorithm we've already covered. Specifically, I hope you see a strong

Â resemblance between this greedy algorithm and Kruskal's algorithm for computing a

Â minimum cost spanning tree. Indeed, we can think of this greedy

Â clustering algorithm as being exactly the same as Kruskal's minimum cost spanning

Â tree algorithm except aborted early. Stopped when there's k components

Â remaining, that is before the final k - 1 edge

Â additions. So, just to make sure the correspondence

Â is clear. What is the graph?

Â What are the edges? What are the edge costs?

Â Well, the objects in the clustering problem, that is the points.

Â Those correspond to vertices in a graph. The other part of the input of the

Â clustering problem are distances, which are given for every pair of points.

Â So those play the role that edge costs were playing in the minimum spanning tree

Â problem. Since we have a distance or an edge cost

Â for every single pair of points, we can think of the edge set in the

Â clustering problem as being the complete graph because we have an edge cost or a

Â distance for every single pair. So this type of agglomerative clustering

Â has a name. This idea of fusing components one at a

Â time using MST-lik criterion. It's called single-link clustering.

Â So a single n clustering is a good idea. If you work at all with clustering

Â problems or unsupervised learning problems, it definitely should be a tool

Â in your toolbox. we're going to justify its existence in

Â one particular way in the next video, when we show that it does indeed maximize

Â the spacing over all possible k clusterings.

Â But even if you don't care about the spacing objective function per se, you

Â want to be familiar with single-link clustering.

Â It has many other nice properties as well.

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