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

373 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, welcome to this optional sequence of videos on state of the art implementations

Â of the union to find data structure.

Â Now as advanced in optional material,

Â let me make a few comments before we get started.

Â So the first comment is I'm going to assume that you're in a particularly

Â motivated mood.

Â Now, no one's forcing you to take these algorithm courses, so

Â I'm always assuming that you're very motivated individual,

Â but that's going to hold even more true than usual in these advanced videos.

Â And the way that plays out is while I'm going to hold myself to the same exacting

Â standards of clarity that I usually do, I'm going to ask a little bit more of you,

Â the viewer.

Â I'm going to perhaps dot a few less Is, cross a few less Ts than I usually do.

Â So you may find yourself periodically needing to pause and

Â think through some of the details that I'm glossing over.

Â The second comment is I'm going to give is sort of short shrift to applications

Â of the union find data structure.

Â That's not because there aren't any, there's plenty of applications of this

Â data structure but for these videos we're really just going to immerse ourselves in

Â the beauty and the depth of ideas behind the design of the data structures and

Â especially the analysis of their performance.

Â The final comment is to keep in mind that this is some seriously

Â next level material.

Â It is totally normal that the first time you see this stuff, you find it confusing.

Â You find it difficult to understand.

Â So confusion should not discourage you.

Â It does not represent any intellectual failing on your part.

Â Rather, keep in mind, it represents an opportunity to get even smarter.

Â So, with those comments in mind, let's go ahead and

Â proceed to a different approach to the Union-find data structure via lazy unions.

Â So let's have one slide with a quick review of what we already know about

Â the Union-find data structure.

Â Recall we discussed this in the context of a fast implementation of

Â Kruskal's minimum spanning tree algorithm The raison d'etre of a union-find

Â data structure is to maintain a partition of a universe of objects.

Â So in the context of Kruskal's algorithm, the objects were the vertices, and

Â the groups we wanted to maintain were the connected components with respect to

Â the edges we committed ourselves to so far.

Â The data structure should support two operations.

Â No prizes for guessing the names of those two operations.

Â First is the find operation.

Â This is given an object from the universe return the name of that object's group.

Â So for example if X was in the middle of this square that I put

Â on the right representing the universe capital X we would return a C3.

Â The name of the group that contains X.

Â So in the context of Kruskal's algorithm, we use find operation to check for cycles.

Â How did we know whether adding a new edge would create a cycle with respect to

Â the edges we've already chosen?

Â Well, it would be if and

Â only if the end points of that edge were already in the same connected component.

Â That is, if and only if the two find operations returned the same answer.

Â In the Union Operation you're given out one object but you call them x and y,

Â and then the responsibility of the operation is to merge the groups

Â that contain x and y.

Â So for example in the picture y was in C4, x was as before in C3.

Â Then your job is to fuse the groups C3 and C4 into a single group.

Â In the context of Kruskal's algorithm,

Â we needed this operation when we added a new edge.

Â This fused together two of the existing connecting components into one.

Â So that was exactly the union operation.

Â So we all ready went through one implementation of union and find.

Â That was order to get a blazingly fast implementation of Kruskal's algorithm.

Â So let's just review how that works.

Â With each group, we associated a linked structure, so

Â each object had one pointer associated with it.

Â And the invariant was in a given group,

Â there was some representative leader object.

Â And everybody in that group pointed to the leader of that group.

Â So for example, on the right I'm showing you a group with three objects, x, y, and

Â z, and x would be the leader of this group.

Â All three of the objects point directly to x, and

Â that would just be the output of the find operation, the leader of the group.

Â We use that as the group name.

Â Now the part which is cool and

Â obvious about this approach is our find operations take constant time.

Â All you do is return the leader of the given object.

Â Now the tricky part was analyzing the cost of Union operations.

Â So the problem here is that to maintain the invariant that

Â every object of a group points to its leader.

Â When you fuse two groups, you have to update a bunch of the objects' leaders.

Â We'd only have one leader for the one new group.

Â The simple, but totally crucial, optimization that we discussed was

Â when two groups merge you update the leader pointers of all objects in

Â the smaller group to point to the leader of the bigger group.

Â That is the new fused group inherits the leader

Â from the bigger of its constituent parts.

Â If we do that optimization, it's still the case that a single union might take linear

Â time fade event time but a sequence of n unions takes only big o of n log in time.

Â And that's because each object endures at most a logarithmic number of leader

Â updates, cause every time it's leader pointer gets updated the population

Â of the group that it inhabits doubles.

Â So in this sequence of videos we are going to discuss a different approach to

Â implementing the union find data structure.

Â And let's just go all in and

Â try to get away with updating only a single pointer in each union.

Â All right, well how are we going to do that?

Â Well, I think if we look at a picture,

Â it'll be clear what the approach is going to be.

Â So let's look at a simple example.

Â There's just six objects in the world and currently they're in two groups.

Â One, two, and three are in one group with leader one.

Â Four, five, and six are in the second group with leader four.

Â So, with our previous implementation of union find and the two groups have equal

Â size, so we pick one arbitrarily to represent the new leader.

Â Let's say four is going to be the new leader.

Â And now we update objects one, two, and three so

Â that they point directly to their new leader, four.

Â And so the new idea is very simple let's just sort of as a short hand for

Â re-wiring all of one, two and three to point to four,

Â let's just re-wire one pointer to point to four and then it's understood that two and

Â three as descendants of one also now have the new leader four as well.

Â So as a result, we do again get a directed tree afterwards,

Â but we don't get one with just depth of one.

Â We don't get as shallow, pushy a tree.

Â We get a deeper one that has two levels below the root.

Â So, let me give you another way of thinking about this in terms of an array

Â representation.

Â And here I also want to make a point that why conceptually it's going

Â to be very useful to think of these union find data structure in terms of these

Â directed trees.

Â You're actually implementing this data structure this is not how you'd do it.

Â You wouldn't bother with actually pointers between the different objects.

Â You'd just have a simply array indexed by the nodes.

Â And in the entry corresponding to a node i you would just store the name

Â of i's parent.

Â For instance if we reframe this exact same example in an array representation

Â before we do the union what do we got?

Â Well objects one, two, and three all have parent one.

Â Objects four, five, and six all have parent four.

Â So in the array we'd have a one.

Â In the first three entries and a four in the second three entries.

Â So in the old solution we have to update the parent pointers of objects one two and

Â three, they're all going to point directly to four, so

Â that gives us an array entirely of fours.

Â Whereas in the new solution the only node whose parent

Â pointer gets updated is that of object one, it gets updated from itself to four.

Â And in a particular objects two and

Â three still point to object one not directly to object four.

Â So that's how union works so in simple example.

Â How does it work in general?

Â Well, in general, you're given two objects, each belongs to some group.

Â You can think of these two groups conceptually as directed trees.

Â If you follow all the parent corners, all parent corners lead to the root vertex.

Â We identify those root vertices with the leaders of these two groups.

Â Now when you have to merge the two trees together, how do you do it?

Â Well, you pick one of the groups and

Â you look at its roots currently it points to itself.

Â You can change its parent pointer to point to the other groups leader.

Â That is you install one of the two roots as a new child of the other trees root.

Â So I hope the pros and cons of this alternative approach to the union find

Â data structure are intuitively clear.

Â The win, the pro, comes in the union operation, which is now very simple.

Â Now you might be tempted to just say union takes constant time,

Â because all you do is update one pointer, It's actually not so simple, right?

Â Because remember you're given two objects, x and y, and

Â in general you're not linking x and y together.

Â They might be somewhere deep in a tree.

Â You're linking roots together.

Â So what is true is that the union operation reduces two

Â two indications of find.

Â You have to find x's root.

Â R sub 1, you have to y's root r sub 2,

Â and then you just do constant time linking either r1 to r 2 or vice versa.

Â Now the issue with this lazy union approach is that it's not at all obvious,

Â and in fact it's not going to be true, that find operation takes constant time.

Â Remember, previously when we actually did this hard work of updating all these

Â leader pointers every time we had a union it guaranteed that whenever there is

Â a find boom, we just look in the field, we return the leader pointer, we're done.

Â Here, parent pointers do not point directly to roots rather

Â you have to traverse in general a sequence of parent pointers from a given object x

Â to go all the way up to the root of the correspondence tree.

Â So because of these trade offs.

Â These pros and cons, it's really not obvious at all whether this

Â lazy union approach to the union find data structure is a good idea.

Â Whether it's going to have any pay offs.

Â So that's going to take really some quite subtle analysis which is the main

Â point of the lectures to come.

Â It's also going to take a couple of optimizations and

Â the next video will start with the first one.

Â You might be wondering, okay, so when you do a union and

Â you have to install one root under the other, how do you make that choice?

Â So the right answer is something called union by rank.

Â That's coming up next.

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