博文

目前显示的是标签为“Amortized analysis”的博文

[ACE] Amortized analysis

图片
Amortized analysis Why talking about amortized analysis When we are learning “Vector” in ACE class, we came up with a growable array-based vector. We want to know what if we execute “push” operation, but the array is full. Instead of throwing a exception, we would like to replace the array with a larger one Algorithm push(o) if t = s.length -1 then A <- new array of size ... for i <- 0 to t do A[i] <- S[i] t <- t+1 A[t+1] <- o garbage collection S From pseudo-code above, what should be the size of the new array? There are two simple strategy you can came up with quickly • incremental strategy: increase size by a constant c • doubling strategy: double the size What is the common analysis for this question? Suppose some individual operation (such as ‘push’) takes time T in the worst-case. So for the total s operations, the worst-case time should be sT(upper bound) • however, such an upper-bound might not ever occur. Because the...