amortized-analysis

Amortized Time with Hash Table

六月ゝ 毕业季﹏ 提交于 2021-01-07 03:04:23
问题 I solved the first part of the question but stuck at the second part. I have an elevator and want to support the following: Init() Set the elevator to start from floor 0 and its travel direction to be up (This function is called only once and the direction can't be changed). O(1) AddStop(K) save into the DS that elevator should stop when it reaches floor k. O(log n) while n is the total number of stops for the elevator. NextStop() send the elevator to its next stop position, if it's in the

Amortized Time Calculation in AVL tree

旧巷老猫 提交于 2020-12-27 07:24:49
问题 My professor showed the following problem in class and mentioned that the answer is O(1) while mine was quit different, I hope to get some help knowing of what mistakes did I made. Question: Calculate the Amortized Time Complexity for F method in AVL tree, when we start from the minimal node and each time we call F over the last found member. Description of F: when we are at specific node F continues just like inorder traversal starting from the current one until the next one in inorder

What happens to amortized analysis in binary counter if flipping a bit at index k costs now 2^k instead of 1?

天大地大妈咪最大 提交于 2020-01-25 04:18:06
问题 Suppose that flipping bit # i costs 2 i ; so, flipping bit #0 costs 1, flipping bit #1 costs 2, flipping bit #2 costs 4, and so on. What is the amortized cost of a call to Increment() , if we call it n times? I think the cost for n Increments should be n ·2 0 /2 0 + n ·2 1 /2 1 + n ·2 2 /2 2 + … + n ·2 n −1 /2 n −1 = n ( n −1) = O ( n 2 ). So each Increment should be O ( n ), right? But the assignment requires me to prove that it's O (log n ). What have I done wrong here? 回答1: Let we have p

Amortized time of dynamic array

自作多情 提交于 2020-01-11 05:24:04
问题 As a simple example, in a specific implementation of the dynamic array, we double the size of the array each time it fills up. Because of this, array reallocation may be required, and in the worst case an insertion may require O(n). However, a sequence of n insertions can always be done in O(n) time, because the rest of the insertions are done in constant time, so n insertions can be completed in O(n) time. The amortized time per operation is therefore O(n) / n = O(1). --from Wiki But in

Binary Counter Amortized Analysis

最后都变了- 提交于 2020-01-06 07:45:19
问题 I guess you already know that if all the entries in the Array starts at 0 and at each step we increment the counter by 1 (by flipping 0's and 1's) then the amortized cost for k increments is O(k). But, what happens if the Array starts with n ? I though that maybe the complexity for k increments is now O(log(n) + k), because of the fact that in the beginning the maximum number of 1's is log(n). Any suggestions ? Thanks in advance 回答1: You are right. There is more than one way to prove this,

Binary Counter Amortized Analysis

送分小仙女□ 提交于 2020-01-06 07:45:12
问题 I guess you already know that if all the entries in the Array starts at 0 and at each step we increment the counter by 1 (by flipping 0's and 1's) then the amortized cost for k increments is O(k). But, what happens if the Array starts with n ? I though that maybe the complexity for k increments is now O(log(n) + k), because of the fact that in the beginning the maximum number of 1's is log(n). Any suggestions ? Thanks in advance 回答1: You are right. There is more than one way to prove this,

amortized cost of modified increment function

亡梦爱人 提交于 2020-01-05 07:13:25
问题 So the scenario goes, the increment algorithm, for n bit binary string A[0....n-1], where A[0] is the least significant bit and A[n-1] is the most significant bit, is: Increment(A,n): i←0 while i<k and A[i]=1 do A[i] ← 0 i ← i+1 if i < k then A[i] ← 1 But the cost to flip a bit at index k is 2^k I'm lost on trying to prove that the amortized cost of this modified binary increment algorithm is O(logn). No matter how I try to approach, it still seems like the amortized cost would be big O(1),

Design a stack that can also dequeue in O(1) amortized time?

[亡魂溺海] 提交于 2020-01-02 05:22:31
问题 I have an abstract data type that can be viewed as a list stored left to right, with the following possible operations: Push: add a new item to the left end of the list Pop: remove the item on the left end of the list Pull: remove the item on the right end of the list Implement this using three stacks and constant additional memory, so that the amortized time for any push, pop, or pull operation is constant. The stacks have basic operations, isEmpty, Push, and Pop. Amortized time means "If I

Amortized analysis of std::vector insertion

余生长醉 提交于 2019-12-29 03:57:05
问题 How do we do the analysis of insertion at the back (push_back) in a std::vector? It's amortized time is O(1) per insertion. In particular in a video in channel9 by Stephan T Lavavej and in this ( 17:42 onwards ) he says that for optimal performance Microsoft's implementation of this method increases capacity of the vector by around 1.5. How is this constant determined? 回答1: Assuming you mean push_back and not insertion, I believe that the important part is the multiply by some constant (as

Amortized Analysis of Algorithms

我的梦境 提交于 2019-12-28 12:44:51
问题 I am currently reading amortized analysis. I am not able to fully understand how it is different from normal analysis we perform to calculate average or worst case behaviour of algorithms. Can someone explain it with an example of sorting or something ? 回答1: Amortized analysis gives the average performance (over time) of each operation in the worst case. In a sequence of operations the worst case does not occur often in each operation - some operations may be cheap, some may be expensive