1 / 49

17.Amortized analysis

17.Amortized analysis. Hsu, Lih-Hsing. The time required to perform a sequence of data structure operations in average over all the operations performed. Average performance of each operation in the worst case.

acostaa
Download Presentation

17.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. 17.Amortized analysis Hsu, Lih-Hsing

  2. The time required to perform a sequence of data structure operations in average over all the operations performed. • Average performance of each operation in the worst case.

  3. For all n, a sequence of n operations takes worst time T(n) in total. The amortize cost of each operation is .

  4. Three common techniques • aggregate analysis • accounting method • potential method

  5. 17.1 The aggregate analysis • Stack operation - PUSH(S, x) - POP(S) - MULTIPOP(S, k) MULTIPOP(S, k) 1 while not STACK-EMPTY(S) and k  0 2 do POP(S) 3 k k – 1

  6. Action of MULTIPOP on a stack S top  23 17 6 39 10 47  initial top  10 47  MULTIPOP(S,4)  MULTIPOP(S,7)

  7. Analysis a sequence of n PUSH, POP, and MULTIPOP operation on an initially empty stack. • O(n2) • O(n) (better bound) • The amortize cost of an operation is .

  8. Incremental of a binary counter

  9. INCREMENT INCREMENT(A) 1 i 0 2 while i < length[A] and A[i] = 1 3 doA[i]  0 4 i i + 1 5 ifi < length[A] 6 thenA[i]  1

  10. Analysis: • O(n k) (k is the word length) • Amortize Analysis:

  11. 17.2 The accounting method • We assign different charges to different operations, with some operations charged more or less than the actually cost. The amount we charge an operation is called its amortized cost.

  12. When an operation’s amortized cost exceeds its actually cost, the difference is assign to specific object in the data structure as credit. Credit can be used later on to help pay for operations whose amortized cost is less than their actual cost.

  13. If we want analysis with amortized costs to show that in the worst cast the average cost per operation is small, the total amortized cost of a sequence of operations must be an upper bound on the total actual cost of the sequence. • Moreover, as in aggregate analysis, this relationship must hold for all sequences of operations.

  14. If we denote the actual cost of the ith operation by ci and the amortized cost of the ith operation by , we require for all sequence of n operations. • The total credit stored in the data structure is the difference between the total actual cost, or .

  15. Stack operation • Amortize cost: O(1)

  16. Incrementing a binary counter • Each time, there is exactly one 0 that is changed into 1. • The number of 1’s in the counter is never negative! • Amortized cost is at most 2 = O(1).

  17. 17.3 The potential method

  18. If then . • If then the potential increases.

  19. Stack operations • the number of objects in the stack of the ith operation.

  20. PUSH

  21. MULTIPOP

  22. POP • The amortized cost of each of these operations is O(1).

  23. Incrementing a binary counter • the number of 1’s in the counter after the ith operations = . • = the ith INCREMENT operation resets bits. • Amortized cost = O(1)

  24. Even if the counter does not start at zero:

  25. 17.4 Dynamic tables

  26. 17.4.1 Table expansion • TABLE-INSERT • TABLE-DELETE (discuss later) • load-factor • (load factor)

  27. TABLE_INSERT TABLE_INSERT(T, x) 1 ifsize[T] = 0 2 then allocate table[T] with 1 slot 3 size[T]  1 4 ifnum[T] = size[T] 5 then allocate new-table with 2  size[T] slots 6 insert all items in table[T] in new-table 7 free table[T] 8 table[T]  new-table 9 size[T] 2  size[T] 10 insert x into table[T] 11 num[T]  num[T] + 1

  28. Aggregate method: • amortized cost = 3

  29. Accounting method: • each item pays for 3 elementary insertions; 1. inserting itself in the current table, 2. moving itself when the table is expanded, and 3. moving another item that has already been moved once when the table is expanded.

  30. Potential method: • (not expansion)

  31. Example:

  32. (expansion)

  33. Example: • Amortized cost = 3

  34. 17.4.2 Table expansion and contraction • To implement a TABLE-DELETE operation, it is desirable to contract the table when the load factor of the table becomes too small, so that the waste space is not exorbitant.

  35. Goal: • The load factor of the dynamic table is bounded below by a constant. • The amortized cost of a table operation is bounded above by a constant. • Set load factor

  36. The first operations are inserted. The second operations, we perform I, D, D, I, I, D, D, … • Total cost of these n operations is . Hence the amortized cost is . • Set load factor (as TABLE_DELETE) (after the contraction, the load factor become )

  37. Initial

  38. TABLE-INSERT • if , same as before. • if if

  39. Example:

  40. If

  41. Example: Amortized cost of Table insert is O(1).

  42. TABLE-DELETE • If does not cause a contraction (i.e.,

  43. Example:

  44. causes a contraction

  45. Example:

  46. if (Exercise 18.4.3) • Amortized cost O(1).

More Related