[Introduction to algorithms] course 1: analysis of table sharing, table amplification, and potential energy analysis; Introduction to Amplification

Source: Internet
Author: User

[Introduction to algorithms] course 1: analysis of table sharing, table amplification, and potential energy analysis; Introduction to Amplification
First, we will introduce today's topic-level analysis and potential energy analysis through the example of table amplification.


What is the size of a hash table?

Theta (n) is suitable
But what if we don't know how big n is?
When dynamic tables are used to solve overflow, a space doubling the size is created and copied.
The worst time for insert is n.


Let's take a look at the average time complexity. Each basic insert operation is 1. When the space overflows, we need to open a larger space and copy the current element, so the time required for space overflow is the I power of 2 (I is the first overflow)


Therefore, the actual time consumption is n + sigma (2 ^ I) 0 <= I <= lg (n + 1) or 3n
Therefore, the insertion time complexity is O (1)


Although there are sometimes huge overhead, it will be evenly distributed by the average overhead.


Horizontal analysis: the average operation complexity is not high, although some operations have a high complexity


Three types of Balancing Methods:
1. Clustering Analysis
2. accounting Method)
3. Potential Energy Analysis
2.3 each operation is allocated with a characteristic price.




Accounting method:
Imagine you have assumed a meeting
The I-th operation is charged as ci
Fictitious profit share price
1 $ for each operation
Unused balance is deposited in the bank for future operations




If the charge for each insert is 3, the insert consumes 1, and the remaining 2 is saved to the bank to prepare for doubling the table, always ensure that the Bank amount is positive.


That is to say, you can always pay for the expansion table in advance, so that a high-sales operation will be amortized.


Potential Energy Method:
One of the most beautiful products in algorithm analysis
At the beginning, the data structure status is D0.
The operation I cost is ci.
Operation I can be seen as converting the data structure from Di-1 to Di


Define potential energy functions
Locate the set of data structures
D0 = 0 the initial potential is 0
All Di> = 0, we cannot let the potential below 0
Defines the price for Ai, the potential Di Ai = Ci + Di-Di-1
The Di-Di-1 part is the amount of potential energy change, if it> = 0 then the Ai> ci I charge more than the actual cost, that is, the operation I save the work required for the subsequent data structure
If the amount of potential energy changes is <0, we use the stored potential energy to convert it into energy to help complete the operation I




The billing method takes into account the price of a fee.
Potential Energy analysis considers bank deposits (storage potential energy)


With sigmaAi = sigma (ci + Di-Di-1) = sigma (ci + Dn-D0)
D0 is 0, Dn is greater than or equal to 0, so the left side is greater than the right side, is an upper limit of the actual cost


Let's take the expansion of the table as an example to analyze the potential energy.
Our potential energy function is 2i-2 ^ ceil (lgi)
How can we derive such potential energy functions?
Difficulty in defining potential energy functions is lower than the definition price.
Di> = 0
I = Ci + Di-Di-1 = I + 2i-2 ^ ceil (lgi)-(2i-2-2 ^ ceil (lgi-1) (I is a power of 2)
1 + 2i-2 ^ ceil (lgi)-(2i-2-2 ^ ceil (lgi-1 ))


Case1 I-1 is 2 power, then Ai = I + 2-2 (I-1) + (I-1) = 3
Case2 I-1 is not 2 power so Ai = 1 + 2i-2 ^ ceil (lgi)-(2i-2-2 ^ ceil (lgi-1) = 3
In this way, the price is also 3.


Focus only on clustering performance without focusing on real-time performance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.