This is the first example of the expansion of the table to introduce today's theme--split analysis and potential energy analysis
What is the appropriate size for a hash table?
Theta (n) More suitable
But what if we don't know how big N is?
Use dynamic tables to resolve overflows create a double-size space and then copy the past
The worst time to do this is to insert a complexity of n
Let's look at the average time complexity, each time the basic insert operation is 1, the space overflow need to open a larger space, and copy the current element in the past, so the space overflow when the time required to 2 of the I-side (I is the first several overflow)
So its real time consumption is N+sigma (2^i) 0<=I<=LG (n+1) is 3n
So the insertion time complexity is O (1)
Although there are sometimes huge costs, they are evenly divided by the average cost, which is split analysis
Split analysis: Average operation complexity is not high, although some operations will have a high degree of complexity
Three types of averaging methods:
1. Aggregation Analysis
2. Bookkeeping Method (Accounting)
3. Potential energy analysis
2.3 They assign a single cost to each operation
Bookkeeping method:
Imagine yourself as a will-remember
For the I operation charge for CI
The cost of earning a fictitious
Every step of the operation takes 1$
Unused balances are deposited in the bank and used to repay subsequent operations
If the charge for each insertion is 3, the insertion consumes 1, the remaining 2 is deposited to the bank to be prepared to double the table, always ensure that the bank's amount is positive
That is, you can always pay for the scale-up, so that a high-cost operation will be split off.
Potential energy methods:
One of the most beautiful products in algorithmic analysis.
At first, the data structure State is D0
The cost of operating i is CI
Operation I can be seen as converting data structures from Di-1 to Di
Defining potential energy functions
Positioning the collection of data structures as real values
D0 = 0 The initial potential is 0
All di >=0, we can't let the potential fall below 0.
Define the cost for the AI, the potential energy Di has ai=ci+di-di-1
Di-di-1 part is the amount of potential energy change, if its >=0 so ai>ci I charge more than the actual cost, that is, I stored the data structure of the following
If the amount of potential energy changes is <0, that is, we use the stored potential energy to transform it to help complete the operation I
The accounting method considers the cost
and the potential energy analysis is about bank deposits (storing potential energy)
With Sigmaai=sigma (ci+di-di-1) =sigma (ci+dn-d0)
D0 is 0,dn greater than or equal to 0, so the left is greater than the right, an upper bound of the actual cost
Once again, let's take a look at the amplification of the table to feel the potential energy analysis
Our potential energy function is 2i-2^ceil (LGI)
How do you derive such a potential energy function?
Defining potential energy functions is less difficult than the defined cost
Di>=0
The cost of the ai=ci+di-di-1= I+2i-2^ceil (LGI)-(2i-2-2^ceil (lgi-1)) (just I is a power of 2)
1+2i-2^ceil (LGI)-(2i-2-2^ceil (lgi-1))
Case1 I-1 is a power of 2, then ai=i+2-2 (i-1) + (i-1) =3
Case2 I-1 is not a power of 2 so Ai=1+2i-2^ceil (LGI)-(2i-2-2^ceil (lgi-1)) =3
And the cost of this is 3.
Pay no attention to real-time performance only focus on aggregation performance
"Introduction to the Algorithm" The 13th lesson divided analysis, table amplification, potential energy analysis