DS algorithm Overview

Source: Internet
Author: User
An algorithm is a description of the specific process of solving a problem. It is a finite sequence of commands. Each Command represents one or more operations. In addition, an algorithm also has five important features. (1) A poor algorithm must always (for any valid input) end after a poor step is executed, and each step can be within a short time (reasonable and acceptable)

An algorithm is a description of the specific process of solving a problem. It is a finite sequence of commands. Each Command represents one or more operations. In addition, an algorithm also has five important features. (1) A poor algorithm must always (for any valid input) end after a poor step is executed, and each step can be within a short time (reasonable and acceptable)

Algorithm

An algorithm is a description of the specific process of solving a problem. It is a finite sequence of commands. Each Command represents one or more operations. In addition, an algorithm also has five important features.

(1) A poor algorithm must always (for any valid input value) end after a poor step is executed, and each step can be within a short time (reasonable, within an acceptable time.

(2) Each instruction in a deterministic algorithm must have a definite meaning, and readers do not have any ambiguity when understanding it. In addition, the algorithm has only one execution path under any conditions, that is, the same output can be obtained for the same input.

(3) The feasibility of an algorithm can be achieved, that is, the operations described in the algorithm can be implemented through the implementation of basic operations for a limited number of times.

(4) input an algorithm has zero or multiple inputs, which are taken from a specific object set.

(5) the output of an algorithm has one or more outputs, which have certain relations with the input.

Algorithm Design Requirements

(1) The correctness algorithm must meet the needs of specific problems.

(2) Readability algorithms are mainly used for reading and communicating, followed by machine execution. Readability helps people understand algorithms.

(3) Robustness when the input data is invalid, the algorithm can also properly respond to or process the data without any inexplicable output results.

(4) efficiency and low storage capacity. Efficiency refers to the algorithm execution time. If multiple algorithms can solve the same problem, the efficiency of algorithms with short execution time is high. The storage capacity requirement refers to the maximum storage space required during Algorithm Execution. Both efficiency and low storage requirements are related to the problem scale.

Algorithm efficiency measurement

The Algorithm Execution time must be measured by the time consumed by the program running on the computer based on the algorithm. There are two methods to measure the execution time of a program:

(1) post-event statistics

(2) methods for pre-ANALYSIS AND ESTIMATION

The time it takes for a program written in advanced programming languages to run on a computer depends on the following factors:

1. which policy is selected based on the algorithm?

2. Problem Scale

3. The higher the language level for the same algorithm, the lower the execution efficiency.

4. The quality of the machine code produced by compiling the program

5. speed at which the machine executes commands

An algorithm consists of a control structure (sequence, branch, and loop) and an original operation (operations of inherent data types). The algorithm time depends on the sum of the two. To make it easier to compare different algorithms of the same problem, the common practice is to select an original operation for the problem (or algorithm type) studied from the algorithm as the basic operation, the number of repeated executions of the basic operation is used as the time measurement of the algorithm.

Time complexity of the algorithm

In general, the number of repeated executions of the basic operation in an algorithm is a function f (n) of the problem scale n. The time measure of the algorithm is recorded as T (n) = O (f (n), which indicates that with the increase of the problem scale n, the growth rate of Algorithm Execution time is the same as that of f (n). It is called the algorithm's gradual time complexity, time complexity.

Obviously, the original operation of the basic operation called the problem should be the original operation whose number of repeated executions is proportional to the execution time of the algorithm, in most cases, it is the original operation in the statements in the deepest loop, and its execution frequency is the same as that of the statements containing it. The statement frequency refers to the number of times that the statement is repeatedly executed.

The following examples illustrate the time complexity.

1, {++ x; s = 0;} the frequency of the "x increment 1" statement is 1, and the time complexity is O (1) constant order.

2, for (int I = 1; I <= n; I ++) {++ x; s + = x ;} if the frequency of the statement "x increases by 1" is n, the time complexity is O (n) linear order.

3, for (int I = 1; I <= n; I ++)

For (int j = 1; j <= n; j ++) {++ x; s ++ = x ;} if the frequency of the statement "x increases by 1" is n * n, the time complexity is O (n * n ).

The algorithm may also present the time complexity of the logarithm order, exponential order, etc. [from the Internet (http://www.68idc.cn )].

Algorithm storage space requirements

This is similar to the time complexity of an algorithm. The space complexity is used as a measure of the storage space required by the algorithm. It is recorded as S (n) = O (f (n )), n indicates the scale (or size) of the problem ). In addition to the storage space required to store commands, constants, variables, and input data, it also requires some work orders for data operations and storage of some auxiliary space for the information required for calculation. If the space occupied by the input data depends only on the problem and is irrelevant to the algorithm, you only need to analyze the extra space except the input and program, otherwise, we should also consider the space required for the input (which is related to the representation of the input data ). If the extra space is a constant relative to the input data volume, this algorithm is called in-situ operation.

If the amount of space occupied depends on a specific input, the analysis is based on the worst case except the specified one.

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.