Algorithm complexity of the front-end learning algorithm

Source: Internet
Author: User
Tags types of functions disk usage

Previous words

This article describes the complexity of the algorithm in detail

Large O notation

Large o notation is a description of the performance and complexity of the algorithm. The following types of functions are often encountered when parsing an algorithm

symbol             nameO (1) constant O (log (n)) logarithm O (        (log (n)) c)    logarithmic polynomial O (n) Linear O (            n2)            two times O ( NC)            polynomial of the O (CN)            Index

How to measure the efficiency of an algorithm? Typically, resources are used, such as CPU (time) consumption, memory consumption, hard disk usage, and network usage. When large o notation is discussed, the general consideration is CPU (time) consumption

Here are some examples to understand the rules of the Big O notation

"O (1)"

function Increment (num) {   return + +num;}

Assume that you run the increment (1) function and the execution time equals X. If you run the increment function again with a different parameter (for example, 2), the execution time is still x. Regardless of the parameters, the increment function has the same performance. Therefore, we say that the complexity of the above function is O (1) (constant)

"O (N)"

Now take the sequential search algorithm as an example:

function Sequentialsearch (array, item) {   for(var i=0; i<array.length; i++ ) {    if//      return  i;    }  }   return -1;}

If you pass an array of 10 elements ([1, ..., 10]) to the function, if you search for the 1 element, you will be able to find the element you want to search for the first time. Here we assume that each time the row {1} is executed, the cost is 1.

Now, if you want to search for element 11. The row {1} executes 10 times (iterates through all the values in the array and cannot find the element to be searched, resulting in a return of-1). If the cost of row {1} is 1, then it executes 10 times the cost is 10, 10 times times the first hypothesis

Now, if the array has 1000 elements ([1, ..., 1000]). The result of searching for 1001 is that line {1} was executed 1000 times (and then returned-1)

The total cost of executing the Sequentialsearch function depends on the number of elements in the array (array size), and also about the value of the search. If you are looking for a value that exists in the array, how many times does the row {1} execute? If you are looking for a value that does not exist in the array, the row {1} executes as many times as the number of arrays, which is usually the worst case scenario.

In the worst case, if the array size is 10, the overhead is 10; If the array size is 1000, the overhead is 1000. It can be concluded that the time complexity of the Sequentialsearch function is O (n) and n is the size of the (input) array

Back to the previous example, modify the implementation of the algorithm to calculate the overhead:

function Sequentialsearch (array, item) {varCost =0;  for(varI=0; i<array.length; i++) { cost++; if(item = = = Array[i]) {//{1}    returni; }} console.log ('Cost to sequentialsearch with input size'+Array.Length+' is'+Cost ); return-1;} 

Execute the above algorithm with different size input array, you can see the different output

"O(n2)"

Examples of O(n2) with bubbling sort:

function Swap (array, index1, index2) {varAux =array[index1]; Array[index1]=ARRAY[INDEX2]; Array[index2]=aux;} function Bubblesort (array) {varLength =Array.Length; for(varI=0; i<length; i++) {//{1}   for(varj=0; j<length-1; J + +) {//{2}    if(Array[j] > array[j+1]) {Swap (array, J, J+1); }  } }} 

Suppose the cost of row {1} and row {2} is 1 respectively. Modify the implementation of the algorithm to calculate the overhead:

function Bubblesort (array) {varLength =Array.Length;varCost =0;  for(varI=0; i<length; i++) {//{1}cost++;  for(varj=0; j<length-1; J + +) {//{2}cost++; if(Array[j] > array[j+1]) {Swap (array, J, J+1); }}} console.log ('Cost to bubblesort with input size'+ length +' is'+Cost );} 

If you execute bubblesort with an array of size 10, the overhead is 100 (102). If you execute bubblesort with an array of size 100, the overhead is 10 000 (1002). Be aware that every time we increase the size of the input, the execution will be more and more long

The time complexity o(n) code has only one layer of loops, while the o(n2) code has a double nested loop. If the algorithm has three layers of nested loops traversing an array, its time complexity is likely to be O(n3)

Complexity of Time

The time complexity of each of the above-mentioned large O symbols is compared:

The following table is the time complexity of commonly used data structures

The following table is the time complexity of the graph:

The following table is the time complexity of the sorting algorithm:

The following table is the time complexity of the search algorithm:

Np

In general, if the complexity of an algorithm is O (NK), where k is constant, we think the algorithm is efficient, this is the polynomial algorithm

For a given problem, if there is a polynomial algorithm, it is counted as p (polynomial, polynomial)

There is also a class of NP (Nondeterministicpolynomial, non-deterministic polynomial) algorithm. If a problem can verify that the solution is correct within the polynomial time, it is counted as NP. If a problem exists in a polynomial algorithm, it is naturally possible to validate its solution in polynomial time. Therefore, all P are NP. However, it remains unclear whether the P=NP is tenable. The most difficult NP problem is the NP complete problem, it satisfies the following two conditions: (1) is a NP problem, that is, the solution can be verified in polynomial time, but has not found a polynomial algorithm, (2) All NP problems can be in the polynomial time to be about it. In order to understand the problem, consider two decision questions L and M. Assuming that algorithm a solves the problem L, the algorithm B can verify that the input Y is the solution of M. The goal is to find a way to convert L to M, so that algorithm B can be used to construct algorithm a

There is also a kind of problem, which only satisfies the second condition of NP complete problem, which is called NP difficult problem. Therefore, NP-complete problems are also subsets of NP-hard problems

The following are Eulerian graph for P < > NP, P, NP, NP-complete, and NP-hard problems:

Examples of non-NP-complete NP-hard problems are downtime problems and Boolean gratification issues (SAT). Examples of NP complete problems are subsets and problems, travel quotient problems, vertex coverage problems, and so on.

Some of the problems we have mentioned are not solvable. However, there is still a way to find an approximate solution within the time that meets the requirements. Heuristic algorithm is one of them. The heuristic algorithm is not necessarily the optimal solution, but it is enough to solve the problem. Examples of heuristic algorithms are local search, genetic algorithm, heuristic navigation, machine learning, etc.

Algorithm complexity of the front-end learning algorithm

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.