Algorithm complexity-mathematical principles of algorithm time complexity: from O (N (log (n)

Source: Internet
Author: User

Overview: when designing an algorithm, consider two aspects: one is the correctness of the algorithm, and the other is the efficiency of the algorithm, that is, complexity. Generally, we give priority to time complexity, which is also discussed in this article. This problem is often encountered during algorithm learning. Why is the time complexity of fast sorting O (nlog (N ))? Why is the time complexity of insertion sorting O (N ^ 2 )? These are the time complexity of the algorithms we are familiar with, and there may be no major problems with the disease. What are we not familiar? If we adopt three-way Merge Sorting instead of two-way Merge Sorting, what is the time complexity? What is the time complexity of a sorting algorithm after some deformation? This article describes how to deduce the time complexity of an algorithm at the bottom of mathematics. Let you really know what it is, not just always have doubts: Why is the time complexity of fast sorting so strange O (nlog (N ))


1. Basic Mathematical Knowledge


First, introduce the following basic mathematical knowledge, which are distributed in advanced mathematics and discrete mathematics without mathematical derivation.

Some inequalities:


 

 

 

 

Whether it is merge or fast sort, we can sum up them to solve the recursive/divide-based problem. They have a general time complexity statement:

The meaning of this equation is that the problem of scale is N, which can be split into a problem where scale is N/B, then, the time complexity is equal to the problem that a's scale is N/B, plus the time spent by a decomposition D (N) and the time spent by a merge C (n ). The second part and the fourth part introduce three methods for solving this equation.


2. Recursive tree method


This is the most intuitive method. It visualizes the preceding equations and then solves them. We use an example to illustrate this situation.

Example: Use the recursive tree to solve t (n) = T (N/10) + T (9n/10) + CN

The recursive tree is shown as follows:


Key Point: Find the depth of the tree and the cost of each layer (note: In this example, it is better to solve the problem because the cost of each layer is the same; but in other cases, it may be that the cost of each layer is different, but an proportional series or other forms of Series)


1) where the depth of the tree is easy to solve: n --> 9/10n --> 81/100 (n) ...... n/n = 1

That is to say, this recursive descent satisfies this trend (B = 10/9 ):

Therefore

So

2) cost of layer I:

The size of each layer is 1/10n and 9/10n respectively, and the cost of each node is cN/10 and 9cn/10. Therefore, the added layer is CN.


3) Total cost:

So

T (n) = O (nlgn)


3. Main Method


The algorithm complexity of the following expressions

T (n) = at (N/B) + f (N) (a> = 1, B> 1)

Proof of the main method: Refer to Chapter 4 of Introduction to Algorithms

The sum formula of the proportional series can be used to solve the problem.


4. Replacement replacement method


Note: This method requires a certain amount of experience, which is a bit similar to mathematical induction. You can guess and then prove it.

1) step: the representation of time complexity is similar

2) key points: To make guesses accurate, to make induction assumptions strong enough to avoid weakening proofs. Replace non-polynomial Variables

Border problems: Moving borders and enhancing inductive assumptions can be used to solve these problems.


Instance:

1) prove that T (n) = T (n/2) + n time complexity O (nlogn)

Make T (n) <cnlgn

Then


Note: The computation of this algorithm complexity is more effective for algorithms based on the branching method.

 

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.