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

351 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

The guarantees states that if you implement a union-find data structure

Â using lazy unions, union by rank, and path impression,

Â then you get a pretty amazing guarantee.

Â Do any sequence you want of m union and find operations.

Â The tool that I worked on, that is data structures, is bound and above by

Â times log star of n, where n is the number of objects in the data structure.

Â So on the one hand I hope you're suitably impressed by this result.

Â So the proof of it that we gave in the last video is frankly totally brilliant.

Â And secondly the guarantee itself is excellent, what with

Â log star of n being bounded above by five for any imaginable value of n.

Â On the other hand, I do hope there's sort of a little voice inside you wondering if

Â this log star bound could really be optimal.

Â Can we do better maybe by a sharper analysis of the data structure

Â we've been discussing, or perhaps by adding further ingenuity,

Â further optimizations to the data structure?

Â For all we know, you might be able to get away with a linear amount of work.

Â So O of M worked to process an arbitrary sequence of finds and unions.

Â It turns out you can do better than the guarantee.

Â There is a sharper analysis of the exact same data structure.

Â That analysis was given by Tarjan, and here is the statement of his guarantee.

Â So the improved bound states that for an arbitrary sequence of m union and

Â find operations, the total work done by this data structure union by rank and

Â path compression, is big O of m times alpha of n.

Â Now what, you may ask, is alpha of n?

Â That's a function known as the inverse Ackermann function.

Â So what then is the inverse Ackermann function?

Â Well the short answer is that it's a function that,

Â incredibly, grows still more slowly, in fact, much, much,

Â much more slowly, than the log star function we discussed in the bound.

Â The precise definition is non-trivial, and that is the subject of this video.

Â In the next video, we'll prove this theorem and

Â explain how the inverse Ackerman function arises

Â in an optimal analysis of union by rank with path compression.

Â So I'll first define the Ackerman function,

Â and then I'll describe its inverse.

Â One thing I should warn you is you will see slight variations on these two

Â definitions out there in the literature.

Â Different authors define them in ways convenient for their own purposes.

Â That's also what I'm going to do here, I'm going to take one particular definition

Â convenient for the union find data structure analysis.

Â All of the variants that you'll see out there in the literature exhibit roughly

Â the same ridiculous growth.

Â So the details aren't really important for the analysis of data structures and

Â algorithms.

Â So you can think of the Ackermann function as having two arguments,

Â which I'm going to denote by k and r, both of these are integers.

Â k should be at least zero, r should be at least one.

Â The Ackermann function is defined recursively.

Â The base case is when k equals zero.

Â For all r, we define a sub zero of r as the successor function,

Â that is it takes its input r and it outputs r plus one.

Â In general, when k is strictly positive,

Â the argument r tells you how many times to apply the operator,

Â the function a k minus 1, starting from the input r.

Â That is, it's the r fold composition of a sub k minus 1.

Â And again applied to the argument r.

Â So in some sense describing the algorithm function isn't that hard right?

Â I didn't really need that much of a slide to actually write down its description,

Â but getting a feel for what on earth this function is, takes some work.

Â So the first thing,

Â maybe this is a sanity check, is note that it is a well-defined function.

Â So, you know, you're all programmers, so

Â you could easily imagine writing a program which took as input, k and

Â r, and at least in principle, given enough time, computed this number.

Â It would be a very simple recursive algorithm with a pseudo-code just

Â following the mathematical definition.

Â So it is some function, given k and r,

Â there is some number that's the result of this definition.

Â Now, in the next sequence of quizzes, let's get a feel for

Â exactly how this function behaves.

Â And so let's start on the first quiz by fixing k to be one.

Â And now, viewing the Ackermann function with k fixed at one,

Â is a function purely of r.

Â So, what function does a1 of r correspond to.

Â All right, so the correct answer is b, at least a1 is quite simple to understand.

Â All it does is double the argument.

Â So if you give it r, it's going to spit out 2r.

Â So, why is that?

Â Well, any one of our by definition is just the function a0 applied to r,

Â r times, a0 by definition is the successor function as one, so if you

Â apply the successor function r times, starting from r you get 2r as a result.

Â So let's now move from a1 to a2.

Â So let's think about exactly the same question, fixing k at 2 and

Â regarding it as a function of r only.

Â What function is it?

Â So answer here is c for

Â every positive integer are a2 of r is equal to r times 2 to the r.

Â And the reasoning is the same as before.

Â So remember a2 of r just means you apply a1 r times.

Â In the last quiz we discovered that a1 was doubling.

Â So if you double r times,

Â it has the effect of multiplying by a 2 to the r factor.

Â So let's take it up a notch yet again.

Â Let's bump up k to three and let's think about A sub 3.

Â But let's not yet think about exactly what a sub 3 is as a function of r.

Â Let's keep things simple.

Â What is just a sub 3 evaluated when r equals 2?

Â So k equals 3, r equals 2, that's a number.

Â What number is it?

Â Okay, so the correct answer is the third one.

Â It's 2048, also known as 2 to the 11th.

Â Let's see why, well a sub 3 of 2 that just means we apply a sub 2 twice to 2.

Â Now in the last quiz we evaluated not only a sub 2 of 2 but

Â a sub 2 of all integers r.

Â We discovered that it's r times 2 to the r.

Â So that means that it's a simple matter to evaluate this

Â application of a sub 2 twice.

Â First a sub 2 of 2 that's just 2 times 2 to the 2 that's just 8.

Â Then a sub 2 of 8 is going to be 8 times 2 to the 8, also known as 2 to the 11 or

Â 2048.

Â So what about in general?

Â How does the function a sub 3 behave as a function of a general parameter r?

Â When we answer this question we're going to see for the first time

Â the ridiculously explosive growth that the Ackerman function exhibits and

Â this will just be the tip of the iceberg right?

Â This is just when k equals 3.

Â So by definition a sub 3 of r you start from r and

Â you apply the function a sub 2 r times.

Â So let's remind ourselves what the function a sub 2 is.

Â We computed that exactly.

Â That's r times 2 to the r.

Â So to make out life simple, let's just think about the 2 to the r part.

Â So in the real function a sub 2,

Â you also multiply by r, but let's just think about the 2 to the r part.

Â So imagine you applied that function over and over and over again.

Â What would you get?

Â Well now you get a tower of 2's where

Â the height of the tower is the number of times that you apply that function.

Â But if you apply it once, you get 2 to the r, If you apply it twice you're going to

Â get two raised to the 2 to the r, 3 times 2 to 2 to the 2 of the r and so on.

Â So all applications of a sub 2 gives you something that's even bigger than a tower

Â of r2's.

Â So, let's move on to a4, and

Â this is the point in which personally my brain begins to hurt.

Â But let's just push a little bit further so

Â that we appreciate the ridiculous growth of the Ackermann function.

Â And as with a3, let's punt for the moment on understanding the dependence for

Â a general r.

Â Let's just figure out what a sub 4 of 2 is.

Â So, k equals 4, r equals 2.

Â Well, so this by definition, you just take 2 and you apply a sub 3 twice.

Â We computed a sub three of 2 in the previous quiz.

Â We discovered that was 2,048.

Â So that's the result of the first application of a3.

Â And now we find ourselves applying a3 to 2048, so in effect r now is 2048.

Â And remember we concluded the last slide by saying well a sub 3,

Â whatever it is, it's at least as big as a tower of r2's.

Â And here r is 2048.

Â So, this of course now is the land of truly ridiculous numbers that we're

Â dealing with.

Â I encourage you to take a calculator or computer and

Â see how big of a tower of 2's you can compute, and

Â it's not going to be anywhere close to 2,000, I can promise you that.

Â And hey that is just a4 of 2, what about the function a4 for a general value of r?

Â Well, of course, in general,

Â a4 of r is going to be r applications of a3 starting at r.

Â Now, in the last slide,

Â we argued that a sub 3, this function is bounded below by a tower function.

Â So a sub 3 takes in some input, some integer,

Â and it spits out some tower with height equal to that integer, so

Â what happens when apply that tower function a3 over and over again?

Â Now you get what's called an iterated tower function.

Â So when you apply the function a sub 3 for the time the r is going to spit out

Â a number which is bounded below by a tower of height r, tower of 2's, all of them.

Â Now we apply a3 a second time, it's output is going to be a tower of 2's,

Â whose height is lowerbounded by a tower of 2's of height r, and so on.

Â Every time you apply a sub 3, you're just going to iterate this tower function.

Â You will sometimes see the iterated tower function referred to as a yowzer function.

Â You probably think I'm pulling your leg, but I'm not kidding.

Â You can look it up.

Â So that is the Ackerman function and

Â a bunch of examples to appreciate just how ridiculously quickly it is growing.

Â So now let's move on to define its inverse.

Â Now that Ackerman function has two parameters k and r excuse me.

Â So to define an inverse I'm going to fix one of those.

Â Specifically I'm going to fix r equal to 2.

Â So the inverse Ackermann function is going to be denoted by alpha.

Â For simplicity, I'll define it only for values of n that are at least 4 and

Â it's defined for giving value of n as the smallest k so

Â that if you apply a sub k to 2, to r equals 2, then the result is at least n.

Â So again it's simple enough to kind of write down a definition like this, but

Â you really have to plug in some concrete numbers to get a feel for what's going on.

Â So lets just write out, you know,

Â what are the values of n that will give you alpha of n equal 1.

Â That will give you alpha of n equal 2, alpha of n equal 3, and so on.

Â Okay so for starters, alpha of 4 is going to be equal to 1, right.

Â So if you applied a 0 to 2, that's the successor function so you only get 3, so

Â that's not at least as big as 4.

Â On the other hand, if you apply a sub 1 to 2,

Â that's the doubling function, so if you apply it to 2 you get 4.

Â So a sub 1 does give you a number which is at least as big as n when n is 4.

Â So next I claim that the values of n for

Â which alpha of n equals two are the values 5, 6, 7 and 8.

Â Why is that true?

Â Well, if you start from 2 and you apply the function a sub 1,

Â the doubling function you only get 4.

Â So, a sub 1 is not sufficient to achieve these values of n's.

Â On the other hand if you apply a sub 2 to 2,

Â remember that's the function which given an r outputs r times 2 to the r, so

Â when you apply it to 2, you get 2 times 2 to the 2, also known as 8.

Â So a sub 2 is sufficient to map 2 to a number at least as big as 8.

Â By the same reasoning, recall that we computed a sub 3 of 2 and

Â we computed that to be 2048.

Â So therefore for all of the bigger values of n starting at 9 and going all the way

Â up to 2048, their alpha value is going to be equal to 3 because that is

Â the smallest value of k such that a sub k of 2 is at least those values of n.

Â And then things start getting a little bit ridiculous.

Â So remember we also lower bounded a sub 4 of 2.

Â We said that's at least a tower of 2's of height 2048.

Â So for any n up to that value, up to a tower of 2's of height 2048,

Â alpha of those n's is going to be equal to 4.

Â This of course implies that the inverse Ackermann

Â function value of any imaginable number is at most 4.

Â And I don't know about you, but my brain is already hurting too much to think about

Â which of the values of n, such that alpha is equal to 5.

Â So as a point of contrast, let's do the same experiment for the log star function,

Â so that we get some appreciation about how as glacially growing a log star may be,

Â the inverse Ackermann function is growing still qualitatively slower.

Â So the log star function remember is the iterated logarithm, so

Â it's starting from n how many times you need to hit log in your calculator,

Â let's say base two before the result drops below 1.

Â So when is log star n going to be equal to 1?

Â Well that's when n is going to be equal to 2.

Â Then you hit log once and you drop from 2 to 1 and you're done.

Â If n equals 3 of 4 then you need more than one application of logarithm to drop below

Â 1, but you only need two applications, so log star of n is going to be 2 for

Â n equals 3 and 4.

Â Similarly for n anywhere between 5 and 16 log star n is going to be equal to 3.

Â Right if we start from 16 that's 2 to the 4, so if you fly a log once you get 4,

Â a second time you get 2, a third time you get 1.

Â By analogy, the values of n for which the log star of n equals 4 are going to be

Â starting at 17 and going up to 2 to the 16, also known as 65536.

Â So let's just go one more step for which n is log star of n equal to 5.

Â Well obviously we start at 65537, and we end at 2 raised to

Â the largest number of the previous block, so 2 raised to the 65536.

Â So, these numbers are impressive enough on the right hand side, but there is no

Â imaginable number of importance for which log star is going to be bigger than 5.

Â Looking at the left and the right hand sides though, we see that there really

Â is a qualitative difference between the rate of growth of these two functions.

Â With the inverse Ackermann function,

Â in fact, growing much, much slower than the log star function.

Â So perhaps the easiest way to see that is to look at the right hand side, and

Â on each of the lines in the right hand side, look at the largest value of n, so

Â that log star is a given number.

Â So for example, in the third line, the largest n such that log star of n equals

Â 3, is a tower of 2s of height three, 2 to the 2, to the 2, also known as 16.

Â Similarly on the fourth and

Â fifth lines the largest n to give values of log star equal to 4 and

Â 5 are a tower of 2s of height 4 and two and a tower of 2s of height 5.

Â On the other hand look at the fourth line on the left hand side.

Â So I really when we ask for the largest values of n that have inverse Ackermann

Â value 4, we're talking about towers of 2s, in fact of height 2048.

Â So that is the n's which give us alpha of n equal to 4.

Â We would have to write down 2048 lines on the right hand side before we had values

Â of n that were equally big.

Â This indicates how the log star function while growing glacially indeed,

Â as it never the less moving at light speed compared to the inverse Ackermann

Â function.

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