transcript

122 Videos - Amortized Analysis

Thumbnail of the video “122 Videos - Amortized Analysis”
Download Markdown
Community upload. AI output has not been independently verified.

Hello everyone, welcome to amortized analysis in 5 minutes. My name is Alex Lynn, and today we'll be talking about amortized analysis in hopefully a more fun and engaging manner. But before we start, let's meet our protagonist, Mr. Alano. Alano is this typical CMU student who is tired from all of his 122 homework. However, he's determined to get that A in the class, so he decides to go to his best friend, coffee, for help. To get coffee, Alano goes to this nice little coffee shop called La Prima, located at Wean Hall. Oh, I almost forgot something: wear your mask, Alano, be stay safe. All right.

Upon arriving at La Prima, Alano realizes that La Prima is actually going through this new green movement, where instead of providing free plastic cups for the students, La Prima decided to sell these very good mugs at a price of $5 a piece. However, these very good mugs are actually not so good. They are actually very poor in quality and will break after exactly five uses, exactly five. So after drinking five cups of coffee, the student will have to pay for another mug. La Prima, however, thinks this is very good for business, so they decide, whatever, I'm just going to use this to promote their new policy. La Prima decides to give all the new students a free mug. So there's Alano's free mug. Alano is very happy with this deal. Free stuff, why not? So he buys his coffee and leaves.

However, this actually got Alano thinking: what is my actual cost for drinking coffee? Is it $1 for just buying the coffee, or is it $6 for getting both the coffee and the mug? Fortunately, Alano is a CS student, so he decides to use what he has learned at 122 to help. Come with me, Alano. All right. Alano draws out this schedule where he lists out the expected money that he's going to have to pay every day. On day one, Alano pays $1, that's just getting the coffee. On day two, three, four, and five, all the same. However, on day six he realizes that his mug would have been broken by now, and he needs to pay for a new mug, which cost him a total of $5, and an extra dollar for drinking coffee, so that's $6. On 7, 8, 9, 10, it's the same thing, and on day 11, another new mug, another $6.

So Alano first tries to apply the worst-case analysis, which he learned in 122. The worst-case analysis tells them to look at the worst case, $6, and simply say that drinking coffee cost me $6. Hm, this doesn't seem right. This is way too expensive unless you drink from Starbucks. He notices that most of the time he just spends $1, so he's going to be wasting a lot of money if he brings $6 to school every time. So he decides to adopt a better plan: amortized analysis.

So what is amortized analysis? What amortized analysis basically is trying to do is spread out the cost of this expensive day into the cheaper days. I'll demonstrate with an example. Alano devises a plan where he only brings $2 every day, and every day he spends $1 on the coffee and saves $1 in his back pocket. He does the same thing the next day: he spends $1 and saves $1. By the time he has gotten to day six, he would have already saved $5 in his back pocket. So on day six he only needs to bring in $2. He spends $1 for the coffee, spends the $5 in his back pocket for the mug, and saves another dollar to be used on the mug at day 11. This is amortized analysis, where the cost of this expensive mug buying is actually spread out. By using amortized analysis, Alano knows that drinking coffee actually costs him $2 every day, which is a much more accurate estimation and, of course, better for his wallet.

All right, so when is amortized analysis useful? Amortized analysis usually arises when an operation has both an expensive case and a cheap case. For drinking coffee, the expensive case is buying a new cup and drinking coffee, which is $6 in total. The cheap case is just filling a cup with coffee, which is $1. Amortized analysis tells us that the total average cost is going to be $2 every time I drink coffee.

For computer science, some algorithms, like dynamic array (also called a resizing array), have a cheap case where I fill in the next array block, which is O(1), and an expensive case when the array is full and I need to copy everything over to resize, which is O(n). By worst-case, I'd be looking at O(n), but this is way too pessimistic because that doesn't happen that often. By using amortized analysis, a more accurate representation is actually O(1). Dynamic array insertion is actually O(1) amortized.

Some last tips before we go. First, don't be scared by the wording. We're usually just looking for the average cost. Amortized analysis is not some fancy, scary monster. It's something that you should be used to from your elementary school math. Second, find the cheap and expensive operations before you start to analyze. That will give you a starting ground for figuring out what you're trying to analyze. Third, identify the cycle. What I mean by this is, like Alano, write down the number of cheap operations you need to do, the expensive operations you need to do, and find a pattern or a cycle that you can operate on. Last of all, remember that a cycle always starts poor. You want to choose a cycle to analyze where you assume that you start poor, just after finishing an expensive operation. By this time you should have $0 in your pocket, and that's where you start analyzing.

Thank you, that's all for amortized analysis. Thank you all for watching. I hope you have all learned something.