Simple understanding of algorithms-analysis and Analysis

Source: Internet
Author: User

Simple understanding of algorithms-analysis and Analysis
The spread analysis is used to evaluate the average cost of an operation sequence in a program. Sometimes the cost of an operation is very high, but it is not so bad in general, it can be interpreted as "allocating" high-cost operations to other operations. The requirement is the average price after the even allocation. There are also three common analysis technologies: Aggregation analysis, accounting, and potential energy. Let's take a look at the example. There are three types of operations: push (s), pop (s), mutlipop (s, k), and push (s), collectively referred to as stack operations. Push (s) can only press one data at a time. Therefore, the operation cost is 1, and pop (s) can only play one data at a time. Therefore, the operation cost is 1, mutlipop (s, k) internally implements a loop pop-up. The cost of each execution is k (k <n, n is the maximum capacity of the stack ). Now the problem arises. I want to analyze the time complexity in the worst case of running n stack operations? The first reaction should be like this: mutlipop (s, k) has the highest price, with the highest bit k = n. The worst case for executing n times is o (n2, in fact, this is not the case. Aggregation analysis requires us to look at the problem as a whole. First, mutlipop (s, k) is also a stack operation, which is effective only when there is data in the stack, therefore, the o (n2) mentioned above is unscientific, while the price of push (s) and pop (s) is 1, we can imagine the worst case. Of course, the first n-1 operation is a stack, and the last operation is mutlipop (n-1). This cost is only 2n-2, the time complexity is o (n). On average, the cost of each operation is o (1. The accounting method is easy to understand. When the analysis is conducted, the cost of the calculation may be higher than or equal to the actual cost, the difference above the actual cost is stored in a data structure called credit, which can be filled in when there is less than the actual cost. Note that these algorithms only provide ideas and come up with a series of ideas on the upper limit of operation costs. The specific practices should be considered by yourself. The same is the example of stack pressure. We can grant the Push (s) operation a price of 2, which is equivalent to 1, and the press will surely pop up, the remaining 1 is used as the fee for pop-up. In this way, the cost of pop (s) and mutlipop (s, k) is 0. What is the significance of this? Try to think about the problem (marked in red) now. The worst case is that all operations are Push (s) at a cost of 2n, the other two operations (the amortized cost is 0) need not be considered as the aggregation cost, and the time complexity is O (n ). The potential energy method is actually a bit similar to the accounting method, but it is not called credit but potential energy. The potential energy method is based on the whole, unlike the accounting method, a specific operation is the overall potential energy. The potential energy method defines a formula; Ci (amortize) = Ci (actual) + f (Di)-f (Di-1) (1) the formula for accumulating the total amortize cost is; ci (total return) = Ci (total actual) + f (Di)-f (D0) (2) where Ci is the cost of each step, f (Di) indicates the potential energy after the I operation is executed, the formula can be understood as the amortized cost of step I operation is equal to the actual cost of step I operation plus the potential energy change from step I-1 operation to step I operation, after understanding this, let's look at the stack operation example. Similarly, the price of Push (s) and pop (s) is 1, and the price of mutlipop (s, k) is k. We need to add 1 to an element potential energy in the stack, if an element potential energy is reduced by 1, f (Di) is never negative, and f (D0) is equal to 0, then according to the above formula (2) you can see that here we can determine that the total cost is the upper limit of the total actual cost, so what we need now is the total cost. According to the formula (1), we can get the price of Push (s) is 2, the price of pop (s) is 0, and the price of mutlipop (s, k) is) the spread cost is also 0 (because the stack potential energy is reduced, it is just offset by the cost, it can also be seen as the potential energy is used to pay the price, so it is reduced ), then return to the accounting method, and the time complexity is O (n ). There is another example in the book about table expansion. Although I can understand it, I still don't understand it very well (I don't understand why he defined it). I 'd like to share with you here, for more information, see ~ Example: Table expansion is like this. There is a program. Suppose this table only allows insertion. When the inserted data finds that the table is full, a new table is automatically created, which is twice the original size, then, copy the existing data and insert it again. The problem is that the cost of this program is required. To be precise, it is still a price. Here, I only want to write two methods. I am dizzy when I write more .. first of all, in aggregate analysis, the cost of inserting a piece of data is regarded as 1. Assume that the table size at the beginning is 1 and there is no data, except that the table data is full, in other cases, the cost is 1, and when the table is full, the size is the power of 2. When the table is full, the cost of data insertion is the cost of the original data replication k (assuming there are k data records) plus the newly inserted one, in addition, the total share cost is C (total share cost) <= n + 20 + 21 + .. + 2lgn <n + 2n = 3n; (here, lgn indicates that the table has been expanded for a total of lgn times and can be calculated). So we will take a look at the accounting method below the cost of 3n/n = 3, the accounting method can be used to better understand why the price is 3. this is the analysis in the book. If the table contains m/2 pieces of data, the table size is m, and there is no credit, it will cost three to insert a piece of data. Why? It seems that 1 is consumed during insertion, 1 is saved as your own credit, and 1 is donated data that is originally in the table but has no credit. In this way, however, if m/2 data records are inserted and m data records exist in total, this happens when the table is to be expanded and all data in the table has 1 credit, it can be used to pay the cost of copying an extended table to a new table, so that the table size becomes 2 MB, and the data is just half the size of the table without credit, which is back to the beginning, just like recursion. That is to say, a piece of data can be expanded forever at three costs...

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.