Question: Amortized Complexity Example: Dynamic ( Extendable ) ArrayWe present a reallocation strategy for an array that will ensure total time complexity O ( n )
Amortized Complexity Example: Dynamic Extendable ArrayWe present a reallocation strategy for an array that will ensure total time complexity On for n successive insert operations, and thus amortized complexity O for one insert.Problem description:Data are sequentially coming to the input, and are inserted intoan array so that the ith element is stored at index iDesign an operation DAInsert for inserting an element in the case whereI Ithe total number n of input data items is unknown,and therefore a static array of sufficient size cannot be allocated beforehand. Extension StrategyConsider the following method of array extension:Let m be the capacity of an array P and let i be the index of the element we are currently inserting.If i m the capacity is exceededI allocate a new array of size mI copy the stored values of the original P into the first half of thenew array,I deallocate the old array, and call the new array PI store the new inserted element at index m and double thevalue of mAt the beginning, we allocate an array of size just to makethe analysis easier Extendable Array: Insert Algorithm DAInsertPx Let i be the index of the element we are currentlyinserting.If im store Pi:x i:i and stop. If im then Allocate P of size m and set Copy P to P Deallocate P and set P : P Store Pi:x and i:im :m ObservationThe time complexity of the algorithm DAInsert applied to an nelement array is n in the worst case. Extendable Array: Amortized AnalysisTheoremSuppose that, at the beginning, the array is empty. Then the time complexity of n successive calls of the operation DAInsert is n Thus, the amortized complexity of one call is Proof.To insert n elements, we perform constanttime insertions, and from time to time extensions of the array.Simple insertion of n elements needs n time.Extending the array by insertion of ith element needs i time.The array is extended only if i is equal to a power of Therefore, all the extensions need k time in total, where k is the greatest power of smaller than nThis geometric series adds up to kn which gives a time complexity of n in total. Do this task with other strategies: if the capacity m is exceeded strategy a: allocate a new array of size m strategy b: allocate a new array of size m Suppose that, at the beginning, the array is empty.Prove that the time complexity of n successive calls of the operation DAInsert with strategy a is On Thus, the amortized complexity of one call is O BONUS: Prove that the time complexity of n successive calls of the operation DAInsert with strategy b is greater than nand thus, the amortized complexity of one call cannot be
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
