1 / 12

Amortized Analysis

Amortized Analysis. In amortized analysis, the time required to perform a sequence of operations is averaged over all the operations performed Aggregate Method : we determine an upper bound T(n) on the total sequence of n operations. The cost of each will then be T(n)/n .

artemas
Télécharger la présentation

Amortized Analysis

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Amortized Analysis • In amortized analysis, the time required to perform a sequence of operations is averaged over all the operations performed • Aggregate Method: we determine an upper bound T(n) on the total sequence of n operations. The cost of each will then be T(n)/n.

  2. Amortized Analysis • Accounting Method: we overcharge some operations early and use them to as prepaid charge later. • Potential Method: we maintain credit as potential energy associated with the structure as a whole.

  3. Aggregate Method • We show that for n operation, the sequence takes worst case T(n) • We use as an example, a stack with a new operation MULTIPOP (S,k) • If we consider PUSH and POP to be elementary operations, then Multipop takes O(n) in the worst case.

  4. Aggregate Method • If we analyze a sequence of n Push, Pop, and Multipop operations, then the stack size is at most n and Multipop takes O(n). Therefore, the sequence takes O(n2). • However, since an object can be only popped once (whether by pop or multipop) for every time it is pushed, the total number of Push operations is at most the total number of Push operations (which is at most n)

  5. Aggregate Method • Another example is incrementing a binary counter that counts up from 0. • Simple incrementation algorithm: Flip every 1-bit until you see a 0-bit. Flip the 0-bit and stop.

  6. Accounting Method • We assign different charges to different operations, with some operations charged more or less than they actually cost. • The amount we charge an operation is its amortized cost. • When an operation’s amortized cost exceeds its actual cost, the difference is assigned to specific objects in the data structure as credit.

  7. Accounting Method • Credit can be used later to pay the cost of operations whose actual cost is greater than its amortized cost. • The total credit stored must always be non-negative at all times.

  8. Accounting Method: Stack • Thing about this: when we push a plate onto a stack, we use $1 to pay actual cost of the push and we leave $1 on the plate. • At any point, every plate on the stack has a dollar on top of it. • When we execute a pop operation, we charge it nothing and pay its cost with the dollar that is on top of it.

  9. Potential Method • Instead of representing prepaid work as credit stored with specific objects, the potential method represents the prepaid work as potential energy than can be released to pay for future operations. • Potential is associated with the data structure as a whole rather than with specific objects.

  10. Potential Method • We start with an initial data structure D0on which n operations are performed. • Let ci be the cost the ith operations and Dibe the data structure that results after applying the ith operation to data structure Di-1 • A potential function  maps Dito a real number (Di) which is the potential associated with Di

  11. Dynamic Tables • In some applications, we don’t know in advance how many objects will be stored in a table. • We must re-allocate with a larger or smaller size as data is added or removed. • Define load factor

  12. Dynamic Tables: Expansion • When the table becomes full, i.e. load factor = 1, then the next operation triggers an expansion. • We allocate a new area twice the size of the old and copy all items from the old table to the new table. • If an operation does not trigger an expansion, its cost is 1. If it does trigger, its cost is num [T] + 1.

More Related