amortized analysis
play

Amortized Analysis - PDF document

Amortized Analysis


  1. Amortized Analysis : � � � 禍兮福之所倚 � 福兮禍之所伏 � � � � 《老子 《 老子》 》第五十八章 第五十八章: 禍兮福之所倚, ,福兮禍之所伏 《 《 老子 老子 》 》 第五十八章 第五十八章 : : 禍兮福之所倚 禍兮福之所倚 , , 福兮禍之所伏 福兮禍之所伏 : � � 福兮禍所依 � � 禍兮福所伏 � � � � 《 《詩經 詩經》 》 : 福兮禍所依, ,禍兮福所伏 《 《 詩經 詩經 》 》 : : 福兮禍所依 福兮禍所依 , , 禍兮福所伏 禍兮福所伏 Amortized analysis is a concept used to evaluate the cost or performance of a sequence of operations on a particular data structure more accurately, instead of considering the worst-case scenario. OP 1 , OP 2 , …, OP m : m operations. The worst-case time complexity of these m operations is not necessarily equal to the sum of their worst-case time complexities. 1

  2. • Potential Functions : an Example • • • Assume that each OP i (1 ≤ ≤ i ≤ ≤ m ) consists of zero ≤ ≤ ≤ ≤ or more pop ’s followed by one push , with respect to a stack. t i : the number of pop ’s and push performed by OP i . t ave : the average number of pop ’s and push performed by each OP i . For example, m = 8, OP 1 , OP 2 : 1 push ; OP 3 : 2 pop ’s and 1 push ; OP 4 , OP 5 OP 6 : 1 push ; OP 7 : 2 pop ’s and 1 push ; OP 8 : 1 pop and 1 push . � t 1 = t 2 = t 4 = t 5 = t 6 = 1, t 3 = t 7 = 3, t 8 = 2, and t ave = 13/8 = 1.625. 2

  3. Clearly, t ave < 2, because there are m push ’s and at most m − − 1 pop ’s performed. − − The upper bound of 2 can be derived as well by a potential function , as shown below. φ φ φ φ i : the number of elements in the stack after OP i . ( φ φ i denotes the potential for OP i +1 to φ φ perform pop ’s, i.e., the maximal number of pop ’s performed by OP i +1 is bounded by φ φ φ i .) φ 3

  4. � φ � � � φ φ i = φ φ φ φ φ i − − 1 + 1 − − − ( t i − − − 1) − − − − = φ φ φ φ i − − 1 + 2 − − t i − − − − � t i = 2 − � � � − ( φ − − φ φ φ i − − − φ − φ φ φ i − − 1 ) − − m m � � � � � � t = 2 m − − − − φ φ φ φ φ φ − φ φ ( − ) i i 1 − − − i i = i = 1 1 = 2 m − − − ( φ − φ φ φ m − − − − φ φ φ 0 ) φ = 2 m − − − φ − φ m φ φ ≤ ≤ 2 m − ≤ ≤ − − 1. − � t ave � � � ≤ ≤ ≤ (2 m − ≤ − − 1)/ m < 2. − 4

  5. • Amortized Analysis of Heaps • • • Suppose that H is a heap of n nodes. Let OP 1 , OP 2 , …, OP m be m operations on H . Assume that each OP i (1 ≤ ≤ ≤ ≤ i ≤ ≤ ≤ m ) is one of the ≤ following two operations: (1) insert an element x into H ; (2) delete the minimum from H . We show below that the average time consumed by each OP i is O (log 2 n ). Each OP i can be implemented by a melding operation on two heaps H i ,1 and H i ,2 . 5

  6. Melding H i ,1 and H i ,2 : 5 1 19 12 50 10 40 30 14 13 20 16 25 H i ,1 H i ,2 Step 1. Merge the right paths of H i ,1 and H i ,2 . (e.g., merge (1, 10, 20) and (5, 12, 14) into (1, 5, 10, 12, 14, 20)) Step 2. Attach all left parts of H i ,1 and H i ,2 to the merged path. 1 50 5 19 10 40 13 12 30 14 16 20 25 6

  7. Step 3. Swap the left subtree and the right subtree of each node (except the lowest one) in the merged path. (e.g., swap the left subtrees and the right subtrees of nodes 1, 5, 10, 12 and 14) 1 5 50 10 19 12 13 40 16 14 30 20 25 7

  8. The execution time of a melding operation is proportional to the total length of the two right paths (e.g., (1, 10, 20) and (5, 12, 14)) of H i ,1 and H i ,2 . The purpose of Step 3 is to reduce the length of the right path of the resulting heap (hence reduce the execution time of the next melding operation). Insertion of x into H can be realized by letting H i ,1 = x and H i ,2 = H . Deletion of the minimum from H can be realized by letting H i ,1 ( H i ,2 ) be the left (right) subtree of the root node of H . 8

  9. Heavy nodes and light nodes of H : x : a node of H . w ( x ) : the number of nodes in the subtree of H that is rooted at x . p ( x ) : the parent node of x in H . A non-root node x of H is heavy if w ( x ) > w ( p ( x ))/2, and light if w ( x ) ≤ ≤ ≤ w ( p ( x ))/2. ≤ A heavy node is further referred to as a right ( left ) heavy node , if it is the right (left) child node of its parent node. 9

  10. For example, 1 50 5 19 10 40 13 12 30 14 16 20 25 x w ( x ) 1 13 5 11 right heavy 10 8 right heavy 12 5 right heavy 13 2 light 14 3 right heavy 16 1 light 19 2 light 20 2 right heavy 25 1 light 30 1 light 40 1 light 50 1 light 10

  11. Let H i denote the heap obtained after OP i , where 1 ≤ ≤ ≤ ≤ i ≤ ≤ ≤ ≤ m . OP 1 OP 2 OP 3 OP m H       → → → H 1  →    →   → → → H 2       → → → → • •       → → → → H m • • • • • • • • • • Assume that H i has n i nodes. Consider H = H 0 and n = n 0 . Then, n i = n i − − 1 ± ± ± 1 for 1 ≤ ± ≤ ≤ ≤ i ≤ ≤ m . ≤ ≤ − − Suppose that H i − − 1,1 and H i − − 1,2 are the two heaps − − − − operated by OP i . H i - - 1,1 - - OP i H i H i - - 1,2 - - 11

  12. t i : the total length of the two right paths of H i − − 1,1 − − and H i − − 1,2 , which is proportional to the time − − spent for OP i . t ave : the average time consumed by each OP i , m � i.e., t ave = ( t . m ) / i i = 1 φ i : the number of right heavy nodes in H i φ φ φ (e.g., φ φ i = 0 for the heap of page 7). φ φ φ i is used as the potential function to evaluate t ave . φ φ φ Let A i = t i + ( φ φ φ φ i − − φ − − φ i − φ φ − 1 ). − − m m � � � � � � A = t + ( φ φ 0 ). φ m − φ φ − φ − − φ φ i i i = i = 1 1 Assume that m is a constant and φ φ φ m − φ − − − φ φ φ 0 ≥ φ ≥ ≥ 0. ≥ m m � � � � � � ( m × × t ave =) t ≤ ≤ A . × × ≤ ≤ i i i = i = 1 1 m � A is evaluated below. i i = 1 12

  13. Fact 1. Each node has at most one child heavy node. k i ,1 : the number of light nodes in the left path of H i . k i ,2 : the number of right heavy nodes attached to the left path of H i . k i ,2 k i ,1 heavy nodes light nodes . . . � � � � k i ,2 ≤ ≤ ≤ k i ,1 + 1 ≤ 13

  14. Fact 2. There are at most � � log 2 n i � � light nodes in the � � � � left (right) path of H i . If y 2 , y 3 , y 4 , … are light nodes, then w ( y 2 ) ≤ ≤ n i /2, w ( y 3 ) ≤ ≤ ≤ ≤ ≤ ≤ w ( y 2 )/2 ≤ ≤ n i /4, ≤ ≤ w ( y 4 ) ≤ ≤ ≤ ≤ w ( y 3 )/2 ≤ ≤ ≤ n i /8, … ≤ � � There are at most � � � � � log 2 n i � � � � � + 1 right heavy nodes attached to the left path of H i . 14

  15. Let n i − − 1,1 ( n i − − 1,2 ) : the number of nodes in H i − − 1,1 ( H i − − 1,2 ); − − − − − − − − h i − − 1,1 ( h i − − 1,2 ) : the number of heavy nodes in the − − − − right path of H i − − 1,1 ( H i − − 1,2 ); − − − − r i − − 1,1 ( r i − − 1,2 ) : the number of light nodes and heavy − − − − nodes in the right path of H i − − 1,1 ( H i − − 1,2 ). − − − − � t i = (1 + r i − � � � − 1,1 ) + (1 + r i − − 1,2 ) − − − − � � log 2 n i − − 1,1 � � � � � � r i − − 1,1 ≤ ≤ ≤ ≤ h i − − 1,1 + − − − − − − � � log 2 n i − − 1,2 � � � � � � r i − − 1,2 ≤ ≤ h i − ≤ ≤ − 1,2 + − − − − − − � t i ≤ � − 1,2 + � � log 2 n i − − 1,1 � � + � � log 2 n i − − 1,2 � � � � � � � � � � � � ≤ ≤ 2 + h i − ≤ − 1,1 + h i − − − − − − − − − � t i < 2 + h i − � × � � log 2 n i � � � � � � � � − 1,1 + h i − − 1,2 + 2 × × × − − − − 15

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend