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 I don't blame you if you're feeling a little bit restless.

Â You've now watched three videos about this alternative approach

Â to the union find data structure based on lazy unions, and

Â frankly we don't have many changeable things to show for it.

Â We already had a perfectly fine implementation of this data structure

Â based on eager unions that gave us constant time fines, and yeah,

Â union could be linear in the worst case,

Â but it was logarithmic on average over sequence of unions.

Â So now we have this other implementation.

Â Both of our operations are requiring logarithmic time.

Â It's true it's a worst case bound, and it's true union by rank is pretty cool.

Â But still, the bottom line up to this point is not that compelling.

Â But I've still got another trick up my sleeve.

Â Now just watch what happens once we employ a second optimization known as

Â path compression.

Â So the optimization is very natural.

Â I suspect many a serious programmer would come up with this on their

Â own if they were tasked with implementing union find with union by rank.

Â So to motivate this optimization let's think about what would be our worst

Â nightmare as someone maintaining such a data structure and hoping for

Â good performance.

Â Well, remember the running time of a find is proportional to the number of parent

Â pointers you have to traverse.

Â That is disproportional to the depth of the object at which it's invoked.

Â So, what's the worst case find look like?

Â Well it's going to be the leaf.

Â And moreover, it's going to be a leaf,

Â which is furthest away from its corresponding roots.

Â Now, on the one hand we're using union by rank, so

Â we know that this depth can't be too bad.

Â It's going to be at most big 0 of log n.

Â However, there will be example there will,

Â in general, be leafs that are theta of log n hops away from their root.

Â And, for all we know, we're just going to get this endless sequence of find

Â operations, where every single one is invoked on

Â a leaf that's a log n number of hops away from from its root.

Â So for example, in the pink tree that is shown in the upper right of the slide

Â maybe someone keeps searching for the object,

Â keeps invoking find from the object one over and over and over again.

Â And then we're going to be suffering a log number of steps with every single

Â operation.

Â But if you think about it,

Â it's totally pointless to keep re-doing the work of previous finds.

Â To keep retraversing the same parent pointers over and over again.

Â So for example, in the pink tree that I've shown in the upper right,

Â imagine that find is invoked from object one.

Â So then we traverse the three parent pointers.

Â We discover the ancestors four and six before terminating at seven.

Â And we discover that seven is the leader or

Â the root corresponding to the object one.

Â Now remember, we do not care that four and

Â six are ancestors of one, that is uninteresting.

Â We only visited them to discover what we actually cared about,

Â that seven is the leader, the root corresponding to one.

Â Well, but let's just cache that information now that we've computed it.

Â So, this is now basically reconstructing the eager union find implementation.

Â Let's just rewire one's parent corner to point straight to seven.

Â We don't need to recompute foreign six in some later find operation.

Â And more generally, after we've invoked FIND from object one, we may as well

Â rewire four's parent pointer as well, to point directly to its leader, seven.

Â So that then is path compression.

Â When FIND is invoked from some node, x, and you traverse parent pointers from x

Â up to its root, call it r for every object that you visit along

Â this path from x to r, you rewire the parent corner to point directly to r.

Â So r doesnt have it's parent pointer changed, it still points to itself.

Â The penultimate object on this path doesn't have its parent pointer changed.

Â It already was pointing to the root r, but

Â anything below the root and its immediate descendents on this path from x

Â will have its parent point updated and it'll be updated to point directly to r.

Â Now we couldn't get away with this if the tree had to be binary.

Â But it doesn't have to be binary.

Â We couldn't get away with this if we had to satisfy some other constraint like

Â a search tree property, but we don't.

Â So nothing's stopping us from just caching this information

Â about who everyone's leaders are.

Â because that's really the only information we're responsible for

Â exporting from this union find data structure.

Â So pictorially, if you like thinking about the trees, in effect,

Â what path compression does is make the trees shallower and more bushy.

Â So in our example in the upper right, it rips out one and

Â four and makes them immediate descendants of the root seven.

Â Prefer to think in terms of the array representation and remember in the array

Â representation, the array index for i is recording the parent of the object i.

Â So, in our pink tree, five, six, and seven all point to seven.

Â They all have parent seven.

Â Whereas, two and three all have parent five.

Â Four has the parent six and one has the parent four.

Â And after applying path compression following a find invoked at the object

Â one, one and four are redirected to have parent seven.

Â So what are the pros and cons of the path compression optimization?

Â Well, the cons side is quite minor.

Â Really we're just doing a little multi-tasking on find and

Â that introduces a small constant factor overhead.

Â We're already doing work proportional to the number of hops.

Â On the path from x to it's root and we are still just doing constant work for

Â each node on that path.

Â We're doing an extra constant work for

Â each node after the fact to rewire the parent pointer to point to the root.

Â The pro should be obvious this is going to speed up all subsequent finds operations.

Â You are really making sure you don't redo redundant work.

Â You don't traverse parent pointers over and over and over again.

Â So what's clear is that FINDs will speed up.

Â What's really not clear is whether they'll speed up by a lot.

Â So this one going to affect the performance of the FIND operation by say

Â a factor of two, where we get something fundamentally better

Â than the logger than performance we were stuck with without path compression.

Â So, let me spend a slide on the subtle point of how ranks interact with the path

Â compression optimization.

Â So, the plan is we're going to manipulate rank fields with path

Â compression in exactly the same way as we did when we didn't have path compression.

Â So how did we manipulate them previously?

Â Well at the birth of the union find data structure everybody has rank zero.

Â Ranks never change except possibly when you do a union.

Â When you do a union you look at the roots of the two objects whose groups you

Â have to union.

Â You look at their ranks.

Â If one of them has strictly bigger rank that becomes

Â the root of your new merged tree.

Â The root with the smaller rank is installed as a child underneath.

Â If you union two roots that have exactly the same rank,

Â you pick arbitrarily which of them is the new root, and you bump up its rank by one.

Â That is how we manipulated ranks before,

Â that is exactly how we're going to manipulate them now.

Â So in particular, when we apply path compression,

Â when we rewire parent pointers following a find, we do not touch anybody's ranks.

Â We leave them all exactly the same.

Â So again, ranks only change in one specific case in a very specific way.

Â Namely, during a union when we merge two trees that have exactly the same rank.

Â And when we do that kind of union, that kind of merge,

Â whichever of the old roots winds up being the new root, we bump up its rank by one.

Â That is the only modification we ever make to any ranks.

Â Now, it might bother you that we don't recompute the ranks when we apply

Â path compression.

Â And in a way I sort of hope it does bother you because by not recomputing ranks, by

Â just manipulating them exactly as before, we're actually losing the semantics

Â of what ranks meant in the previous video, so the key invariant we had

Â that ranks exactly represented worst case search time to that object is now lost.

Â The easiest way to see what I'm talking about probably through an example, so

Â let me redraw the same tree we had on the previous slide, both before and

Â after we apply path compression.

Â Following a find invoked at the object one.

Â So what I've done here is, in the top tree, before the path compression's been

Â applied, I've labeled ranks just as they were in the previous video.

Â So for each node in the top tree, its rank is equal to the longest path.

Â From a leaf to that note.

Â Then I applied path compression from he object one

Â resulting in the bushier more shallow tree on the bottom.

Â And as I said I did not touch anybody's ranks when I applied

Â this path compression.

Â I didn't do any recomplications.

Â And you'll see now we've lost the semantics for the ranks.

Â Just to give a simple example there are now leaves that don't have rank zero,

Â they have ranks strictly bigger than zero.

Â So what we can say is that for every node the rank is an upper bound.

Â Not necessarily exactly the same as, but

Â at least as big as the longest path from a leaf to that node.

Â But because of the shortcuts that we've installed we no longer have the equality

Â that we had before.

Â There is good news however.

Â We can definitely salvage a lot of the technology that we

Â developed in last video for the union by rank analysis and

Â apply it fruitfully here to the case with path compression.

Â In particular, any statement that we made last video

Â that talks only about the ranks of objects and not about their parent pointers per se

Â that will be as true with path compression as without.

Â Why?

Â Well, think about making two copies of a union find data structure.

Â Exactly the same set of objects and they'll run them in parallel on exactly

Â the same sequence of union and find operations.

Â So, by definition,

Â we manipulate the ranks of objects in exactly the same way in the two copies.

Â It doesn't matter if there's path compression or not.

Â So at all moments in time, every object will have exactly the same rank in

Â one copy as it does in the other copy.

Â Path compression or no, it doesn't matter.

Â Exactly the same ranks.

Â What's going to be different in the two copies are the parent pointers.

Â They're, in some sense, going to be further along.

Â They're going to point further up into the tree in the copy with path compression.

Â But no matter.

Â Any statement that's purely about ranks, it's going to be equally well true with or

Â without path compression.

Â So in particular, remember the ranking lemma that we proved in the last video.

Â That says there's at most n/2 to the r objects of rank r.

Â That was true without path compression.

Â It's going to be true with path compression.

Â And we're going to use it in the forthcoming analysis.

Â Another fact which is still true with path compression,

Â in fact in some sense it's only more true with path compression, is that whenever

Â you traverse a parent corner you will get to a node with strictly larger rank.

Â Remember in the last video we said, as you go up toward the root,

Â you're going to see a strictly increasing sequence of ranks.

Â Here each pointer with path compression represents a sequence

Â of pointers without path compression.

Â So certainly you will still only be increasing in a rank every time you

Â traverse a pointer.

Â Let's now quantify the benefit of path compression by proving new performance

Â guarantees on the operations and the union find data structure.

Â And frankly, the running time bounds are going to blow away the logarithmic bound

Â that we had when we were only doing union by rank without path compression.

Â We're going to do the analysis in two steps, and

Â it's going to be historically accurate in the sense that I'm first going to show you

Â what theorem by Hopcroft-Ullman, which gives an excellent but

Â not quite optimal bound on the performance of this union find data structure.

Â And then we're going to build on the Hopcroft-Ullman ideas

Â to prove even better bound on the performance on this data structure.

Â So here is the performance guarantee that Hopcroft and

Â Ullman proved back in 1973, about 40 years ago.

Â They said,

Â consider a union find data structure implemented as we've been discussing.

Â Lazy unions, union by rank, path compression.

Â Consider an arbitrary sequence of m, find, and union operations, and

Â suppose the data structure contains n objects.

Â Then the total work that the data structure does to process this entire

Â sequence of m operations is m times log star of n.

Â What is log* of n, you ask?

Â Well, it's the iterated logarithm operator.

Â So, my analogy, remember when we first demystified the logarithm way back at

Â the beginning of part one, we notice the log base two of a number is well,

Â you just type that number in your calculator,

Â you count the number of times you divide by two until the result drops below one.

Â So, log star, you type the number n into your calculator and

Â you count the number of times you need to hit log before the result drops below one.

Â So it totally boggles the mind just how slowly this log star function grows.

Â In fact, let me give you a quick quiz just to make sure that you appreciate

Â the glacial growth of the log star function.

Â So the correct answer is B.

Â It is five.

Â And you should probably spend a couple seconds just reflecting on how absurd

Â this is.

Â Right? So you take this ridiculous number 2

Â to the 65500.

Â Remember the number of atoms in the known universe estimated is way smaller

Â than this.

Â Which is 10 to the 80.

Â So, you take this ridiculous number, and you apply the log star function.

Â You get back 5.

Â Why do you get back 5?

Â Well, take the logarithm once.

Â What do you get back?

Â 65,536, also known as 2 to the 16, so you take log again.

Â You get back 16, also known as two to the four.

Â You take log again.

Â You get back four, also known as two to the two.

Â You take log again.

Â You get back a 2, and then one more time gets you down to 1.

Â So, you can think of log star as the inverse of the tower function,

Â where the tower function takes, think of it as a positive integer, t, and

Â it just raises two to the two to the two to the two to the two t times.

Â And this, in particular, shows that log star, despite its glacial growth,

Â is not bounded by a constant.

Â This function is not.

Â Big o of one.

Â For any constant c you can think of I can in principle exhibit a large enough n so

Â that log star of my n is bigger than your c.

Â Namely I can just set n to be two to the two to the two to the so on c times.

Â Log star of that will be at least c.

Â So that's the Hobcroft-Owen performance guarantee that

Â on average over a sequence of m union of five operations you spend

Â this only ridiculously small log star n amount per operation.

Â We're going to prove it in this next video.

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