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

438 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

In this video, we're going to prove our first performance guarantee on

Â the union-find data structure with path compression.

Â This is the bound first established by Hopcroft and Ullman.

Â Let me briefly remind you about the guarantee.

Â Consider a union-find data structure where you're using lazy unions.

Â You're doing union by rank and you're using the path compression optimization.

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

Â The guarantee is that over the course of this sequence of operations,

Â the total work that you do is at most m, the number of operations,

Â times the glacially growing function log* of n.

Â Remember, log* n is defined as the number of times you have to apply the logarithm

Â function to n before you get a result which is below 1.

Â Remember, 2 raised to the 65,536,

Â a totally absurd number, log* of that number is merely five.

Â So the theorem is true no matter what m is, no matter how many operations you do,

Â whether you do very few, whether you do a whole lot of them.

Â I'm going to focus on the interesting case where m is at least asymptotically

Â as big as n, where m is omega of n.

Â I'll let you think through in the privacy of your own home

Â why the arguments that we're about to see imply the theorem no matter what m is.

Â So one final comment about this guarantee before we turn to its proof.

Â Notice what the theorem is not saying.

Â The theorem is not saying that every single one of these find and

Â union operations runs in big O of log start and time.

Â And that's because that statement in general, which would be stronger,

Â that statement is false.

Â There are going to be operations in general that take more than log* n time.

Â Now on the one hand we know no operations going to be slower than logarithmic time.

Â Even when we didn't have path compression,

Â no operation was worse than the log n time.

Â And we're not going to do any worse with path compression.

Â So the worst case time bound is log n but

Â some operations may indeed run that slowly.

Â But over a sequence of m operations,

Â the average amount of work we do on a per operation basis is only log* of n.

Â This is exactly the sort of so called amortized analysis that we did in our

Â very first union-find implementation with eager unions.

Â We agreed that particular unions might take linear time, but

Â over a sequence of unions we spent only logarithmic time.

Â So the same thing is here, with the exception that we're getting

Â a radically better on average running time bound of log* n over a sequence.

Â So before I launch into the proof details,

Â let's just spend one slide talking a little bit about the proof plan.

Â And in particular, what is the intuition behind the performance that we're trying

Â to make precise?

Â That we're trying to encode in a mathematical way

Â in the proof that's about to follow?

Â Well if we're hoping to prove a bound better than log n,

Â what we were stuck with without path compression it better be the case that

Â installing all these short cuts really qualitatively speeds up finds in unions.

Â And on some level it's sort of clear that things have to be sped up.

Â We're replacing an old chain of pointers with a single pointer so

Â you can only go faster.

Â But how do we keep track of this progress?

Â How do we compile this intuition and make it into a rigorous guarantee.

Â So here's the idea.

Â Let's focus on an object x which is no longer a root.

Â At this point it's parent is somebody other than itself.

Â And one thing I want you to remember from our union by rank analysis is that

Â once an object is not a root, its rank is frozen forevermore.

Â Now we proved that in the context when there was no path compression.

Â But again remember we manipulate ranks in exactly the same way when there

Â is path compression.

Â So that's still true.

Â If you're an object and

Â you're no longer a root there is no way you're rank will ever change again.

Â Now what we're certainly hoping is true is that finds that originate, at say,

Â at this object x are running fast.

Â Not only that, they should be getting faster and faster as time goes on.

Â Because we're doing more and more path compression.

Â So here's the big idea.

Â The way we're going to reason about the worst case running time

Â of the find operation, or equivalently,

Â the longest sequence of parent pointers we might have to traverse to get to a root,

Â is we're going to think about the sequence of ranks that we observe

Â as we traverse these parent pointers from an object x up to the root.

Â Let me give you an example, so what would be the worst case situation?

Â Well the worst case would be if we have a data structure, and

Â let's say that the maximum rank is something like 100,

Â the worst case sequence of ranks, the longest sequence we would ever see,

Â would be if we start a find operation at an object with rank zero,

Â we traverse a parent pointer, we get to its parent, it has rank one.

Â We traverse its parent,

Â it has rank two, then three, then four, and so on all the way up to 100.

Â Now remember, ranks have to strictly increase whenever we traverse

Â a parent pointer.

Â As we discussed, that was true with or without path compression.

Â So in the worst case situation,

Â to go from 0 to 100, you have to traverse 100 pointers.

Â So that'd be a bummer.

Â Wouldn't it be nice if every time we traversed a parent pointer,

Â the rank increased, not just by one, but by a much bigger number.

Â So for example, if we went from 0 to 10, to 20, to 30, to 40, and so

Â on, that would guarantee that we'd get to the max ranked node 100 in only ten steps.

Â So again, the bottom line is, if we can have a better, larger lower bound

Â on the gap in ranks between objects and its parent, that implies

Â more rapid progress through the possible ranks of the nodes that we can see.

Â And it translates to faster finds, to fewer parent pointer traversals.

Â So with that idea in mind, the big gaps between ranks imply rapid progress,

Â I want to propose as a progress measure for a given non-root object x.

Â The gap between x's rank, which again remember is is frozen forevermore,

Â and the rank of its current parent.

Â So this progress measure is a good one for two reasons.

Â First as we just discussed, if you have a handle on this gap,

Â if you can lower bound it then that gives you an upper bound on the search time.

Â Secondly, this gap allow us to quantify

Â the benefit of path compression of installing these shortcuts.

Â Specifically, whenever you install a new short cut you

Â rewire an object's parent pointer to point higher up in the tree.

Â Its new parent is going to have rank strictly bigger than its old one.

Â That means this gap is only going to get bigger.

Â Summarizing path compression improves this progress measure.

Â That is if an object x previously had a parent p and

Â then has its parent pointer rewired to a different node p prime the rank of p

Â prime is bigger than the rank of p.

Â And just to make sure this is crystal clear let's just draw a couple of cartoon

Â examples.

Â So first just in the abstract, if you think about an object x.

Â Suppose it has some parent p and suppose the root of the tree is some p prime.

Â So some ancestor strictly further up in the tree.

Â Remember, ranks always increase as you go up the tree,

Â as you follow the parent pointers.

Â So that means p's rank is strictly bigger than x.

Â p prime's rank is the important thing, is strictly bigger than p.

Â So when you rewire x's parent pointer to point from p no longer but

Â rather to p prime, it acquires a new parent, and its

Â new parent is an ancestor of its old one, ergo it must have strictly bigger rank.

Â For that reason, the gap between x's rank, which is fixed forevermore, and

Â the rank of its new parent.

Â That gap is bigger than the gap between its rank and the rank of its old parent.

Â You can also see this effect in action with

Â the running seven object example we were using in the last video.

Â So I've shown that example, that tree in pink, both before and

Â after path compression.

Â And, again, before path compression, I just defined the ranks as with union by

Â rank so the rank of each node is equal to the longest path from a leaf to that node,

Â and then of course we don't change the ranks when we apply path compression.

Â And what do we observe?

Â Where exactly two of the objects had their parent pointer rewired namely objects

Â one and four had their parent pointer rewired to point directly to seven.

Â And as a consequence the gap in rank between object one and its parent

Â has leaped from merely one, the difference between each rank and the rank of four

Â up to three, the difference between its rank and that of its new parent seven.

Â Similarly object four, its rank gap has jumped from one to two.

Â Its rank is only less than its old parent six but

Â it's two less than that of its new parent seven.

Â So believe it or not we actually have the two crucial building blocks for

Â the Hopcraft and Ullman analysis.

Â Building block number one is the rank lemma that we discussed a couple of videos

Â ago, the fact that with or without path compression you can't have too many

Â objects with a given rank r, you can only have at most n over 2 to vr.

Â The second building block is the one we just covered,

Â that every time you update a parent pointer under path compression, the gap

Â has to grow between the rank of that object and the rank of its new parent.

Â The rest of the proof is just an optimal exploitation of those two building blocks,

Â let me now show you the details.

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