AmortizedAnalysis

Insert charge 3 credits 1 credit for insert save 2

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: to a table of twice the size. ・DELETE: if table is ¼-full, contract to a table of half the size. Memory usage. A dynamic table uses O(n) memory to store n items. Pf. Table is always at least ¼-full (provided it is not empty). ▪ 45 Dynamic table: insert and delete insert 1 2 3 4 5 6 7 8 9 10 11 12 2 3 4 5 6 7 8 9 10 11 12 delete 1 resize and delete 1 2 3 4 46 Dynamic table: insert and delete Accounting. ・INSERT: charge 3 credits (1 credit for insert; save 2 with new item). ・DELETE: charge 2 credits (1 credit to delete, save 1 in emptied slot). discard any existing credits Theorem. [via accounting method] Starting from an empty dynamic table, any intermixed sequence of n INSERT and DELETE operations takes O(n) time. Pf. The algorithm maintains the invariant that there are 2 credits with each item in the right half of table; 1 credit with each empty slot in the left half. ・When table doubles, each item in right half of table has 2 credits. ・When table halves, each empty slot in left half of table has 1 credit. ▪ 47 Dynamic table: insert and delete Theorem. [via potential method] Starting from an empty dynamic table, any intermixed sequence of n INSERT and DELETE operations takes O(n) time. Pf sketch. ・Let α(Di) = size(Di) / capacity(Di). (Di ) = 2 size(Di ) capacity (Di ) 1 size(Di ) 2 capacity (Di ) ・When α(D) = 1/2, Φ(D) = 0. ・When α(D) = 1, Φ(D) = size(Di). ・When α(D) = 1/4, Φ(D) = size(Di). 1/2 < 1/2 [zero potential after resizing] [can pay for expansion] [can pay for contraction] ... 48...
View Full Document

This document was uploaded on 02/05/2014.

Ask a homework question - tutors are online