Data structure and algorithm (Python)-General concepts and algorithm efficiency analysis

Source: Internet
Author: User
Tags ticket
It 's written in front .

After learning the Python basics, start with this section to formally learn about data structure and algorithm related content. This is a more complex topic, generally divided into the primary, advanced, and specialized algorithm analysis three stages to learn, so we also need to be gradual. This section is mainly familiar with the general concepts of data structure and algorithms, and then familiar with the algorithm efficiency analysis of large o notation, the knowledge structure as shown in the following figure:

what is an algorithm. 1) The definition of the algorithm

algorithm (algorithm), refers to a description of the steps to solve a particular problem.
In mathematics, it is a finite sequence of operational steps, each of which represents the execution of an operation. For example, a manual calculation of two integers and the algorithm described as: two digits to write on the paper, and then starting from a single digit, bitwise sum, encountered and greater than 10 to the high, and finally calculated the sum of two digits.

In a computer, an algorithm is a finite sequence of instructions, each of which specifies one or more operations. For example, in 12306 Web site to complete the ticket query, ticket ordering and other tasks, on the computer are implemented by a series of algorithms.

In the process of using computer to solve the problem, we first model the problem, construct the appropriate algorithm, and then write the relevant program, the flow is as follows (from introduction to algorithm):

2) algorithm of the 5 major characteristics

For an algorithm, there are 5 major features, listed below:

Enter an algorithm with 0 or more external inputs, Note that you can not enter.

Output an algorithm has one or more outputs, Note that the algorithm must have some form of output.

The poor (finiteness) algorithm must be completed within a finite step.

Each of the deterministic (definiteness ) algorithms must have no ambiguity (only one explanation), and under any condition, the algorithm has only one execution path and only the same output for the same input.

The operation described in the feasibility (effectiveness ) algorithm can be implemented effectively through the basic operation already implemented, which is feasible. 3) The factors of algorithm evaluation

There are different ways to solve the same problem, and how to compare and choose between these methods becomes a key.

For example, to different cities, can choose the means of transport have trains, ships, airplanes, self-driving, passenger buses, carpool and many other, these different ways of travel, in terms of comfort, price, time, security and other aspects have different, need to compare and choose from multiple angles.

The evaluation algorithm is good or bad also has various factors, mainly includes the following factors:

Whether correctness solves the problem correctly.

The readability algorithm is mainly written by people, followed by machine execution. Is easy to understand as the key to implementation and maintenance.

Implementation of difficulty algorithm is easy to achieve.

Is the storage overhead algorithm a reasonable amount of memory or external storage space consumed?

The execution time algorithm is time-consuming and acceptable to execute.

Can a robust program react reasonably to unexpected input? For example, a simple calculation program, when encountered in addition to the 0 operation, should remind the user of the error, rather than the program crashes. What is a data structure. 1) abstract data type

The first step in solving the problem by computer is to model the problem, in the process of modeling, we need to take into account the data input, processing, input and other content, the algorithm describes the operation of the specific process of data, but how to represent and store the data in the problem model, you need to select or redesign a favorable structure.
Abstract data type (Types,adt) is a theoretical concept that describes, from a logical level, the range of possible values, allowable operations, and behavioral performance of operations. ADT is independent of specific implementation details (implementation-independent ). An integer, for example, is a ADT, and its possible values include -1,0,1 ..., the allowed operations include subtraction, and greater than, less than, and so on. These are mathematical models that have nothing to do with how they are expressed in calculations. 2) Data structure

ADT is a theoretical mathematical model, and the data structure is the realization of the abstract data type on the computer, the concept of implementation level, which is realized by the specific computer language and the basic type of the language. 3) The difference between ADT and data structure

The difference between them can be seen from the above definition. For example, stacks (stack) is a kind of ADT, defined it is an advanced structure, supporting operations include: into the stack (push), out of the stack (POP), look at the top of the stack, and determine whether the stack is empty (empty) and so on 4 kinds of operations. The computation can be realized through an array, called Arraystack, or by a chain list, called Linkliststack. These two concrete implementations are called stacks of data structures. evaluation criteria for algorithm efficiency

As mentioned above, if we evaluate transportation, we may choose comfort, time, safety, price and other criteria to judge, similar to judge an algorithm good or bad need some criteria.

When the spatial and time complexity of an algorithm is analyzed, it can be analyzed by statistical analysis after the execution of the program ( ex post Statistical Method ), or it can be analyzed theoretically (ex ante estimation method ) When the program is not executed. For the same algorithm, executed on a different machine, by the processor, machine length, storage space, instruction set, and so on, the run-time differences, such as running in the "Tianhe" supercomputer and ordinary PC program, running time may be very different; the same algorithm, even if the same machine to run the program, But programs written with C or Ada are about 20 times times faster than those written in Basic or Lisp. As a result, the method of statistical analysis is often unreliable (except for performance comparisons of programs written for a particular device), so the method of prior analysis of estimates is frequently used.

Since the method is estimated beforehand, and there is no actual execution of the program, it is clearly impossible to use absolute units of byte size or length of time , and a theoretical abstract standard should be used. In the above we mentioned such factors as readability, correctness and so on, these factors are necessary for every good algorithm, these factors are not distinguishable, the real factor is the degree of space complexity (spatialcomplexity) and time complexity Complexity) two standards. These two complexities, generally with the amount of data entered by the problem, that is, with the problem scale N, into some kind of functional relationship, such as time complexity can be expressed as:
T (n) =f (n) t (n) =f (n).

In seeking this function relationship, we first find an operation that is used as the basic operation (Elementary Operation) to estimate the relationship between its execution times and N. The so-called basic operation refers to the algorithm in the time has a key impact, and the scale of the problem is proportional to the operation. For example, checking whether an element x is in a set of number A and comparing X to an element in a is equal to the value of an operation, can be considered as a basic operation.

def find_in_array (array,val): "" "
    naive search algorithm
    :p Aram array:input elements Array
    :p Aram Val : The  value to search
    : return:index if found or-1
    ""
    for I, X in enumerate (array):
        if x = = val:
  
   # Basic Operation return
           I
    return-1
  

In the above lookup process, we encounter 3 scenarios: in the worst case (worst-case) the element to look for is in the last position of the array t (n) =n t (n) =n average (average case) Assuming that each element has the same probability of being looked up, the average lookup requires a number of comparisons: T (n) =∑ni=11n∗i=1+n2 t (n) =\sum_{i=1}^{n}\frac{1}{n}*i=\frac{1+n}{2} Best Case ( Best-case) to find the element in the first position of the array t (n) =1 t (n) =1

Let's focus on the Big O notation of time complexity. Progressive Analysis Method 1 What is the Progressive analysis method?

The goal of the Asymptotic analysis method (asymptotic analyses) is to find the problem-handling time and the scale of the problem, as the scale of the problem when the upper and lower relations, through the upper limit we understand the worst of the algorithm, through the lower limit to understand the best case of the algorithm.

Big O definition: suppose f (n) f (n) is the representation of the algorithm time complexity, and g (n) g (n) is the most influential factor, if: F (n) <=CG (n) f (n), for all n>=n0,c>0,n0>=1 n >= n_{0}, C > 0,n_{0} >= 1 is established, then we can write F (n) f (n) as: F (n) =o (g (n)) f (n) =o (g (n))

The most influential factor in the above definition is the part of the complexity expression that has the most effect on the result, for example: F (n) =5n2+2n+1 f (n) =5n^2 + 2n + 1, then when n increases, it is clear that N2 n^2 determines f

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.