10th session: What is an algorithm?

Source: Internet
Author: User

Many problems, the algorithm is not unique. The same problem, there are a number of algorithms to solve the problem. Because the algorithm is not unique, the relative good algorithm still exists. What's the algorithm for applause?

First an algorithm must have the following properties:

    1. The algorithm must first be correct, that is, for any set of input, including reasonable input and unreasonable input, always get the expected output. If an algorithm only obtains the expected output for a reasonable input, and the result of the output cannot be predicted in exceptional cases, then it is not correct.
    2. The algorithm must be composed of a series of specific steps, and each step can be understood and executed by the computer, rather than abstract and fuzzy concepts.
    3. Each step has a definite order of execution, that is, where in the previous step, what is next, must be clear, no ambiguity.
    4. No matter how complex the algorithm is, it must end in a finite step and terminate the operation, that is, the steps of the algorithm must be limited. In any case, the algorithm cannot be trapped in an infinite loop.

A solution to a problem can be expressed in a variety of ways, but only the solutions that meet the above 4 criteria are called algorithms.

1. Correctness

Correctness: The correctness of the algorithm is that the algorithm should have at least input, output and processing without ambiguity, can correctly reflect the needs of the problem, can get the correct answer.

However, the "correct" algorithm is usually very different in usage, broadly divided into the following four levels.

    1. The algorithm program has no syntax errors.
    2. The algorithm program can produce the output result satisfying the requirement for the valid input data.
    3. The algorithm program is able to produce results that satisfy the specification for illegal input data.
    4. The algorithm program for carefully selected, even difficult test data have to meet the requirements of the output results.
    • For these four layers of meaning, level 1 requires the lowest, but just no grammatical error is not a good algorithm. This is like just a solution to food and clothing, not a life of happiness. and level 4 is the hardest, and it's almost impossible to verify all of the inputs to get the right results.

Therefore, the correctness of the algorithm can not be proved by program in most cases, but is proved by mathematical method. Prove that a complex algorithm is correct at all levels and is expensive. So in general, we put level 3 as a criterion for whether an algorithm is correct.

2. Readability

Readability: Another purpose of algorithm design is to facilitate reading, understanding and communication.

    • High readability helps people understand algorithms, obscure algorithms tend to imply errors, are difficult to detect, and are difficult to debug and modify.

We write code for the purpose, on the one hand, for the computer to execute, but there is an important purpose is to facilitate the reading of others, let people understand and communicate, they may also read in the future, if the readability is not good, the time has not known what to write. Readability is an important sign of the algorithm (and the code that implements it).

3. Robustness

A good algorithm should also be able to handle the situation where the input data is not valid. For example, the input time or distance should not be negative and so on.

Robustness: When the input data is not legal, the algorithm can also make relevant processing, rather than produce abnormal or inexplicable results.

4. High time efficiency and low storage capacity

Finally, a good algorithm should also have the characteristics of high time efficiency and low storage capacity.

    • Time efficiency refers to the execution time of the algorithm, for the same problem, if there are more than one algorithm can be solved, the algorithm of short execution time is high efficiency, long execution time is low efficiency.
    • Storage requirement refers to the maximum amount of memory that is required during the execution of the algorithm, mainly refers to the RAM or external hard disk storage space occupied by the algorithm when the program runs.

The design algorithm should try to meet the needs of time efficiency and low storage. In life, people want to spend the least money, with the shortest time, do the biggest thing, the algorithm is the same idea, preferably with the least storage space, spend the least time, do the same thing is a good algorithm.

    • In conclusion, good algorithms should have the characteristics of correctness, readability, robustness, high efficiency and low storage capacity.
Extended Reading

10th session: What is an 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.