Question: 2. Consider a data structure that maintain a binary counter of unspecified length and supports reset, two operations: i) increnent, which increments the counter's value

2. Consider a data structure that maintain a binary counter of unspecified length and supports reset, two operations: i) increnent, which increments the counter's value by 1 and that resets the counter's value to 0. A simple way to implement a binary counter is to allocate a very large array, say of length M, of bits when the data structure is initialized and set all these bits to 0. Then, the increment operation can be implemented as follows: function increent(B) while (B]1 do Notice that this implementation of increent does not check for an overflow it just assumes that M is going to be large enough that checking for overflow is unnnecessary. In your thinking about this problem, do not worry about the possibility of an overflow Example: Suppose that after initializing the binary counter data structure, we perform five increent operations. Thus the current value of the counter is 5, which is represented by Bol 1, B[1-0, B[2] 1, B?-0 for all i > 2. Then calling increment once more changes BO to 0 and B1 to 1, leaving all other bits unchanged. (a) Suppose that the binary ounter is initialized as described above. Now consider a sequence of n operations, some of which are increment operations and some of which are reset operations. What is the worst case running time of any one of these operations, as a function of n b) Argue that the amortized running time of these operations is O() Hint: Every increment is a sequence of assignments that turn a bunch of bits from 1 to 0 followed by one assignment that turns a bit from 0 to 1. Now notice that every assignment that turns a bit from 1 to 0 can be "charged" to a previous increnent operation that turned that bit from 0 to 1. 2. Consider a data structure that maintain a binary counter of unspecified length and supports reset, two operations: i) increnent, which increments the counter's value by 1 and that resets the counter's value to 0. A simple way to implement a binary counter is to allocate a very large array, say of length M, of bits when the data structure is initialized and set all these bits to 0. Then, the increment operation can be implemented as follows: function increent(B) while (B]1 do Notice that this implementation of increent does not check for an overflow it just assumes that M is going to be large enough that checking for overflow is unnnecessary. In your thinking about this problem, do not worry about the possibility of an overflow Example: Suppose that after initializing the binary counter data structure, we perform five increent operations. Thus the current value of the counter is 5, which is represented by Bol 1, B[1-0, B[2] 1, B?-0 for all i > 2. Then calling increment once more changes BO to 0 and B1 to 1, leaving all other bits unchanged. (a) Suppose that the binary ounter is initialized as described above. Now consider a sequence of n operations, some of which are increment operations and some of which are reset operations. What is the worst case running time of any one of these operations, as a function of n b) Argue that the amortized running time of these operations is O() Hint: Every increment is a sequence of assignments that turn a bunch of bits from 1 to 0 followed by one assignment that turns a bit from 0 to 1. Now notice that every assignment that turns a bit from 1 to 0 can be "charged" to a previous increnent operation that turned that bit from 0 to 1
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
