Presentation is loading. Please wait.

Presentation is loading. Please wait.

17.Amortized analysis Hsu, Lih-Hsing. Computer Theory Lab. Chapter 17P.2 The time required to perform a sequence of data structure operations in average.

Similar presentations


Presentation on theme: "17.Amortized analysis Hsu, Lih-Hsing. Computer Theory Lab. Chapter 17P.2 The time required to perform a sequence of data structure operations in average."— Presentation transcript:

1 17.Amortized analysis Hsu, Lih-Hsing

2 Computer Theory Lab. Chapter 17P.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 Computer Theory Lab. Chapter 17P.3 For all n, a sequence of n operations takes worst time T(n) in total. The amortize cost of each operation is.

4 Computer Theory Lab. Chapter 17P.4 Three common techniques aggregate analysis accounting method potential method

5 Computer Theory Lab. Chapter 17P.5 17.1 The aggregate analysis Stack operation - PUSH(S, x) - POP(S) - M ULTIPOP (S, k) M ULTIPOP (S, k) 1 while not S TACK- E MPTY (S) and k  0 2 do POP(S) 3 k  k – 1

6 Computer Theory Lab. Chapter 17P.6 Action of M ULTIPOP on a stack S top  23 17 6 39 10 47  initial top  10 47  M ULTIPOP (S,4)  M ULTIPOP (S,7)

7 Computer Theory Lab. Chapter 17P.7 Analysis a sequence of n PUSH, POP, and MULTIPOP operation on an initially empty stack. O(n 2 ) O(n) (better bound) The amortize cost of an operation is.

8 Computer Theory Lab. Chapter 17P.8 Incremental of a binary counter

9 Computer Theory Lab. Chapter 17P.9 I NCREMENT I NCREMENT (A) 1i  0 2while i < length[A] and A[i] = 1 3do A[i]  0 4i  i + 1 5if i < length[A] 6then A[i]  1

10 Computer Theory Lab. Chapter 17P.10 Analysis: O(n k) (k is the word length) Amortize Analysis:

11 Computer Theory Lab. Chapter 17P.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 Computer Theory Lab. Chapter 17P.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 Computer Theory Lab. Chapter 17P.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 Computer Theory Lab. Chapter 17P.14 If we denote the actual cost of the i th operation by c i and the amortized cost of the i th 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 Computer Theory Lab. Chapter 17P.15 Stack operation Amortize cost: O(1) PUSH1 2 POP1 0 M ULTIPOP min{k,s}M ULTIPOP 0

16 Computer Theory Lab. Chapter 17P.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). 0101 1 0101 2 1010 1 1010 0

17 Computer Theory Lab. Chapter 17P.17 17.3 The potential method

18 Computer Theory Lab. Chapter 17P.18

19 Computer Theory Lab. Chapter 17P.19 If then. If then the potential increases.

20 Computer Theory Lab. Chapter 17P.20 Stack operations the number of objects in the stack of the i th operation.

21 Computer Theory Lab. Chapter 17P.21 PUSH

22 Computer Theory Lab. Chapter 17P.22 MULTIPOP

23 Computer Theory Lab. Chapter 17P.23 POP The amortized cost of each of these operations is O(1).

24 Computer Theory Lab. Chapter 17P.24 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)

25 Computer Theory Lab. Chapter 17P.25 Even if the counter does not start at zero:

26 17.4 Dynamic tables

27 Computer Theory Lab. Chapter 17P.27 17.4.1 Table expansion TABLE-INSERT TABLE-DELETE (discuss later) load-factor (load factor)

28 Computer Theory Lab. Chapter 17P.28 TABLE_INSERT T ABLE _I NSERT (T, x) 1if size[T] = 0 2 then allocate table[T] with 1 slot 3 size[T]  1 4if num[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

29 Computer Theory Lab. Chapter 17P.29 Aggregate method: amortized cost = 3

30 Computer Theory Lab. Chapter 17P.30 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.

31 Computer Theory Lab. Chapter 17P.31 Potential method: (not expansion)

32 Computer Theory Lab. Chapter 17P.32 Example:

33 Computer Theory Lab. Chapter 17P.33 (expansion)

34 Computer Theory Lab. Chapter 17P.34 Example: Amortized cost = 3

35 Computer Theory Lab. Chapter 17P.35 17.4.2 Table expansion and contraction To implement a T ABLE -D ELETE 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.

36 Computer Theory Lab. Chapter 17P.36 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

37 Computer Theory Lab. Chapter 17P.37 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 T ABLE _D ELETE ) (after the contraction, the load factor become )

38 Computer Theory Lab. Chapter 17P.38

39 Computer Theory Lab. Chapter 17P.39 Initial

40 Computer Theory Lab. Chapter 17P.40 TABLE-INSERT if, same as before. if

41 Computer Theory Lab. Chapter 17P.41 Example:

42 Computer Theory Lab. Chapter 17P.42 If

43 Computer Theory Lab. Chapter 17P.43 Example: Amortized cost of Table insert is O(1).

44 Computer Theory Lab. Chapter 17P.44 T ABLE -D ELETE If does not cause a contraction (i.e.,

45 Computer Theory Lab. Chapter 17P.45 Example:

46 Computer Theory Lab. Chapter 17P.46 causes a contraction

47 Computer Theory Lab. Chapter 17P.47

48 Computer Theory Lab. Chapter 17P.48 Example:

49 Computer Theory Lab. Chapter 17P.49 if (Exercise 18.4.3) Amortized cost O(1).


Download ppt "17.Amortized analysis Hsu, Lih-Hsing. Computer Theory Lab. Chapter 17P.2 The time required to perform a sequence of data structure operations in average."

Similar presentations


Ads by Google