Data structures
Static problems. Given an input, produce an output. DATA STRUCTURES Ex. Sorting, FFT, edit distance, shortest paths, MST, max-flow, ...
‣ amortized analysis Dynamic problems. Given a sequence of operations (given one at a time), ‣ binomial heaps produce a sequence of outputs. Ex. Stack, queue, priority queue, symbol table, union-find, …. ‣ Fibonacci heaps ‣ union-find Algorithm. Step-by-step procedure to solve a problem. Data structure. Way to store and organize data. Ex. Array, linked list, binary heap, binary search tree, hash table, …
1 2 3 4 5 6 7 8 7
Lecture slides by Kevin Wayne 33 22 55 23 16 63 86 9 http://www.cs.princeton.edu/~wayne/kleinberg-tardos 33 10
44 86 33 99 1 ● 4 ● 1 ● 3 ●
46 83 90 93 47 60 Last updated on Apr 8, 2013 6:13 AM 2
Appetizer Appetizer
Goal. Design a data structure to support all operations in O(1) time. Data structure. Three arrays A[1.. n], B[1.. n], and C[1.. n], and an integer k. ・INIT(n): create and return an initialized array (all zero) of length n. ・A[i] stores the current value for READ (if initialized). ・READ(A, i): return ith element of array. ・k = number of initialized entries. ・WRITE(A, i, value): set ith element of array to value. ・C[j] = index of jth initialized entry for j = 1, …, k. ・If C[j] = i, then B[i] = j for j = 1, …, k. true in C or C++, but not Java Assumptions. ・Can MALLOC an uninitialized array of length n in O(1) time. Theorem. A[i] is initialized iff both 1 ≤ B[i] ≤ k and C[B[i]] = i. ・Given an array, can read or write ith element in O(1) time. Pf. Ahead.
1 2 3 4 5 6 7 8
Remark. An array does INIT in O(n) time and READ and WRITE in O(1) time. A[ ] ? 22 55 99 ? 33 ? ?
B[ ] ? 3 4 1 ? 2 ? ?
C[ ] 4 6 2 3 ? ? ? ?
k = 4
A[4]=99, A[6]=33, A[2]=22, and A[3]=55 initialized in that order 3 4 Appetizer Appetizer
Theorem. A[i] is initialized iff both 1 ≤ B[i] ≤ k and C[B[i]] = i. Pf. ⇒ Suppose is the th entry to be initialized. INIT (A, n) READ (A, i) WRITE (A, i, value) ・ A[i] j
______・Then C[j] = i and B[i] = j. k ← 0. IF (INITIALIZED (A[i])) IF (INITIALIZED (A[i])) ・Thus, C[B[i]] = i. A ← MALLOC(n). RETURN A[i]. A[i] ← value. B ← MALLOC(n). ELSE ELSE C ← MALLOC(n). RETURN 0. k ← k + 1.
______
s ← MALLOC(n). A[i] ← value. 1 2 3 4 5 6 7 8
______B[i] ← k. A[ ] ? 22 55 99 ? 33 ? ? C[k] ← i. INITIALIZED (A, i) ______B[ ] ? 3 4 1 ? 2 ? ? IF (1 ≤ B[i] ≤ k) and (C[B[i]] = i) RETURN true. C[ ] 4 6 2 3 ? ? ? ? ELSE k = 4 RETURN false. ______A[4]=99, A[6]=33, A[2]=22, and A[3]=55 initialized in that order 5 6
Appetizer
Theorem. A[i] is initialized iff both 1 ≤ B[i] ≤ k and C[B[i]] = i. Pf. ⇐ AMORTIZED ANALYSIS ・Suppose A[i] is uninitialized. ・If B[i] < 1 or B[i] > k, then A[i] clearly uninitialized. ‣ binary counter ・If 1 ≤ B[i] ≤ k by coincidence, then we still can't have C[B[i]] = i ‣ multipop stack because none of the entries C[1.. k] can equal i. ▪ ‣ dynamic table
1 2 3 4 5 6 7 8
A[ ] ? 22 55 99 ? 33 ? ?
B[ ] ? 3 4 1 ? 2 ? ? Lecture slides by Kevin Wayne
http://www.cs.princeton.edu/~wayne/kleinberg-tardos
C[ ] 4 6 2 3 ? ? ? ?
k = 4
A[4]=99, A[6]=33, A[2]=22, and A[3]=55 initialized in that order 7 Last updated on Apr 8, 2013 6:13 AM Amortized analysis Amortized analysis: applications
Worst-case analysis. Determine worst-case running time of a data structure ・Splay trees. operation as function of the input size. ・Dynamic table. can be too pessimistic if the only way to encounter an expensive operation is if there ・Fibonacci heaps. were lots of previous cheap operations ・Garbage collection. ・Move-to-front list updating. ・Push-relabel algorithm for max flow. Amortized analysis. Determine worst-case running time of a sequence of ・Path compression for disjoint-set union. data structure operations as a function of the input size. ・Structural modifications to red-black trees. ・Security, databases, distributed computing, ... Ex. Starting from an empty stack implemented with a dynamic table, any sequence of n push and pop operations takes O(n) time in the worst case. SIAM J. ALG. DISC. METH. 1985 Society for Industrial and Applied Mathematics Vol. 6, No. 2, April 1985 016
AMORTIZED COMPUTATIONAL COMPLEXITY* ROBERT ENDRE TARJANt
Abstract. A powerful technique in the complexity analysis of data structures is amortization, or averaging over time. Amortized running time is a realistic but robust complexity measure for which we can obtain surprisingly tight upper and lower bounds on a variety of algorithms. By following the principle of designing algorithms whose amortized complexity is low, we obtain "self-adjusting" data structures that are simple, flexible and efficient. This paper surveys recent work by several researchers on amortized complexity.
ASM(MOS) subject classifications. 68C25, 68E05
9 1. Introduction. Webster’s [34] defines "amortize" as "to put money aside at 10 intervals, as in a sinking fund, for gradual payment of (a debt, etc.)." We shall adapt this term to computational complexity, meaning by it "to average over time" or, more precisely, "to average the running times of operations in a sequence over the sequence." The following observation motivates our study of amortization: In many uses of data structures, a sequence of operations, rather than just a single operation, is performed, and we are interested in the total time of the sequence, rather than in the times of the individual operations. A worst-case analysis, in which we sum the worst-case times Binary counter of the individual operations, may be unduly pessimistic, because it ignores correlated effects of the operations on the data structure. On the other hand, an average-case analysis may be inaccurate, since the probabilistic assumptions needed to carry out the analysis may be false. In such a situation, an amortized analysis, in which we Goal. Increment aaverage k-bitthe runningbinarytime percounteroperation over (moda (worst-case) 2k). sequence of operations, can yield an answer that is both realistic and robust. MORTIZED NALYSIS Representation. To thmake leastthe idea significantof amortization and bitthe motivation of counter.behind it more concrete, A A aletj =us jconsider17.1a very Aggregatesimple example. analysisConsider the manipulation of a stack by a 455 sequence of operations composed of two kinds of unit-time primitives: push, which adds a new item to the top of the stack, and pop, which removes and returns the top item on the Counterstack. We wish to analyze the running timeTotalof a sequence of operations, each composed of zero or[7] more[6] [5]pops[4] [3]followed[2] [1] [0]by a push. Assume we start with an ‣ binary counter value A A A A A A A A cost empty stack and carry out m such operations. A single operation in the sequence can take up to m time0 units,00000000as happens if each of the first m-0 1 operations performs no ‣ multipop stack pops and the last1operation00000001performs m 1 pops. However,1 altogether the m operations can perform at most2 2m00000010pushes and pops, since there are3 only m pushes altogether and each pop must correspond to an earlier push. This example3 may seem00000011too simple to be useful, 4but such stack manipulation ‣ dynamic table indeed occurs in4applications00000100as diverse as planarity-testing7 [14] and related problems [24] and linear-time5 string00000101matching [18]. In this paper 8we shall survey a number of settings in which6amortization00000110is useful. Not only does amortized10 running time provide a more exact way7 to measure00000111the running time of known11 algorithms, but it suggests that there may be8 new algorithms00001000efficient in an amortized15 rather than a worst-case sense. As we shall see, such algorithms do exist, and they are simpler, more efficient, and more flexible9 than their00001001worst-case cousins. 16 10 00001010 18 11 00001011 19 * Received by the editors December 29, 1983. This work was presented at the SIAM Second Conference on the Applications12of Discrete00001100Mathematics held at Massachusetts22Institute of Technology, Cambridge, Massachusetts, June1327-29, 1983.00001101 23 t Bell Laboratories,14 Murray00001110Hill, New Jersey 07974. 25 15 00001111306 26 CHAPTER 17 16 00010000 31
Figure 17.2 An 8-bit binary counter as its value goes from 0 to 16 by a sequence of 16 INCREMENT Cost model. Number ofoperations. bits flipped. Bits that flip to achieve the next value are shaded. The running cost for flipping bits is shown at the right. Notice that the total cost is always less than twice the total number of INCREMENT operations.
operations on an initially zero counter causes AŒ1 to flip n=2 times. Similarly,12 b c bit AŒ2 flips only every fourth time, or n=4 times in a sequence of n INCREMENT b c i operations. In general, for i 0; 1; : : : ; k 1,bitAŒi flips n=2 times in a D ! b c sequence of n INCREMENT operations on an initially zero counter. For i k, " bit AŒi does not exist, and so it cannot flip. The total number of flips in the sequence is thus
k 1 ! n 1 1 Goal. Increment a k-bit binary counter (mod 2k). Aggregate method. Sum up sequence of operations, weighted by their cost. th Representation. aj = j least17.1 Aggregate significant analysis bit of counter. 455 17.1 Aggregate analysis 455 Counter Total Counter Total [7] [6] [5] [4] [3] [2] [1] [0] [7] [6] [5] [4] [3] [2] [1] [0] value A A A A A A A A cost value A A A A A A A A cost 0 00000000 0 0 00000000 0 1 00000001 1 1 00000001 1 2 00000010 3 2 00000010 3 3 00000011 4 3 00000011 4 4 00000100 7 4 00000100 7 5 00000101 8 5 00000101 8 6 00000110 10 6 00000110 10 7 00000111 11 7 00000111 11 8 00001000 15 8 00001000 15 9 00001001 16 9 00001001 16 10 00001010 18 10 00001010 18 11 00001011 19 11 00001011 19 12 00001100 22 12 00001100 22 13 00001101 23 13 00001101 23 14 00001110 25 14 00001110 25 15 00001111 26 15 00001111 26 16 00010000 31 16 00010000 31 Theorem. Starting from the zero counter, a sequence of n INCREMENT Figure 17.2 An 8-bit binary counter as its value goes from 0 to 16 by a sequence of 16 INCREMENT Figure 17.2 An 8-bit binary counter as its value goes from 0 to 16 by a sequence of 16 INCREMENT operations flips O(n k) bits.operations. Bits that flip to achieve the next value are shaded. The running cost for flipping bits is operations. Bits that flip to achieve the next value are shaded. The running cost for flipping bits is shown at the right. Notice that the total cost is always less than twice the total number of INCREMENT shown at the right. Notice that the total cost is always less than twice the total number of INCREMENT Pf. At most k bits flippedoperations. per increment. ▪ operations. operations on an initially zero counter causes AŒ1 to flip n=2 times. Similarly,13 operations on an initially zero counter causes AŒ1 to flip n=2 times. Similarly,14 b c b c bit AŒ2 flips only every fourth time, or n=4 times in a sequence of n INCREMENT bit AŒ2 flips only every fourth time, or n=4 times in a sequence of n INCREMENT b c i b c i operations. In general, for i 0; 1; : : : ; k 1,bitAŒi flips n=2 times in a operations. In general, for i 0; 1; : : : ; k 1,bitAŒi flips n=2 times in a D ! b c D ! b c sequence of n INCREMENT operations on an initially zero counter. For i k, sequence of n INCREMENT operations on an initially zero counter. For i k, " " bit AŒi does not exist, and so it cannot flip. The total number of flips in the bit AŒi does not exist, and so it cannot flip. The total number of flips in the Binary counter: aggregatesequence ismethod thus Accounting method (banker'ssequence is thusmethod) k 1 k 1 ! n 1 1 ! n 1 1