=========================================================================== CSC 263H Tutorial Outline for Week 9 Winter 2004 =========================================================================== ------------------------------------------- Amortized Analysis Example - Binary Counter ------------------------------------------- A Binary Counter is a sequence of K bits (K is fixed) on which a single operation can be performed: INCREMENT, which adds 1 to the integer represented in binary by the counter. The cost of a single INCREMENT operation is simply equal to the number of bits that need to be changed by the INCREMENT. For example, if K=5, Initial counter: 00000 (value = 0) after INCREMENT: 00001 (value = 1) cost = 1 after INCREMENT: 00010 (value = 2) cost = 2 after INCREMENT: 00011 (value = 3) cost = 1 after INCREMENT: 00100 (value = 4) cost = 3 after INCREMENT: 00101 (value = 5) cost = 1 after INCREMENT: 00110 (value = 6) cost = 2 after INCREMENT: 00111 (value = 7) cost = 1 after INCREMENT: 01000 (value = 8) cost = 4 after INCREMENT: 01001 (value = 9) cost = 1 ... after INCREMENT: 11101 (value = 29) cost = 1 after INCREMENT: 11110 (value = 30) cost = 2 after INCREMENT: 11111 (value = 31) cost = 1 after INCREMENT: 00000 (value = 0) cost = 5 ... Amortized analysis using the Accounting Method: Recall that in the accounting method, each operation has a "cost" (its actual running time) and a "charge" (an upper bound on its amortized running time). Moreover, individual elements in the data structure may be assigned "credits" in the following way: when an operation's charge is greater than or equal to its cost, the charge is used to "pay" for the operation's cost and whetever is left over will be assigned as "credit" to specific elements in the data structure. When an operation's charge is less than its cost, some of the credit assigned to particular elements will be used to pay for the cost of the operation. We want to define charges and a scheme for assigning credits to ensure that at any point during a sequence of operations, the total amount of credit available in the data structure is non-negative. This allows us to conclude that the total cost of the sequence of operations is no greater than the total charge, to get an upper bound on the worst-case sequence complexity. We will analyze the binary counter problem using the accounting method during tutorial. If you have finished A3, prepare for this tutorial by attempting to do this problem on your own. Time will be given during the tutorial to work on the problem together.