Introduction to algorithms-aggregation Analysis of analysis on the same level, and introduction to algorithms-equal level analysis

Source: Internet
Author: User

Introduction to algorithms-aggregation Analysis of analysis on the same level, and introduction to algorithms-equal level analysis

In the analysis, the time required to perform a series of data structure operations is obtained by averaging all the operations executed.

The spread analysis can be used to prove that in a series of operations, even if a single operation has a high price after the average of all operations, the average price is still very small. The difference between the case-based analysis and the case-based analysis is that the case-based analysis does not involve probability. The case-based analysis ensures that each operation has an average performance.

Clustering Analysis

In clustering analysis, we need to prove that the total time for the sequence of all n, composed of n operations is T (n) in the worst case). Therefore, in the worst case, the average cost (or even analyzed) of each operation is T (n)/n.Please note that this spread price applies to each operation, even if there are several types of operations in the sequence.

Stack operations

In the first example of clustering analysis, we want to analyze the stack after adding a new operation. There are two basic stack operations. The time cost of each operation is O (1 ):

PUSH (S, x): PUSH object x to stack S.

POP (S): the POP-up object is returned at the top of the POP-up S stack.

Because the running time of these two operations is O (1), the cost of each operation can be considered as 1. Therefore, the total cost of a sequence Containing n PUSH and POP operations is n, and the actual running time of these n operations is O (n ).

Now we add a stack operation MULTIPOP (S, k), which is used to pop up k stack top objects of stack S, or when the stack contains less than k objects, the data object in the entire stack is displayed.

When MULTIPOP (S, s) operates on a stack containing s objects, what is the running time? The actual running time has a linear relationship with the actually executed POP operations. Therefore, it is sufficient to analyze MULTIPOP with an abstract cost of 1 by PUSH and POP. The number of while LOOP iterations is the number of objects popped up from the stack in min (s, k ). You must call POP once for each iteration of the loop. Therefore, the total cost of MULTIPOP is min (s, k), and the actual running time is a linear function of this price.

Now we can analyze a sequence composed of n PUSH, POP, and MULTIPOP operations, which act on a stack with an initial empty value. The worst case for a MULTIPOP operation in a sequence is O (n), because the stack size is mostly n. Therefore, the worst case time for any stack operation is O (n). Therefore, the cost of n operation sequences is O (n ^ 2 ),Because there may be O (n) MULTIPOP operations, each cost is O (n). Although this analysis is correct, the O (n ^ 2) conclusion obtained by separately considering the worst case price of each operation is not accurate enough.

Using clustering analysis, we can obtain a better upper bound of the entire sequence considering n operations. In fact, although the cost of a MULTIPOP operation may be high, when it is used as a stack that is initially empty, any one contains n pushes, the cost of the sequence of POP and MULTIPOP operations is mostly O (n ). Why? An object isAfter being pushed into the stack, It is pushed up to once. Therefore, on a non-empty stack, the number of POP calls (including MULTIPOP calls) is at most equal to the number of PUSH operations, that is, n. The total time for any n values including n PUSH, POP, and MULTIPOP operations is O (n ). The average cost of each operation is O (n)/n = O (1 ). In clustering analysis, we assign the average cost for each operation. In this example, the price of the three stack operations is O (1 ).

We once again stressed that although the average cost and running time of a stack operation are O (1), probabilistic reasoning is not used.In fact, we provide the worst case O (n) for n operations in a column ). In addition to the total cost, n can be used to obtain the average cost of each operation, that is, to share the cost.

Related Article

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.