馃攳
Amortized Analysis | Data Structures - YouTube
Channel: unknown
[0]
Hey guys, in this video I'm going to do a question in Data Structures about amortized
[4]
analysis. This one says, determine the amortized cost of adding to an array-backed heap.
[9]
So we're just looking at the add() method.
[11]
And it says assume the following resize
[13]
strategy: that the initial capacity is equal to 1, and when the array is full, you double the capacity
[18]
when you resize it to add again. For these types of amortized cost questions,
[24]
you're going to know basically what the runtime is of each operation, and you can do it for several times.
[32]
So you can go from 1 to N.
[34]
And then you should be able to see--it's basically, the definition is that when you have a sequence of operations
[41]
and you know the pattern of their runtimes, you can do an amortized cost analysis for that
[47]
method. I'm going to go ahead and get started.
[51]
So we're going to do a little table that shows the runtime cost of each operation.
[56]
We're going to find the amortized cost of the total N operations
[60]
and then we're going to find the amortized cost per operation.
[65]
Like I said,
[66]
we're going to go from adding one, two, and then just enough times until we can find a pattern.
[74]
I'm going to go up to nine.
[76]
And we are given enough information to know what
[79]
to do with the capacity, the resize, and the cost.
[83]
The first time you're adding to the array, the array is empty, and it has a capacity of one.
[88]
You don't have to resize because all we're doing is adding for the first time and
[93]
we're just going to say that we know, or you should know
[97]
when you're doing this, what the cost of adding to an array heap is. So that's going to be--in general
[103]
it's O(log N).
[104]
When you're adding one, the
[107]
cost is going to be log(1), and so we're done with the first one. Now to add
[113]
a second time to the array, we have to resize. So we have to do our first resize.
[119]
The capacity is now going to be two because we know from the
[123]
strategies that we double the capacity
[124]
when the array is full. And so the cost is going to be the cost of adding--our N
[129]
is two here--so it's log(2) plus the resize cost is one. So we have log(2) + 1
[137]
for adding a second item. Our capacity is two. To add a third item to the heap,
[144]
we're going to have to resize again.
[146]
Now our capacity is four and the cost is going to be log(3)
[152]
plus the cost of adding two.
[154]
We don't have to resize to add four, so our capacity is still four.
[159]
We leave this blank, and it's going to just be log(4) for the cost. For five,
[165]
we're going to have to resize again because our capacity was four.
[168]
We resize and add four, so our
[172]
capacity is now eight. And the cost of adding five is going to be log(5)
[177]
plus the resize cost, which is four. And now we're going to be able to add up to eight without having to resize.
[185]
Our capacity can stay at 8. We don't have to
[190]
resize. This is going to be log(6). This is going to be log(7). Eight is still going to be just
[198]
log(8)
[200]
and then we're going to do one more final resize. To double, we're going to add eight.
[205]
Our capacity is going to be 16 and then the cost is going to be log(9) + 8.
[211]
Okay, so this is enough of the table filled out to notice a distinct pattern with the cost column. We see that if
[219]
you consider this to be N, the cost is log(N)
[223]
plus the cost of resizing. At the bottom, let's go ahead and figure out what the patterns are.
[231]
For N total operations,
[233]
you see that the cost is log(N), and we're doing this N times
[239]
plus the cost of resizing. The resizing--the pattern is
[245]
essentially 1 + 2 + 4 + 8 and it's that sequence of numbers. So 1 + 2
[252]
+ 4 + 8, which is essentially doubling each time. And the formula for that is going to be 2n - 1.
[259]
So when N is 1, you have 2 - 1 that's 1, and so on. The pattern
[266]
overall is N * log(N) + 2N - 1. When it's asking for the total or overall
[273]
amortized cost, you want to give that in a big O notation.
[276]
If you take the big O notation of this, it's going to be O(nlog(n)).
[282]
Because O(nlogn) is bigger than O(n), and you have an addition here. This would be basically
[288]
O(nlogn) + O(n). The final answer for the total amortized cost is
[294]
O(nlogn). And the cost per operation, you just divide that by N, and you would get
[301]
O(log N)
[303]
per call to the add method, and that's pretty much it. So we are done with this problem.
[312]
*chiptune music plays*
Most Recent Videos:
You can go back to the homepage right here: Homepage





